Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat : #183 태그 등록, 수정 api 구현 #185

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -49,4 +47,43 @@ public ResponseEntity<BaseResponse<PageResponse<TagDto>>> getTagList(
new BaseResponse<PageResponse<TagDto>>().data(tagList)
);
}

/**
* 태그 생성 요청
* @param tagName 태그 이름
* @return 200 OK, 메세지
* @author Yujin-nKim(김유진)
*/
@PostMapping("/tags")
public ResponseEntity<BaseResponse<Void>> createTag(@RequestParam String tagName) {
tagService.createTag(tagName);
return ResponseEntity.status(HttpStatus.OK).body(
new BaseResponse<Void>().message("태그 생성 요청이 처리되었습니다."));
}

/**
* 태그 수정 요청
* @param tagId 수정할 태그의 식별자
* @param tagName 태그 이름
* @return 200 OK, 메세지
* @author Yujin-nKim(김유진)
*/
@PutMapping("/tags/{tagId}")
public ResponseEntity<BaseResponse<Void>> modifyTag(@PathVariable Long tagId, @RequestParam String tagName) {
tagService.modifyTag(tagId, tagName);
return ResponseEntity.status(HttpStatus.OK).body(
new BaseResponse<Void>().message("태그 수정 요청이 처리되었습니다."));
}

/**
* 태그 상세 조회
* @param tagId 수정할 태그의 식별자
* @return 태그 상세
* @author Yujin-nKim(김유진)
*/
@GetMapping("/tags/{tagId}")
public ResponseEntity<BaseResponse<TagDto>> getTag(@PathVariable Long tagId) {
return ResponseEntity.status(HttpStatus.OK).body(
new BaseResponse<TagDto>().data(tagService.getTag(tagId)));
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
28 changes: 28 additions & 0 deletions src/main/java/com/t3t/bookstoreapi/tag/model/entity/Tag.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,11 @@ public interface TagRepository extends JpaRepository<Tag, Long> {
* @author Yujin-nKim(김유진)
*/
Page<Tag> findAll(Pageable pageable);

/**
* 주어진 태그 이름이 존재하는지 여부를 확인
* @param tagName 확인할 태그 이름
* @return 태그 이름이 존재하면 true, 그렇지 않으면 false를 반환
*/
boolean existsByTagName(String tagName);
}
35 changes: 35 additions & 0 deletions src/main/java/com/t3t/bookstoreapi/tag/service/TagService.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -44,4 +46,37 @@ public PageResponse<TagDto> 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));
}
}
Loading