diff --git a/src/main/java/store/itpick/backend/common/response/status/BaseExceptionResponseStatus.java b/src/main/java/store/itpick/backend/common/response/status/BaseExceptionResponseStatus.java index 34d78da..1dc9031 100644 --- a/src/main/java/store/itpick/backend/common/response/status/BaseExceptionResponseStatus.java +++ b/src/main/java/store/itpick/backend/common/response/status/BaseExceptionResponseStatus.java @@ -65,6 +65,7 @@ public enum BaseExceptionResponseStatus implements ResponseStatus { COMMENT_PARENT_NOT_FOUND(6003,HttpStatus.BAD_REQUEST.value(), "부모 댓글이 존재하지 않습니다."), INVALID_COMMENT_HEART_VALUE(6004,HttpStatus.BAD_REQUEST.value(), "댓글 좋아요 생성 요청에서 잘못된 값이 존재합니다."), COMMENT_NOT_FOUND(6005,HttpStatus.BAD_REQUEST.value(), "해당 댓글이 존재하지 않습니다."), + KEYWORD_NOT_FOUND(6006,HttpStatus.BAD_REQUEST.value(), "해당 키워드가 존재하지 않습니다."), /** diff --git a/src/main/java/store/itpick/backend/dto/debate/PostDebateRequest.java b/src/main/java/store/itpick/backend/dto/debate/PostDebateRequest.java index 82172d5..8cf2c0a 100644 --- a/src/main/java/store/itpick/backend/dto/debate/PostDebateRequest.java +++ b/src/main/java/store/itpick/backend/dto/debate/PostDebateRequest.java @@ -1,6 +1,7 @@ package store.itpick.backend.dto.debate; import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -14,6 +15,9 @@ @Builder public class PostDebateRequest { + @NotNull(message = "keyword ID는 필수입니다.") + private Long keywordId; + @NotBlank(message = "title은 필수입니다.") private String title; diff --git a/src/main/java/store/itpick/backend/model/Debate.java b/src/main/java/store/itpick/backend/model/Debate.java index c2dae2b..3cb2857 100644 --- a/src/main/java/store/itpick/backend/model/Debate.java +++ b/src/main/java/store/itpick/backend/model/Debate.java @@ -46,4 +46,8 @@ public class Debate { @OneToMany(mappedBy = "debate") private List comment; + + @ManyToOne + @JoinColumn(name = "keyword_id", nullable = false) + private Keyword keyword; } diff --git a/src/main/java/store/itpick/backend/model/Keyword.java b/src/main/java/store/itpick/backend/model/Keyword.java index c7c0b66..8cb550f 100644 --- a/src/main/java/store/itpick/backend/model/Keyword.java +++ b/src/main/java/store/itpick/backend/model/Keyword.java @@ -49,6 +49,9 @@ public class Keyword { ) private List communityPeriods = new ArrayList<>(); + @OneToMany(mappedBy = "keyword") + private List debates; + @PrePersist protected void onCreate() { Timestamp now = Timestamp.from(Instant.now()); diff --git a/src/main/java/store/itpick/backend/service/DebateService.java b/src/main/java/store/itpick/backend/service/DebateService.java index e5b5905..cf4e40a 100644 --- a/src/main/java/store/itpick/backend/service/DebateService.java +++ b/src/main/java/store/itpick/backend/service/DebateService.java @@ -9,14 +9,8 @@ import store.itpick.backend.dto.debate.*; import store.itpick.backend.dto.vote.PostVoteRequest; import store.itpick.backend.jwt.JwtProvider; -import store.itpick.backend.model.Comment; -import store.itpick.backend.model.CommentHeart; -import store.itpick.backend.model.Debate; -import store.itpick.backend.model.User; -import store.itpick.backend.repository.CommentHeartRepository; -import store.itpick.backend.repository.CommentRepository; -import store.itpick.backend.repository.DebateRepository; -import store.itpick.backend.repository.UserRepository; +import store.itpick.backend.model.*; +import store.itpick.backend.repository.*; import java.sql.Timestamp; import java.time.LocalDateTime; @@ -30,6 +24,7 @@ @RequiredArgsConstructor public class DebateService { + private final KeywordRepository keywordRepository; private final DebateRepository debateRepository; private final CommentRepository commentRepository; private final CommentHeartRepository commentHeartRepository; @@ -38,7 +33,11 @@ public class DebateService { @Transactional public PostDebateResponse createDebate(PostDebateRequest postDebateRequest) { - Debate debate = Debate.builder().title(postDebateRequest.getTitle()).content(postDebateRequest.getContent()).hits(0L).onTrend(false).status("active").createAt(Timestamp.valueOf(LocalDateTime.now())).updateAt(Timestamp.valueOf(LocalDateTime.now())).build(); + + Keyword keyword = keywordRepository.findById(postDebateRequest.getKeywordId()) + .orElseThrow(() -> new DebateException(KEYWORD_NOT_FOUND)); + + Debate debate = Debate.builder().title(postDebateRequest.getTitle()).content(postDebateRequest.getContent()).hits(0L).onTrend(false).status("active").createAt(Timestamp.valueOf(LocalDateTime.now())).updateAt(Timestamp.valueOf(LocalDateTime.now())).keyword(keyword).build(); debate = debateRepository.save(debate);