From 2fe59ebe45dddfca58d19e3b541fa2e2388230aa Mon Sep 17 00:00:00 2001 From: hyeonda02 Date: Wed, 24 Jul 2024 23:46:03 +0900 Subject: [PATCH 1/5] =?UTF-8?q?Refactor=20:=20advice,=20exception=20?= =?UTF-8?q?=ED=81=B4=EB=9E=98=EC=8A=A4=20=EC=9C=84=EC=B9=98=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../umc/kkijuk/server/career/service/CareerServiceImpl.java | 2 +- .../controller}/CareerExceptionControllerAdvice.java | 3 ++- .../domian}/exception/CareerValidationException.java | 2 +- .../umc/kkijuk/server/career/service/CareerServiceTest.java | 4 ++-- .../kkijuk/server/career/service/CareerServiceUnitTest.java | 2 +- 5 files changed, 7 insertions(+), 6 deletions(-) rename src/main/java/umc/kkijuk/server/{career/controller/exception => common/controller}/CareerExceptionControllerAdvice.java (92%) rename src/main/java/umc/kkijuk/server/{career/controller => common/domian}/exception/CareerValidationException.java (73%) 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 28f4c5e3..a4e96ec1 100644 --- a/src/main/java/umc/kkijuk/server/career/service/CareerServiceImpl.java +++ b/src/main/java/umc/kkijuk/server/career/service/CareerServiceImpl.java @@ -3,7 +3,7 @@ import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import umc.kkijuk.server.career.controller.exception.CareerValidationException; +import umc.kkijuk.server.common.domian.exception.CareerValidationException; import umc.kkijuk.server.career.controller.response.CareerGroupedByResponse; import umc.kkijuk.server.career.controller.response.CareerResponseMessage; import umc.kkijuk.server.career.domain.Career; diff --git a/src/main/java/umc/kkijuk/server/career/controller/exception/CareerExceptionControllerAdvice.java b/src/main/java/umc/kkijuk/server/common/controller/CareerExceptionControllerAdvice.java similarity index 92% rename from src/main/java/umc/kkijuk/server/career/controller/exception/CareerExceptionControllerAdvice.java rename to src/main/java/umc/kkijuk/server/common/controller/CareerExceptionControllerAdvice.java index 7045dbee..b15580de 100644 --- a/src/main/java/umc/kkijuk/server/career/controller/exception/CareerExceptionControllerAdvice.java +++ b/src/main/java/umc/kkijuk/server/common/controller/CareerExceptionControllerAdvice.java @@ -1,4 +1,4 @@ -package umc.kkijuk.server.career.controller.exception; +package umc.kkijuk.server.common.controller; import com.fasterxml.jackson.databind.exc.InvalidFormatException; @@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.RestControllerAdvice; import umc.kkijuk.server.career.controller.response.CareerResponse; import umc.kkijuk.server.career.controller.response.CareerResponseMessage; +import umc.kkijuk.server.common.domian.exception.CareerValidationException; @RestControllerAdvice(assignableTypes = {umc.kkijuk.server.career.controller.CareerController.class}) diff --git a/src/main/java/umc/kkijuk/server/career/controller/exception/CareerValidationException.java b/src/main/java/umc/kkijuk/server/common/domian/exception/CareerValidationException.java similarity index 73% rename from src/main/java/umc/kkijuk/server/career/controller/exception/CareerValidationException.java rename to src/main/java/umc/kkijuk/server/common/domian/exception/CareerValidationException.java index c468b0ee..2fec85e1 100644 --- a/src/main/java/umc/kkijuk/server/career/controller/exception/CareerValidationException.java +++ b/src/main/java/umc/kkijuk/server/common/domian/exception/CareerValidationException.java @@ -1,4 +1,4 @@ -package umc.kkijuk.server.career.controller.exception; +package umc.kkijuk.server.common.domian.exception; public class CareerValidationException extends RuntimeException{ diff --git a/src/test/java/umc/kkijuk/server/career/service/CareerServiceTest.java b/src/test/java/umc/kkijuk/server/career/service/CareerServiceTest.java index 4bb070e9..da701ef7 100644 --- a/src/test/java/umc/kkijuk/server/career/service/CareerServiceTest.java +++ b/src/test/java/umc/kkijuk/server/career/service/CareerServiceTest.java @@ -10,8 +10,8 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.annotation.DirtiesContext; -import umc.kkijuk.server.career.controller.exception.CareerExceptionControllerAdvice; -import umc.kkijuk.server.career.controller.exception.CareerValidationException; +import umc.kkijuk.server.common.controller.CareerExceptionControllerAdvice; +import umc.kkijuk.server.common.domian.exception.CareerValidationException; import umc.kkijuk.server.career.controller.response.CareerGroupedByResponse; import umc.kkijuk.server.career.controller.response.CareerResponse; import umc.kkijuk.server.career.controller.response.CareerResponseMessage; diff --git a/src/test/java/umc/kkijuk/server/career/service/CareerServiceUnitTest.java b/src/test/java/umc/kkijuk/server/career/service/CareerServiceUnitTest.java index 1b2f479d..658a43d1 100644 --- a/src/test/java/umc/kkijuk/server/career/service/CareerServiceUnitTest.java +++ b/src/test/java/umc/kkijuk/server/career/service/CareerServiceUnitTest.java @@ -6,7 +6,7 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import umc.kkijuk.server.career.controller.exception.CareerValidationException; +import umc.kkijuk.server.common.domian.exception.CareerValidationException; import umc.kkijuk.server.career.domain.Career; import umc.kkijuk.server.career.domain.Category; import umc.kkijuk.server.career.dto.CareerRequestDto; From d51dfa5e8bd514e319da7245ea5337951edfe4d0 Mon Sep 17 00:00:00 2001 From: hyeonda02 Date: Fri, 26 Jul 2024 21:26:58 +0900 Subject: [PATCH 2/5] =?UTF-8?q?Feat=20:=20Career=20=ED=99=9C=EB=8F=99=20?= =?UTF-8?q?=ED=83=9C=EA=B7=B8=20=EC=83=9D=EC=84=B1,=20=EC=A1=B0=ED=9A=8C,?= =?UTF-8?q?=20=EC=82=AD=EC=A0=9C=20API=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ExceptionControllerAdvice.java | 7 +++ .../exception/InvalidTagNameException.java | 8 ++++ .../server/tag/controller/TagController.java | 46 +++++++++++++++++++ .../umc/kkijuk/server/tag/domain/Tag.java | 18 ++++++++ .../kkijuk/server/tag/dto/TagRequestDto.java | 25 ++++++++++ .../kkijuk/server/tag/dto/TagResponseDto.java | 32 +++++++++++++ .../tag/dto/converter/TagConverter.java | 35 ++++++++++++++ .../server/tag/repository/TagRepository.java | 11 +++++ .../kkijuk/server/tag/service/TagService.java | 12 +++++ .../server/tag/service/TagServiceImpl.java | 43 +++++++++++++++++ 10 files changed, 237 insertions(+) create mode 100644 src/main/java/umc/kkijuk/server/common/domian/exception/InvalidTagNameException.java create mode 100644 src/main/java/umc/kkijuk/server/tag/controller/TagController.java create mode 100644 src/main/java/umc/kkijuk/server/tag/domain/Tag.java create mode 100644 src/main/java/umc/kkijuk/server/tag/dto/TagRequestDto.java create mode 100644 src/main/java/umc/kkijuk/server/tag/dto/TagResponseDto.java create mode 100644 src/main/java/umc/kkijuk/server/tag/dto/converter/TagConverter.java create mode 100644 src/main/java/umc/kkijuk/server/tag/repository/TagRepository.java create mode 100644 src/main/java/umc/kkijuk/server/tag/service/TagService.java create mode 100644 src/main/java/umc/kkijuk/server/tag/service/TagServiceImpl.java 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 eee72568..ca77aac5 100644 --- a/src/main/java/umc/kkijuk/server/common/controller/ExceptionControllerAdvice.java +++ b/src/main/java/umc/kkijuk/server/common/controller/ExceptionControllerAdvice.java @@ -5,6 +5,7 @@ import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestControllerAdvice; +import umc.kkijuk.server.common.domian.exception.InvalidTagNameException; import umc.kkijuk.server.common.domian.exception.ResourceNotFoundException; import umc.kkijuk.server.common.domian.exception.ReviewRecruitNotMatchException; import umc.kkijuk.server.common.domian.response.ErrorResponse; @@ -24,4 +25,10 @@ public ErrorResponse resourceNotFoundException(ResourceNotFoundException excepti public ErrorResponse ReviewRecruitMatchException(ReviewRecruitNotMatchException exception) { return new ErrorResponse(exception.getMessage()); } + + @ResponseStatus(HttpStatus.BAD_REQUEST) + @ExceptionHandler(InvalidTagNameException.class) + public ErrorResponse DuplicateTagNameException(InvalidTagNameException exception) { + return new ErrorResponse(exception.getMessage()); + } } diff --git a/src/main/java/umc/kkijuk/server/common/domian/exception/InvalidTagNameException.java b/src/main/java/umc/kkijuk/server/common/domian/exception/InvalidTagNameException.java new file mode 100644 index 00000000..d7a862b3 --- /dev/null +++ b/src/main/java/umc/kkijuk/server/common/domian/exception/InvalidTagNameException.java @@ -0,0 +1,8 @@ +package umc.kkijuk.server.common.domian.exception; + +public class InvalidTagNameException extends RuntimeException{ + public InvalidTagNameException(String message) { + super(message); + + } +} diff --git a/src/main/java/umc/kkijuk/server/tag/controller/TagController.java b/src/main/java/umc/kkijuk/server/tag/controller/TagController.java new file mode 100644 index 00000000..08a41fa1 --- /dev/null +++ b/src/main/java/umc/kkijuk/server/tag/controller/TagController.java @@ -0,0 +1,46 @@ +package umc.kkijuk.server.tag.controller; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import jakarta.validation.Valid; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; +import umc.kkijuk.server.tag.domain.Tag; +import umc.kkijuk.server.tag.dto.TagRequestDto; +import umc.kkijuk.server.tag.dto.TagResponseDto; +import umc.kkijuk.server.tag.dto.converter.TagConverter; +import umc.kkijuk.server.tag.service.TagService; + +@io.swagger.v3.oas.annotations.tags.Tag(name="tag",description = "태그 관련 API") +@RestController +@RequiredArgsConstructor +@RequestMapping("/career") +public class TagController { + + private final TagService tagService; + + @PostMapping("/tag") + @Operation(summary = "태그 추가 API", description = "태그 - 태그를 생성하는 API") + public ResponseEntity create(@RequestBody @Valid TagRequestDto.CreateTagDto request) { + Tag createTag = tagService.createTag(request); + return ResponseEntity + .ok() + .body(TagConverter.toTagResult(createTag)); + } + @GetMapping("/tag") + @Operation(summary = "태그 조회 API", description = "태그 - 태그 조회하는 API") + public ResponseEntity read() { + return ResponseEntity + .ok() + .body(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) { + tagService.delete(tagId); + return ResponseEntity.ok().body(null); + } + +} diff --git a/src/main/java/umc/kkijuk/server/tag/domain/Tag.java b/src/main/java/umc/kkijuk/server/tag/domain/Tag.java new file mode 100644 index 00000000..29a8e271 --- /dev/null +++ b/src/main/java/umc/kkijuk/server/tag/domain/Tag.java @@ -0,0 +1,18 @@ +package umc.kkijuk.server.tag.domain; + +import jakarta.persistence.*; +import lombok.*; + +@Entity +@Getter +@Builder +@AllArgsConstructor(access = AccessLevel.PROTECTED) +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class Tag { + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name="hashtag_id") + private Long id; + @Column(name="hashtag_name", length = 30) //일단 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 new file mode 100644 index 00000000..25779fca --- /dev/null +++ b/src/main/java/umc/kkijuk/server/tag/dto/TagRequestDto.java @@ -0,0 +1,25 @@ +package umc.kkijuk.server.tag.dto; + +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Size; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + + +public class TagRequestDto { + @Getter + @Builder + @AllArgsConstructor + @NoArgsConstructor + public static class CreateTagDto{ + @NotBlank(message = "해시태그 이름은 필수 입력 사항입니다.") + @Size(max = 30) + @Schema(description = "해시태그 이름", example = "역량 키워드", type="string") + String tagName; + + + } +} diff --git a/src/main/java/umc/kkijuk/server/tag/dto/TagResponseDto.java b/src/main/java/umc/kkijuk/server/tag/dto/TagResponseDto.java new file mode 100644 index 00000000..7fe6f120 --- /dev/null +++ b/src/main/java/umc/kkijuk/server/tag/dto/TagResponseDto.java @@ -0,0 +1,32 @@ +package umc.kkijuk.server.tag.dto; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.util.List; + +public class TagResponseDto { + @Getter + @Builder + @NoArgsConstructor + @AllArgsConstructor + public static class ResultTagDto{ + @Schema(description = "생성된 활동 태그 Id", example = "1", type = "Long") + private Long id; + @Schema(description = "생성된 활동 태그 이름", example = "핵심 역량", type = "String") + private String tagName; + } + @Getter + @Builder + @NoArgsConstructor + @AllArgsConstructor + public static class ResultTagDtoList{ + @Schema(description = "활동 태그들의 개수",example = "3") + private int count; + @Schema(description = "활동 태그들 리스트") + private List tagList; + } +} diff --git a/src/main/java/umc/kkijuk/server/tag/dto/converter/TagConverter.java b/src/main/java/umc/kkijuk/server/tag/dto/converter/TagConverter.java new file mode 100644 index 00000000..1fa47ec3 --- /dev/null +++ b/src/main/java/umc/kkijuk/server/tag/dto/converter/TagConverter.java @@ -0,0 +1,35 @@ +package umc.kkijuk.server.tag.dto.converter; + +import umc.kkijuk.server.tag.domain.Tag; +import umc.kkijuk.server.tag.dto.TagRequestDto; +import umc.kkijuk.server.tag.dto.TagResponseDto; + +import java.util.List; +import java.util.stream.Collectors; + +public class TagConverter { + public static Tag toTag(TagRequestDto.CreateTagDto request) { + return Tag.builder() + .name(request.getTagName()) + .build(); + } + public static TagResponseDto.ResultTagDto toTagResult(Tag hashTag) { + return TagResponseDto.ResultTagDto.builder() + .id(hashTag.getId()) + .tagName(hashTag.getName()) + .build(); + } + + public static TagResponseDto.ResultTagDtoList toResultTagDtoList(List tagList) { + return TagResponseDto.ResultTagDtoList.builder() + .count(tagList.size()) + .tagList(tagList.stream().map( + value -> TagResponseDto.ResultTagDto.builder() + .tagName(value.getName()) + .id(value.getId()) + .build() + ).collect(Collectors.toList())) + .build(); + } + +} diff --git a/src/main/java/umc/kkijuk/server/tag/repository/TagRepository.java b/src/main/java/umc/kkijuk/server/tag/repository/TagRepository.java new file mode 100644 index 00000000..c0161873 --- /dev/null +++ b/src/main/java/umc/kkijuk/server/tag/repository/TagRepository.java @@ -0,0 +1,11 @@ +package umc.kkijuk.server.tag.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import umc.kkijuk.server.tag.domain.Tag; + +import java.util.List; + +public interface TagRepository extends JpaRepository { + boolean existsByName(String name); + List findAll(); +} diff --git a/src/main/java/umc/kkijuk/server/tag/service/TagService.java b/src/main/java/umc/kkijuk/server/tag/service/TagService.java new file mode 100644 index 00000000..bbfafc33 --- /dev/null +++ b/src/main/java/umc/kkijuk/server/tag/service/TagService.java @@ -0,0 +1,12 @@ +package umc.kkijuk.server.tag.service; + +import umc.kkijuk.server.tag.domain.Tag; +import umc.kkijuk.server.tag.dto.TagRequestDto; +import umc.kkijuk.server.tag.dto.TagResponseDto; + + +public interface TagService { + Tag createTag(TagRequestDto.CreateTagDto request); + TagResponseDto.ResultTagDtoList findAllTags(); + void delete(Long tagId); +} diff --git a/src/main/java/umc/kkijuk/server/tag/service/TagServiceImpl.java b/src/main/java/umc/kkijuk/server/tag/service/TagServiceImpl.java new file mode 100644 index 00000000..3d9993f2 --- /dev/null +++ b/src/main/java/umc/kkijuk/server/tag/service/TagServiceImpl.java @@ -0,0 +1,43 @@ +package umc.kkijuk.server.tag.service; + + +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import umc.kkijuk.server.common.domian.exception.InvalidTagNameException; +import umc.kkijuk.server.tag.domain.Tag; +import umc.kkijuk.server.tag.dto.TagRequestDto; +import umc.kkijuk.server.tag.dto.TagResponseDto; +import umc.kkijuk.server.tag.dto.converter.TagConverter; +import umc.kkijuk.server.tag.repository.TagRepository; + +import java.util.List; +import java.util.Optional; + +@Service +@Transactional(readOnly = true) +@RequiredArgsConstructor +public class TagServiceImpl implements TagService { + private final TagRepository tagRepository; + @Override + @Transactional + public Tag createTag(TagRequestDto.CreateTagDto request) { + String tagName = request.getTagName(); + if (tagName == null || tagName.trim().isEmpty() || tagRepository.existsByName(tagName)) { + throw new InvalidTagNameException("태그 이름은 공백일 수 없으며, 이미 존재하는 이름은 사용할 수 없습니다."); + } + Tag hashTag = TagConverter.toTag(request); + return tagRepository.save(hashTag); + } + @Override + public TagResponseDto.ResultTagDtoList findAllTags() { + return TagConverter.toResultTagDtoList(tagRepository.findAll()); + } + @Override + @Transactional + public void delete(Long tagId) { + Tag deleteTag = tagRepository.findById(tagId).get(); + tagRepository.delete(deleteTag); + } + +} From 636a837748de2ede8305f872847060ff7c8f9c2f Mon Sep 17 00:00:00 2001 From: hyeonda02 Date: Fri, 26 Jul 2024 23:28:10 +0900 Subject: [PATCH 3/5] =?UTF-8?q?Refactor=20:=20Career=20=ED=99=9C=EB=8F=99?= =?UTF-8?q?=20=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); } + } From fdcf15fec2acfb12092235549db5d28298707a8f Mon Sep 17 00:00:00 2001 From: hyeonda02 Date: Fri, 26 Jul 2024 23:37:07 +0900 Subject: [PATCH 4/5] =?UTF-8?q?Test=20:=20Career=20=EC=98=88=EC=99=B8?= =?UTF-8?q?=EC=B2=98=EB=A6=AC=20=EB=B6=80=EB=B6=84=EC=97=90=20=EB=8C=80?= =?UTF-8?q?=ED=95=9C=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../umc/kkijuk/server/career/service/CareerServiceTest.java | 5 +++-- .../kkijuk/server/career/service/CareerServiceUnitTest.java | 6 ++++-- .../java/umc/kkijuk/server/tag/service/TagServiceTest.java | 4 ++++ 3 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 src/test/java/umc/kkijuk/server/tag/service/TagServiceTest.java diff --git a/src/test/java/umc/kkijuk/server/career/service/CareerServiceTest.java b/src/test/java/umc/kkijuk/server/career/service/CareerServiceTest.java index da701ef7..9b232090 100644 --- a/src/test/java/umc/kkijuk/server/career/service/CareerServiceTest.java +++ b/src/test/java/umc/kkijuk/server/career/service/CareerServiceTest.java @@ -21,6 +21,7 @@ import umc.kkijuk.server.career.dto.CareerResponseDto; 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; @@ -172,7 +173,7 @@ void init() { .build(); //when //then - assertThrows(CareerValidationException.class, () -> careerService.updateCareer(999L,updateCareerDto)); + assertThrows(ResourceNotFoundException.class, () -> careerService.updateCareer(999L,updateCareerDto)); } @Test void update_수정시_날짜_형식이_잘못된_경우_에러() { @@ -225,7 +226,7 @@ void init() { //given //when //then - assertThrows(CareerValidationException.class, () -> careerService.deleteCareer(999L)); + assertThrows(ResourceNotFoundException.class, () -> careerService.deleteCareer(999L)); } diff --git a/src/test/java/umc/kkijuk/server/career/service/CareerServiceUnitTest.java b/src/test/java/umc/kkijuk/server/career/service/CareerServiceUnitTest.java index 658a43d1..b25fc4b6 100644 --- a/src/test/java/umc/kkijuk/server/career/service/CareerServiceUnitTest.java +++ b/src/test/java/umc/kkijuk/server/career/service/CareerServiceUnitTest.java @@ -12,6 +12,8 @@ import umc.kkijuk.server.career.dto.CareerRequestDto; import umc.kkijuk.server.career.dto.CareerResponseDto; import umc.kkijuk.server.career.repository.CareerRepository; +import umc.kkijuk.server.common.domian.exception.ResourceNotFoundException; + import java.time.LocalDate; import java.util.Arrays; import java.util.List; @@ -96,7 +98,7 @@ void init() { when(careerRepository.findById(anyLong())).thenReturn(Optional.empty()); //when //then - assertThrows(CareerValidationException.class, ()->{ + assertThrows(ResourceNotFoundException.class, ()->{ careerService.deleteCareer(1L);}); verify(careerRepository,never()).delete(any(Career.class)); } @@ -115,7 +117,7 @@ void init() { // when // then - assertThrows(CareerValidationException.class, () -> careerService.updateCareer(999L, updateCareerDto)); + assertThrows(ResourceNotFoundException.class, () -> careerService.updateCareer(999L, updateCareerDto)); verify(careerRepository, never()).save(any(Career.class)); } diff --git a/src/test/java/umc/kkijuk/server/tag/service/TagServiceTest.java b/src/test/java/umc/kkijuk/server/tag/service/TagServiceTest.java new file mode 100644 index 00000000..600a477b --- /dev/null +++ b/src/test/java/umc/kkijuk/server/tag/service/TagServiceTest.java @@ -0,0 +1,4 @@ +package umc.kkijuk.server.tag.service; + +public class TagServiceTest { +} From 5ab6712ffa5748a42bb14d843a81f2d5e9df1fef Mon Sep 17 00:00:00 2001 From: hyeonda02 Date: Sat, 27 Jul 2024 00:22:34 +0900 Subject: [PATCH 5/5] =?UTF-8?q?Test=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=EC=97=90=20?= =?UTF-8?q?=EB=8C=80=ED=95=9C=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tag/controller/response/TagResponse.java | 1 - .../kkijuk/server/tag/dto/TagRequestDto.java | 1 - .../kkijuk/server/tag/service/TagService.java | 2 +- .../server/tag/service/TagServiceTest.java | 134 ++++++++++++++++++ 4 files changed, 135 insertions(+), 3 deletions(-) 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 index 24130ce7..6c4ed7d4 100644 --- a/src/main/java/umc/kkijuk/server/tag/controller/response/TagResponse.java +++ b/src/main/java/umc/kkijuk/server/tag/controller/response/TagResponse.java @@ -4,7 +4,6 @@ import lombok.Builder; import lombok.Getter; import org.springframework.http.HttpStatus; -import umc.kkijuk.server.career.controller.response.CareerResponse; @Builder @Getter 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 44320383..41de90f7 100644 --- a/src/main/java/umc/kkijuk/server/tag/dto/TagRequestDto.java +++ b/src/main/java/umc/kkijuk/server/tag/dto/TagRequestDto.java @@ -1,7 +1,6 @@ package umc.kkijuk.server.tag.dto; import io.swagger.v3.oas.annotations.media.Schema; -import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.Size; import lombok.AllArgsConstructor; import lombok.Builder; diff --git a/src/main/java/umc/kkijuk/server/tag/service/TagService.java b/src/main/java/umc/kkijuk/server/tag/service/TagService.java index bbfafc33..60e4cdc6 100644 --- a/src/main/java/umc/kkijuk/server/tag/service/TagService.java +++ b/src/main/java/umc/kkijuk/server/tag/service/TagService.java @@ -7,6 +7,6 @@ public interface TagService { Tag createTag(TagRequestDto.CreateTagDto request); - TagResponseDto.ResultTagDtoList findAllTags(); + TagResponseDto.ResultTagDtoList findAllTags(); void delete(Long tagId); } diff --git a/src/test/java/umc/kkijuk/server/tag/service/TagServiceTest.java b/src/test/java/umc/kkijuk/server/tag/service/TagServiceTest.java index 600a477b..82f80dd8 100644 --- a/src/test/java/umc/kkijuk/server/tag/service/TagServiceTest.java +++ b/src/test/java/umc/kkijuk/server/tag/service/TagServiceTest.java @@ -1,4 +1,138 @@ package umc.kkijuk.server.tag.service; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.DirtiesContext; +import umc.kkijuk.server.common.domian.exception.InvalidTagNameException; +import umc.kkijuk.server.common.domian.exception.ResourceNotFoundException; +import umc.kkijuk.server.tag.domain.Tag; +import umc.kkijuk.server.tag.dto.TagRequestDto; +import umc.kkijuk.server.tag.dto.TagResponseDto; +import umc.kkijuk.server.tag.repository.TagRepository; + +import java.util.List; +import java.util.Optional; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertAll; +import static org.junit.jupiter.api.Assertions.assertThrows; + +@SpringBootTest +@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) public class TagServiceTest { + @Autowired + private TagService tagService; + @Autowired + private TagRepository tagRepository; + + private Tag tag1; + private Tag tag2; + @BeforeEach + void init() { + tag1 = Tag.builder() + .name("test tag1") + .build(); + tag2 = Tag.builder() + .name("test tag2") + .build(); + tagRepository.save(tag1); + tagRepository.save(tag2); + } + + @Test + void create_새로운_tag_만들기() { + //given + TagRequestDto.CreateTagDto request = TagRequestDto.CreateTagDto.builder() + .tagName("test tag3") + .build(); + //when + Tag newTag = tagService.createTag(request); + //then + assertAll( + ()->assertThat(newTag.getId()).isEqualTo(3L), + ()->assertThat(newTag.getName()).isEqualTo("test tag3") + ); + } + @Test + void create_새로운_tag_만들기_null_입력시_에러() { + //given + TagRequestDto.CreateTagDto request = TagRequestDto.CreateTagDto.builder() + .tagName("") + .build(); + //when + //then + assertThrows(InvalidTagNameException.class, () -> tagService.createTag(request)); + + } + @Test + void create_새로운_tag_만들기_빈_문자열_입력시_에러() { + //given + TagRequestDto.CreateTagDto request = TagRequestDto.CreateTagDto.builder() + .tagName(" ") + .build(); + //when + //then + assertThrows(InvalidTagNameException.class, () -> tagService.createTag(request)); + + } + @Test + void create_새로운_tag_만들기_존재하는_tagName_입력시_에러() { + //given + TagRequestDto.CreateTagDto request = TagRequestDto.CreateTagDto.builder() + .tagName("test tag1") + .build(); + //when + //then + assertThrows(InvalidTagNameException.class, () -> tagService.createTag(request)); + + + } + @Test + void delete_기존_tag_삭제하기() { + //given + Long tagId = 1L; + //when + tagService.delete(tagId); + //then + Optional deletedTag = tagRepository.findById(tagId); + assertThat(deletedTag).isEmpty(); + + } + @Test + void delete_기존_tag_삭제하기_없는_tagId_입력시_에러() { + //given + Long tagId = 999L; + //when + //given + assertThrows(ResourceNotFoundException.class, () -> tagService.delete(tagId)); + + } + @Test + void read_태그_전부_조회하기() { + //given + //when + TagResponseDto.ResultTagDtoList tagListResult = tagService.findAllTags(); + List tagList = tagListResult.getTagList(); + + TagResponseDto.ResultTagDto tag1 = tagList.get(0); + TagResponseDto.ResultTagDto tag2 = tagList.get(1); + + //then + assertAll( + () -> assertThat(tagListResult.getCount()).isEqualTo(2), + () -> assertThat(tagListResult.getTagList().size()).isEqualTo(2), + () -> assertThat(tagList).isNotEmpty(), + () -> assertThat(tagList.size()).isEqualTo(2), + () -> assertThat(tag1.getId()).isEqualTo(1L), + () -> assertThat(tag1.getTagName()).isEqualTo("test tag1"), + () -> assertThat(tag2.getId()).isEqualTo(2L), + () -> assertThat(tag2.getTagName()).isEqualTo("test tag2") + + ); + + + } + }