diff --git a/src/main/java/com/t3t/bookstoreapi/book/model/entity/Book.java b/src/main/java/com/t3t/bookstoreapi/book/model/entity/Book.java index 9df385c8..779f9733 100644 --- a/src/main/java/com/t3t/bookstoreapi/book/model/entity/Book.java +++ b/src/main/java/com/t3t/bookstoreapi/book/model/entity/Book.java @@ -8,7 +8,6 @@ import com.t3t.bookstoreapi.order.exception.BookStockShortageException; import com.t3t.bookstoreapi.publisher.model.entity.Publisher; import lombok.*; -import org.springframework.data.jpa.repository.Lock; import javax.persistence.*; import java.math.BigDecimal; diff --git a/src/main/java/com/t3t/bookstoreapi/tag/controller/TagController.java b/src/main/java/com/t3t/bookstoreapi/tag/controller/TagController.java index 48ddd048..3ccb0dfd 100644 --- a/src/main/java/com/t3t/bookstoreapi/tag/controller/TagController.java +++ b/src/main/java/com/t3t/bookstoreapi/tag/controller/TagController.java @@ -10,9 +10,7 @@ import org.springframework.data.domain.Sort; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; @RestController @@ -49,4 +47,43 @@ public ResponseEntity>> getTagList( new BaseResponse>().data(tagList) ); } + + /** + * 태그 생성 요청 + * @param tagName 태그 이름 + * @return 200 OK, 메세지 + * @author Yujin-nKim(김유진) + */ + @PostMapping("/tags") + public ResponseEntity> createTag(@RequestParam String tagName) { + tagService.createTag(tagName); + return ResponseEntity.status(HttpStatus.OK).body( + new BaseResponse().message("태그 생성 요청이 처리되었습니다.")); + } + + /** + * 태그 수정 요청 + * @param tagId 수정할 태그의 식별자 + * @param tagName 태그 이름 + * @return 200 OK, 메세지 + * @author Yujin-nKim(김유진) + */ + @PutMapping("/tags/{tagId}") + public ResponseEntity> modifyTag(@PathVariable Long tagId, @RequestParam String tagName) { + tagService.modifyTag(tagId, tagName); + return ResponseEntity.status(HttpStatus.OK).body( + new BaseResponse().message("태그 수정 요청이 처리되었습니다.")); + } + + /** + * 태그 상세 조회 + * @param tagId 수정할 태그의 식별자 + * @return 태그 상세 + * @author Yujin-nKim(김유진) + */ + @GetMapping("/tags/{tagId}") + public ResponseEntity> getTag(@PathVariable Long tagId) { + return ResponseEntity.status(HttpStatus.OK).body( + new BaseResponse().data(tagService.getTag(tagId))); + } } diff --git a/src/main/java/com/t3t/bookstoreapi/tag/exception/TagNameAlreadyExistsException.java b/src/main/java/com/t3t/bookstoreapi/tag/exception/TagNameAlreadyExistsException.java new file mode 100644 index 00000000..877ccac7 --- /dev/null +++ b/src/main/java/com/t3t/bookstoreapi/tag/exception/TagNameAlreadyExistsException.java @@ -0,0 +1,10 @@ +package com.t3t.bookstoreapi.tag.exception; + +public class TagNameAlreadyExistsException extends RuntimeException{ + + private static final String DEFAULT_MESSAGE = "이미 등록된 태그 이름입니다."; + + public TagNameAlreadyExistsException() { + super(DEFAULT_MESSAGE); + } +} diff --git a/src/main/java/com/t3t/bookstoreapi/tag/model/entity/Tag.java b/src/main/java/com/t3t/bookstoreapi/tag/model/entity/Tag.java index 7f850b14..047818ad 100644 --- a/src/main/java/com/t3t/bookstoreapi/tag/model/entity/Tag.java +++ b/src/main/java/com/t3t/bookstoreapi/tag/model/entity/Tag.java @@ -1,10 +1,16 @@ package com.t3t.bookstoreapi.tag.model.entity; import javax.validation.constraints.NotNull; + +import com.t3t.bookstoreapi.tag.model.dto.TagDto; import lombok.*; import javax.persistence.*; +/** + * 태그(tags) 엔티티 + * @author Yujin-nKim(김유진) + */ @NoArgsConstructor(access = AccessLevel.PROTECTED) @AllArgsConstructor @Builder @@ -21,4 +27,26 @@ public class Tag { @NotNull @Column(name = "tag_name") private String tagName; + + /** + * 태그의 이름을 업데이트 + * @param tagName 업데이트 이름 + * @author Yujin-nKim(김유진) + */ + public void updateTagName(String tagName) { + this.tagName = tagName; + } + + /** + * Tag 객체를 TagDto 객체로 변환 + * @param tag tag 객체 + * @return TagDto 객체 + * @author Yujin-nKim(김유진) + */ + public TagDto of(Tag tag) { + return TagDto.builder() + .id(tag.getTagId()) + .name(tag.getTagName()) + .build(); + } } diff --git a/src/main/java/com/t3t/bookstoreapi/tag/repository/TagRepository.java b/src/main/java/com/t3t/bookstoreapi/tag/repository/TagRepository.java index 4f6677e1..257b96ed 100644 --- a/src/main/java/com/t3t/bookstoreapi/tag/repository/TagRepository.java +++ b/src/main/java/com/t3t/bookstoreapi/tag/repository/TagRepository.java @@ -15,4 +15,11 @@ public interface TagRepository extends JpaRepository { * @author Yujin-nKim(김유진) */ Page findAll(Pageable pageable); + + /** + * 주어진 태그 이름이 존재하는지 여부를 확인 + * @param tagName 확인할 태그 이름 + * @return 태그 이름이 존재하면 true, 그렇지 않으면 false를 반환 + */ + boolean existsByTagName(String tagName); } diff --git a/src/main/java/com/t3t/bookstoreapi/tag/service/TagService.java b/src/main/java/com/t3t/bookstoreapi/tag/service/TagService.java index 55954469..d5cc3654 100644 --- a/src/main/java/com/t3t/bookstoreapi/tag/service/TagService.java +++ b/src/main/java/com/t3t/bookstoreapi/tag/service/TagService.java @@ -1,6 +1,8 @@ package com.t3t.bookstoreapi.tag.service; import com.t3t.bookstoreapi.model.response.PageResponse; +import com.t3t.bookstoreapi.tag.exception.TagNameAlreadyExistsException; +import com.t3t.bookstoreapi.tag.exception.TagNotFoundException; import com.t3t.bookstoreapi.tag.model.dto.TagDto; import com.t3t.bookstoreapi.tag.model.entity.Tag; import com.t3t.bookstoreapi.tag.repository.TagRepository; @@ -44,4 +46,37 @@ public PageResponse getTagList(Pageable pageable) { .last(tagPage.isLast()) .build(); } + + /** + * 태그 생성 + * @param tagName 태그 이름 + * @author Yujin-nKim(김유진) + */ + public void createTag(String tagName) { + if (tagRepository.existsByTagName(tagName)) { + throw new TagNameAlreadyExistsException(); + } + tagRepository.save(Tag.builder().tagName(tagName).build()); + } + + /** + * 태그 수정 + * @param tagId 수정할 태그의 식별자 + * @param tagName 태그 이름 + * @author Yujin-nKim(김유진) + */ + public void modifyTag(Long tagId, String tagName) { + Tag tag = tagRepository.findById(tagId).orElseThrow(TagNotFoundException::new); + tag.updateTagName(tagName); + } + + /** + * 태그 상세 조회 + * @param tagId 수정할 태그의 식별자 + * @return 태그 상세 + * @author Yujin-nKim(김유진) + */ + public TagDto getTag(Long tagId) { + return TagDto.of(tagRepository.findById(tagId).orElseThrow(TagNotFoundException::new)); + } } \ No newline at end of file