From 636a837748de2ede8305f872847060ff7c8f9c2f Mon Sep 17 00:00:00 2001 From: hyeonda02 Date: Fri, 26 Jul 2024 23:28:10 +0900 Subject: [PATCH] =?UTF-8?q?Refactor=20:=20Career=20=ED=99=9C=EB=8F=99=20?= =?UTF-8?q?=ED=83=9C=EA=B7=B8=20=EA=B4=80=EB=A0=A8=20API=20=EC=98=88?= =?UTF-8?q?=EC=99=B8=20=EC=B2=98=EB=A6=AC=20=EB=B0=8F=20=EC=9D=91=EB=8B=B5?= =?UTF-8?q?=20=ED=98=95=EC=8B=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../career/service/CareerServiceImpl.java | 4 ++- .../controller/ExceptionControllerAdvice.java | 2 +- .../server/tag/controller/TagController.java | 19 ++++++-------- .../tag/controller/response/TagResponse.java | 25 +++++++++++++++++++ .../umc/kkijuk/server/tag/domain/Tag.java | 6 +++-- .../kkijuk/server/tag/dto/TagRequestDto.java | 5 +--- .../server/tag/service/TagServiceImpl.java | 9 ++++--- 7 files changed, 48 insertions(+), 22 deletions(-) create mode 100644 src/main/java/umc/kkijuk/server/tag/controller/response/TagResponse.java diff --git a/src/main/java/umc/kkijuk/server/career/service/CareerServiceImpl.java b/src/main/java/umc/kkijuk/server/career/service/CareerServiceImpl.java index a4e96ec1..2860da17 100644 --- a/src/main/java/umc/kkijuk/server/career/service/CareerServiceImpl.java +++ b/src/main/java/umc/kkijuk/server/career/service/CareerServiceImpl.java @@ -11,6 +11,8 @@ import umc.kkijuk.server.career.dto.converter.CareerConverter; import umc.kkijuk.server.career.repository.CareerRepository; import umc.kkijuk.server.career.repository.CategoryRepository; +import umc.kkijuk.server.common.domian.exception.ResourceNotFoundException; + import java.time.LocalDate; import java.util.List; import java.util.Map; @@ -90,7 +92,7 @@ public List getCareerGroupedBy(String status) @Override public Optional findCareer(Long careerId) { return Optional.ofNullable(careerRepository.findById(careerId).orElseThrow( - () -> new CareerValidationException(CareerResponseMessage.CAREER_NOT_FOUND.toString()))); + () -> new ResourceNotFoundException("Career",careerId))); } diff --git a/src/main/java/umc/kkijuk/server/common/controller/ExceptionControllerAdvice.java b/src/main/java/umc/kkijuk/server/common/controller/ExceptionControllerAdvice.java index ca77aac5..c9a19da4 100644 --- a/src/main/java/umc/kkijuk/server/common/controller/ExceptionControllerAdvice.java +++ b/src/main/java/umc/kkijuk/server/common/controller/ExceptionControllerAdvice.java @@ -28,7 +28,7 @@ public ErrorResponse ReviewRecruitMatchException(ReviewRecruitNotMatchException @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(InvalidTagNameException.class) - public ErrorResponse DuplicateTagNameException(InvalidTagNameException exception) { + public ErrorResponse InvalidTagNameException(InvalidTagNameException exception) { return new ErrorResponse(exception.getMessage()); } } diff --git a/src/main/java/umc/kkijuk/server/tag/controller/TagController.java b/src/main/java/umc/kkijuk/server/tag/controller/TagController.java index 08a41fa1..054bd093 100644 --- a/src/main/java/umc/kkijuk/server/tag/controller/TagController.java +++ b/src/main/java/umc/kkijuk/server/tag/controller/TagController.java @@ -4,8 +4,9 @@ import io.swagger.v3.oas.annotations.Parameter; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; -import org.springframework.http.ResponseEntity; +import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*; +import umc.kkijuk.server.tag.controller.response.TagResponse; import umc.kkijuk.server.tag.domain.Tag; import umc.kkijuk.server.tag.dto.TagRequestDto; import umc.kkijuk.server.tag.dto.TagResponseDto; @@ -22,25 +23,21 @@ public class TagController { @PostMapping("/tag") @Operation(summary = "태그 추가 API", description = "태그 - 태그를 생성하는 API") - public ResponseEntity create(@RequestBody @Valid TagRequestDto.CreateTagDto request) { + public TagResponse create(@RequestBody @Valid TagRequestDto.CreateTagDto request) { Tag createTag = tagService.createTag(request); - return ResponseEntity - .ok() - .body(TagConverter.toTagResult(createTag)); + return TagResponse.success(HttpStatus.OK,"태그를 성공적으로 생성했습니다.", TagConverter.toTagResult(createTag)); } @GetMapping("/tag") @Operation(summary = "태그 조회 API", description = "태그 - 태그 조회하는 API") - public ResponseEntity read() { - return ResponseEntity - .ok() - .body(tagService.findAllTags()); + public TagResponse read() { + return TagResponse.success(HttpStatus.OK, "모든 태그를 성공적으로 조회했습니다.", tagService.findAllTags()); } @DeleteMapping("/tag/{tagId}") @Operation(summary = "태그 삭제 API",description = "태그 - 태그를 삭제하는 API") @Parameter(name = "tagId",description = "태그 Id, path variable 입니다. 존재하는 태그 Id 값을 넘겨 주세요.",example = "1") - public ResponseEntity delete(@PathVariable Long tagId) { + public TagResponse delete(@PathVariable Long tagId) { tagService.delete(tagId); - return ResponseEntity.ok().body(null); + return TagResponse.success(HttpStatus.OK, "태그 삭제가 성공적으로 이루어졌습니다.", null); } } diff --git a/src/main/java/umc/kkijuk/server/tag/controller/response/TagResponse.java b/src/main/java/umc/kkijuk/server/tag/controller/response/TagResponse.java new file mode 100644 index 00000000..24130ce7 --- /dev/null +++ b/src/main/java/umc/kkijuk/server/tag/controller/response/TagResponse.java @@ -0,0 +1,25 @@ +package umc.kkijuk.server.tag.controller.response; + +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.Builder; +import lombok.Getter; +import org.springframework.http.HttpStatus; +import umc.kkijuk.server.career.controller.response.CareerResponse; + +@Builder +@Getter +public class TagResponse { + private int status; + private String message; + @JsonInclude(JsonInclude.Include.NON_NULL) + private T data; + + public TagResponse(final int status,final String message,T data){ + this.status = status; + this.message = message; + this.data = data; + } + public static TagResponse success(HttpStatus status, String message, T data){ + return new TagResponse<>(status.value(),message,data); + } +} diff --git a/src/main/java/umc/kkijuk/server/tag/domain/Tag.java b/src/main/java/umc/kkijuk/server/tag/domain/Tag.java index 29a8e271..f4d5ef09 100644 --- a/src/main/java/umc/kkijuk/server/tag/domain/Tag.java +++ b/src/main/java/umc/kkijuk/server/tag/domain/Tag.java @@ -1,6 +1,7 @@ package umc.kkijuk.server.tag.domain; import jakarta.persistence.*; +import jakarta.validation.constraints.NotNull; import lombok.*; @Entity @@ -10,9 +11,10 @@ @NoArgsConstructor(access = AccessLevel.PROTECTED) public class Tag { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name="hashtag_id") + @Column(name="tag_id") private Long id; - @Column(name="hashtag_name", length = 30) //일단 length 30 ( 최대 길이 여쭤보고 변경해야 함. ) + @NotNull + @Column(name="tag_name", length = 30) private String name; } diff --git a/src/main/java/umc/kkijuk/server/tag/dto/TagRequestDto.java b/src/main/java/umc/kkijuk/server/tag/dto/TagRequestDto.java index 25779fca..44320383 100644 --- a/src/main/java/umc/kkijuk/server/tag/dto/TagRequestDto.java +++ b/src/main/java/umc/kkijuk/server/tag/dto/TagRequestDto.java @@ -15,11 +15,8 @@ public class TagRequestDto { @AllArgsConstructor @NoArgsConstructor public static class CreateTagDto{ - @NotBlank(message = "해시태그 이름은 필수 입력 사항입니다.") @Size(max = 30) - @Schema(description = "해시태그 이름", example = "역량 키워드", type="string") + @Schema(description = "태그 이름", example = "역량 키워드", type="string") String tagName; - - } } diff --git a/src/main/java/umc/kkijuk/server/tag/service/TagServiceImpl.java b/src/main/java/umc/kkijuk/server/tag/service/TagServiceImpl.java index 3d9993f2..87ee0fdb 100644 --- a/src/main/java/umc/kkijuk/server/tag/service/TagServiceImpl.java +++ b/src/main/java/umc/kkijuk/server/tag/service/TagServiceImpl.java @@ -5,6 +5,8 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import umc.kkijuk.server.common.domian.exception.InvalidTagNameException; +import umc.kkijuk.server.common.domian.exception.ResourceNotFoundException; +import umc.kkijuk.server.common.domian.response.ErrorResponse; import umc.kkijuk.server.tag.domain.Tag; import umc.kkijuk.server.tag.dto.TagRequestDto; import umc.kkijuk.server.tag.dto.TagResponseDto; @@ -23,8 +25,8 @@ public class TagServiceImpl implements TagService { @Transactional public Tag createTag(TagRequestDto.CreateTagDto request) { String tagName = request.getTagName(); - if (tagName == null || tagName.trim().isEmpty() || tagRepository.existsByName(tagName)) { - throw new InvalidTagNameException("태그 이름은 공백일 수 없으며, 이미 존재하는 이름은 사용할 수 없습니다."); + if (tagName==null || tagName.trim().isEmpty() || tagRepository.existsByName(tagName)) { + throw new InvalidTagNameException("태그 이름은 비워둘 수 없으며, 이미 존재하는 이름은 사용할 수 없습니다."); } Tag hashTag = TagConverter.toTag(request); return tagRepository.save(hashTag); @@ -36,8 +38,9 @@ public TagResponseDto.ResultTagDtoList findAllTags() { @Override @Transactional public void delete(Long tagId) { - Tag deleteTag = tagRepository.findById(tagId).get(); + Tag deleteTag = tagRepository.findById(tagId).orElseThrow(() -> new ResourceNotFoundException("Tag", tagId)); tagRepository.delete(deleteTag); } + }