diff --git a/backend/src/main/java/ddangkong/controller/content/BalanceContentController.java b/backend/src/main/java/ddangkong/controller/content/BalanceContentController.java new file mode 100644 index 000000000..495d6752f --- /dev/null +++ b/backend/src/main/java/ddangkong/controller/content/BalanceContentController.java @@ -0,0 +1,22 @@ +package ddangkong.controller.content; + +import ddangkong.controller.content.dto.BalanceContentResponse; +import ddangkong.service.content.BalanceContentService; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/api") +@RequiredArgsConstructor +public class BalanceContentController { + + private final BalanceContentService balanceContentService; + + @GetMapping("/balances/rooms/{roomId}/question") + public BalanceContentResponse getBalanceContent(@PathVariable Long roomId) { + return balanceContentService.findRecentBalanceContent(roomId); + } +} diff --git a/backend/src/main/java/ddangkong/controller/content/dto/BalanceContentResponse.java b/backend/src/main/java/ddangkong/controller/content/dto/BalanceContentResponse.java new file mode 100644 index 000000000..74c267707 --- /dev/null +++ b/backend/src/main/java/ddangkong/controller/content/dto/BalanceContentResponse.java @@ -0,0 +1,27 @@ +package ddangkong.controller.content.dto; + +import ddangkong.controller.option.dto.BalanceOptionResponse; +import ddangkong.domain.content.BalanceContent; +import ddangkong.domain.content.Category; +import ddangkong.domain.option.BalanceOption; +import lombok.Builder; + +public record BalanceContentResponse( + Long questionId, + Category category, + String title, + BalanceOptionResponse firstOption, + BalanceOptionResponse secondOption +) { + + @Builder + private BalanceContentResponse(BalanceContent balanceContent, + BalanceOption firstOption, + BalanceOption secondOption) { + this(balanceContent.getId(), + balanceContent.getCategory(), + balanceContent.getName(), + BalanceOptionResponse.from(firstOption), + BalanceOptionResponse.from(secondOption)); + } +} diff --git a/backend/src/main/java/ddangkong/controller/option/dto/BalanceOptionResponse.java b/backend/src/main/java/ddangkong/controller/option/dto/BalanceOptionResponse.java index 33e746ecf..665562cda 100644 --- a/backend/src/main/java/ddangkong/controller/option/dto/BalanceOptionResponse.java +++ b/backend/src/main/java/ddangkong/controller/option/dto/BalanceOptionResponse.java @@ -7,6 +7,6 @@ public record BalanceOptionResponse( String content ) { public static BalanceOptionResponse from(BalanceOption balanceOption) { - return new BalanceOptionResponse(balanceOption.getId(), balanceOption.getContent()); + return new BalanceOptionResponse(balanceOption.getId(), balanceOption.getName()); } } diff --git a/backend/src/main/java/ddangkong/controller/question/BalanceQuestionController.java b/backend/src/main/java/ddangkong/controller/question/BalanceQuestionController.java deleted file mode 100644 index 08897e6d8..000000000 --- a/backend/src/main/java/ddangkong/controller/question/BalanceQuestionController.java +++ /dev/null @@ -1,22 +0,0 @@ -package ddangkong.controller.question; - -import ddangkong.controller.question.dto.BalanceQuestionResponse; -import ddangkong.service.question.BalanceQuestionService; -import lombok.RequiredArgsConstructor; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -@RestController -@RequestMapping("/api") -@RequiredArgsConstructor -public class BalanceQuestionController { - - private final BalanceQuestionService balanceQuestionService; - - @GetMapping("/balances/rooms/{roomId}/question") - public BalanceQuestionResponse getBalanceQuestion(@PathVariable Long roomId) { - return balanceQuestionService.findRecentBalanceQuestion(roomId); - } -} diff --git a/backend/src/main/java/ddangkong/controller/question/dto/BalanceQuestionResponse.java b/backend/src/main/java/ddangkong/controller/question/dto/BalanceQuestionResponse.java deleted file mode 100644 index e021dab80..000000000 --- a/backend/src/main/java/ddangkong/controller/question/dto/BalanceQuestionResponse.java +++ /dev/null @@ -1,24 +0,0 @@ -package ddangkong.controller.question.dto; - -import ddangkong.controller.option.dto.BalanceOptionResponse; -import ddangkong.domain.option.BalanceOption; -import ddangkong.domain.question.BalanceQuestion; -import ddangkong.domain.question.Category; -import lombok.Builder; - -public record BalanceQuestionResponse( - Long questionId, - Category category, - String title, - BalanceOptionResponse firstOption, - BalanceOptionResponse secondOption -) { - @Builder - private BalanceQuestionResponse(BalanceQuestion question, BalanceOption firstOption, BalanceOption secondOption) { - this(question.getId(), - question.getCategory(), - question.getContent(), - BalanceOptionResponse.from(firstOption), - BalanceOptionResponse.from(secondOption)); - } -} diff --git a/backend/src/main/java/ddangkong/domain/question/BalanceQuestion.java b/backend/src/main/java/ddangkong/domain/content/BalanceContent.java similarity index 86% rename from backend/src/main/java/ddangkong/domain/question/BalanceQuestion.java rename to backend/src/main/java/ddangkong/domain/content/BalanceContent.java index 0b56e56dc..bb2238210 100644 --- a/backend/src/main/java/ddangkong/domain/question/BalanceQuestion.java +++ b/backend/src/main/java/ddangkong/domain/content/BalanceContent.java @@ -1,4 +1,4 @@ -package ddangkong.domain.question; +package ddangkong.domain.content; import jakarta.persistence.Column; import jakarta.persistence.Entity; @@ -14,7 +14,7 @@ @Entity @NoArgsConstructor(access = AccessLevel.PROTECTED) @Getter -public class BalanceQuestion { +public class BalanceContent { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -25,5 +25,5 @@ public class BalanceQuestion { private Category category; @Column(nullable = false) - private String content; + private String name; } diff --git a/backend/src/main/java/ddangkong/domain/question/Category.java b/backend/src/main/java/ddangkong/domain/content/Category.java similarity index 56% rename from backend/src/main/java/ddangkong/domain/question/Category.java rename to backend/src/main/java/ddangkong/domain/content/Category.java index fe67d4c5a..ba69a0a07 100644 --- a/backend/src/main/java/ddangkong/domain/question/Category.java +++ b/backend/src/main/java/ddangkong/domain/content/Category.java @@ -1,4 +1,4 @@ -package ddangkong.domain.question; +package ddangkong.domain.content; public enum Category { EXAMPLE, diff --git a/backend/src/main/java/ddangkong/domain/content/RoomContentRepository.java b/backend/src/main/java/ddangkong/domain/content/RoomContentRepository.java new file mode 100644 index 000000000..bd20d71cd --- /dev/null +++ b/backend/src/main/java/ddangkong/domain/content/RoomContentRepository.java @@ -0,0 +1,10 @@ +package ddangkong.domain.content; + +import ddangkong.domain.room.RoomContent; +import java.util.Optional; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface RoomContentRepository extends JpaRepository { + + Optional findTopByRoomIdOrderByCreatedAtDesc(Long roomId); +} diff --git a/backend/src/main/java/ddangkong/domain/option/BalanceOption.java b/backend/src/main/java/ddangkong/domain/option/BalanceOption.java index 5fcc62d27..894e22e6b 100644 --- a/backend/src/main/java/ddangkong/domain/option/BalanceOption.java +++ b/backend/src/main/java/ddangkong/domain/option/BalanceOption.java @@ -1,6 +1,6 @@ package ddangkong.domain.option; -import ddangkong.domain.question.BalanceQuestion; +import ddangkong.domain.content.BalanceContent; import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; @@ -23,9 +23,9 @@ public class BalanceOption { private Long id; @Column(nullable = false) - private String content; + private String name; @ManyToOne(optional = false, fetch = FetchType.LAZY) - @JoinColumn(name = "balance_question_id", nullable = false) - private BalanceQuestion balanceQuestion; + @JoinColumn(name = "balance_content_id", nullable = false) + private BalanceContent balanceContent; } diff --git a/backend/src/main/java/ddangkong/domain/option/BalanceOptionRepository.java b/backend/src/main/java/ddangkong/domain/option/BalanceOptionRepository.java index 7209a1a78..7635ab607 100644 --- a/backend/src/main/java/ddangkong/domain/option/BalanceOptionRepository.java +++ b/backend/src/main/java/ddangkong/domain/option/BalanceOptionRepository.java @@ -1,10 +1,10 @@ package ddangkong.domain.option; -import ddangkong.domain.question.BalanceQuestion; +import ddangkong.domain.content.BalanceContent; import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; public interface BalanceOptionRepository extends JpaRepository { - List findByBalanceQuestion(BalanceQuestion balanceQuestion); + List findByBalanceContent(BalanceContent balanceContent); } diff --git a/backend/src/main/java/ddangkong/domain/question/RoomQuestionRepository.java b/backend/src/main/java/ddangkong/domain/question/RoomQuestionRepository.java deleted file mode 100644 index 0006cf241..000000000 --- a/backend/src/main/java/ddangkong/domain/question/RoomQuestionRepository.java +++ /dev/null @@ -1,10 +0,0 @@ -package ddangkong.domain.question; - -import ddangkong.domain.room.RoomQuestion; -import java.util.Optional; -import org.springframework.data.jpa.repository.JpaRepository; - -public interface RoomQuestionRepository extends JpaRepository { - - Optional findTopByRoomIdOrderByCreatedAtDesc(Long roomId); -} diff --git a/backend/src/main/java/ddangkong/domain/room/RoomQuestion.java b/backend/src/main/java/ddangkong/domain/room/RoomContent.java similarity index 79% rename from backend/src/main/java/ddangkong/domain/room/RoomQuestion.java rename to backend/src/main/java/ddangkong/domain/room/RoomContent.java index e3283839d..b9508e77b 100644 --- a/backend/src/main/java/ddangkong/domain/room/RoomQuestion.java +++ b/backend/src/main/java/ddangkong/domain/room/RoomContent.java @@ -1,7 +1,7 @@ package ddangkong.domain.room; import ddangkong.domain.BaseEntity; -import ddangkong.domain.question.BalanceQuestion; +import ddangkong.domain.content.BalanceContent; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; import jakarta.persistence.GeneratedValue; @@ -16,7 +16,7 @@ @Entity @NoArgsConstructor(access = AccessLevel.PROTECTED) @Getter -public class RoomQuestion extends BaseEntity { +public class RoomContent extends BaseEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -27,6 +27,6 @@ public class RoomQuestion extends BaseEntity { private Room room; @ManyToOne(optional = false, fetch = FetchType.LAZY) - @JoinColumn(name = "balance_question_id") - private BalanceQuestion balanceQuestion; + @JoinColumn(name = "balance_content_id") + private BalanceContent balanceContent; } diff --git a/backend/src/main/java/ddangkong/service/question/BalanceQuestionService.java b/backend/src/main/java/ddangkong/service/content/BalanceContentService.java similarity index 60% rename from backend/src/main/java/ddangkong/service/question/BalanceQuestionService.java rename to backend/src/main/java/ddangkong/service/content/BalanceContentService.java index 7ac45964c..a87ae3a40 100644 --- a/backend/src/main/java/ddangkong/service/question/BalanceQuestionService.java +++ b/backend/src/main/java/ddangkong/service/content/BalanceContentService.java @@ -1,10 +1,10 @@ -package ddangkong.service.question; +package ddangkong.service.content; -import ddangkong.controller.question.dto.BalanceQuestionResponse; +import ddangkong.controller.content.dto.BalanceContentResponse; import ddangkong.domain.option.BalanceOption; import ddangkong.domain.option.BalanceOptionRepository; -import ddangkong.domain.question.BalanceQuestion; -import ddangkong.domain.question.RoomQuestionRepository; +import ddangkong.domain.content.BalanceContent; +import ddangkong.domain.content.RoomContentRepository; import ddangkong.service.excpetion.BusinessLogicException; import ddangkong.service.excpetion.ViolateDataException; import java.util.List; @@ -14,34 +14,34 @@ @Service @RequiredArgsConstructor -public class BalanceQuestionService { +public class BalanceContentService { private static final int BALANCE_OPTION_SIZE = 2; - private final RoomQuestionRepository roomQuestionRepository; + private final RoomContentRepository roomContentRepository; private final BalanceOptionRepository balanceOptionRepository; @Transactional(readOnly = true) - public BalanceQuestionResponse findRecentBalanceQuestion(Long roomId) { - BalanceQuestion balanceQuestion = findRecentQuestion(roomId); - List balanceOptions = findBalanceOptions(balanceQuestion); + public BalanceContentResponse findRecentBalanceContent(Long roomId) { + BalanceContent balanceContent = findRecentContent(roomId); + List balanceOptions = findBalanceOptions(balanceContent); - return BalanceQuestionResponse.builder() - .question(balanceQuestion) + return BalanceContentResponse.builder() + .balanceContent(balanceContent) .firstOption(balanceOptions.get(0)) .secondOption(balanceOptions.get(1)) .build(); } - private BalanceQuestion findRecentQuestion(Long roomId) { - return roomQuestionRepository.findTopByRoomIdOrderByCreatedAtDesc(roomId) + private BalanceContent findRecentContent(Long roomId) { + return roomContentRepository.findTopByRoomIdOrderByCreatedAtDesc(roomId) .orElseThrow(() -> new BusinessLogicException("해당 방의 질문이 존재하지 않습니다.")) - .getBalanceQuestion(); + .getBalanceContent(); } - private List findBalanceOptions(BalanceQuestion balanceQuestion) { - List balanceOptions = balanceOptionRepository.findByBalanceQuestion(balanceQuestion); + private List findBalanceOptions(BalanceContent balanceContent) { + List balanceOptions = balanceOptionRepository.findByBalanceContent(balanceContent); validateBalanceOptions(balanceOptions); return balanceOptions; } diff --git a/backend/src/test/java/ddangkong/controller/question/BalanceQuestionControllerTest.java b/backend/src/test/java/ddangkong/controller/content/BalanceContentControllerTest.java similarity index 63% rename from backend/src/test/java/ddangkong/controller/question/BalanceQuestionControllerTest.java rename to backend/src/test/java/ddangkong/controller/content/BalanceContentControllerTest.java index 96aa89c0f..99107635a 100644 --- a/backend/src/test/java/ddangkong/controller/question/BalanceQuestionControllerTest.java +++ b/backend/src/test/java/ddangkong/controller/content/BalanceContentControllerTest.java @@ -1,18 +1,18 @@ -package ddangkong.controller.question; +package ddangkong.controller.content; import static org.assertj.core.api.Assertions.assertThat; import ddangkong.controller.BaseControllerTest; import ddangkong.controller.option.dto.BalanceOptionResponse; -import ddangkong.controller.question.dto.BalanceQuestionResponse; -import ddangkong.domain.question.Category; +import ddangkong.controller.content.dto.BalanceContentResponse; +import ddangkong.domain.content.Category; import io.restassured.RestAssured; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; -class BalanceQuestionControllerTest extends BaseControllerTest { +class BalanceContentControllerTest extends BaseControllerTest { - private static final BalanceQuestionResponse EXPECTED_RESPONSE = new BalanceQuestionResponse( + private static final BalanceContentResponse EXPECTED_RESPONSE = new BalanceContentResponse( 1L, Category.EXAMPLE, "민초 vs 반민초", new BalanceOptionResponse(1L, "민초"), new BalanceOptionResponse(2L, "반민초")); @@ -22,11 +22,11 @@ class 방의_질문_조회 { @Test void 현재_방의_질문을_조회할_수_있다() { - BalanceQuestionResponse actual = RestAssured.given().log().all() + BalanceContentResponse actual = RestAssured.given().log().all() .when().get("/api/balances/rooms/1/question") .then().log().all() .statusCode(200) - .extract().as(BalanceQuestionResponse.class); + .extract().as(BalanceContentResponse.class); assertThat(actual).isEqualTo(EXPECTED_RESPONSE); } diff --git a/backend/src/test/java/ddangkong/domain/question/RoomQuestionRepositoryTest.java b/backend/src/test/java/ddangkong/domain/content/RoomContentRepositoryTest.java similarity index 68% rename from backend/src/test/java/ddangkong/domain/question/RoomQuestionRepositoryTest.java rename to backend/src/test/java/ddangkong/domain/content/RoomContentRepositoryTest.java index bd15ed56f..caffa0ddd 100644 --- a/backend/src/test/java/ddangkong/domain/question/RoomQuestionRepositoryTest.java +++ b/backend/src/test/java/ddangkong/domain/content/RoomContentRepositoryTest.java @@ -1,9 +1,9 @@ -package ddangkong.domain.question; +package ddangkong.domain.content; import static org.assertj.core.api.Assertions.assertThat; import ddangkong.domain.BaseRepositoryTest; -import ddangkong.domain.room.RoomQuestion; +import ddangkong.domain.room.RoomContent; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -11,14 +11,14 @@ class RoomQuestionRepositoryTest extends BaseRepositoryTest { @Autowired - private RoomQuestionRepository roomQuestionRepository; + private RoomContentRepository roomContentRepository; @Nested class 방의_최신_질문_조회 { @Test void 방의_가장_최신의_질문을_조회할_수_있다() { - RoomQuestion actual = roomQuestionRepository.findTopByRoomIdOrderByCreatedAtDesc(1L).get(); + RoomContent actual = roomContentRepository.findTopByRoomIdOrderByCreatedAtDesc(1L).get(); assertThat(actual.getId()).isEqualTo(2L); } diff --git a/backend/src/test/java/ddangkong/service/question/BalanceQuestionServiceTest.java b/backend/src/test/java/ddangkong/service/content/BalanceContentServiceTest.java similarity index 56% rename from backend/src/test/java/ddangkong/service/question/BalanceQuestionServiceTest.java rename to backend/src/test/java/ddangkong/service/content/BalanceContentServiceTest.java index 3b30e2305..f398f3fd8 100644 --- a/backend/src/test/java/ddangkong/service/question/BalanceQuestionServiceTest.java +++ b/backend/src/test/java/ddangkong/service/content/BalanceContentServiceTest.java @@ -1,42 +1,42 @@ -package ddangkong.service.question; +package ddangkong.service.content; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import ddangkong.controller.option.dto.BalanceOptionResponse; -import ddangkong.controller.question.dto.BalanceQuestionResponse; -import ddangkong.domain.question.Category; +import ddangkong.controller.content.dto.BalanceContentResponse; +import ddangkong.domain.content.Category; import ddangkong.service.BaseServiceTest; import ddangkong.service.excpetion.BusinessLogicException; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -class BalanceQuestionServiceTest extends BaseServiceTest { +class BalanceContentServiceTest extends BaseServiceTest { private static final Long PROGRESS_ROOM_ID = 1L; private static final Long NOT_EXIST_ROOM_ID = 2L; - private static final BalanceQuestionResponse BALANCE_QUESTION_RESPONSE = new BalanceQuestionResponse( + private static final BalanceContentResponse BALANCE_CONTENT_RESPONSE = new BalanceContentResponse( 1L, Category.EXAMPLE, "민초 vs 반민초", new BalanceOptionResponse(1L, "민초"), new BalanceOptionResponse(2L, "반민초")); @Autowired - private BalanceQuestionService balanceQuestionService; + private BalanceContentService balanceContentService; @Nested - class 방의_최신_질문_조회 { + class 방의_최신_내용_조회 { @Test - void 방의_최신_질문을_조회할_수_있다() { - BalanceQuestionResponse actual = balanceQuestionService.findRecentBalanceQuestion(PROGRESS_ROOM_ID); + void 방의_최신_내용을_조회할_수_있다() { + BalanceContentResponse actual = balanceContentService.findRecentBalanceContent(PROGRESS_ROOM_ID); - assertThat(actual).isEqualTo(BALANCE_QUESTION_RESPONSE); + assertThat(actual).isEqualTo(BALANCE_CONTENT_RESPONSE); } @Test void 방이_없을_경우_예외를_던진다() { - assertThatThrownBy(() -> balanceQuestionService.findRecentBalanceQuestion(NOT_EXIST_ROOM_ID)) + assertThatThrownBy(() -> balanceContentService.findRecentBalanceContent(NOT_EXIST_ROOM_ID)) .isInstanceOf(BusinessLogicException.class) .hasMessage("해당 방의 질문이 존재하지 않습니다."); } diff --git a/backend/src/test/resources/init-test.sql b/backend/src/test/resources/init-test.sql index 71dbf94a8..90b21797b 100644 --- a/backend/src/test/resources/init-test.sql +++ b/backend/src/test/resources/init-test.sql @@ -2,30 +2,30 @@ SET REFERENTIAL_INTEGRITY FALSE; TRUNCATE TABLE member; TRUNCATE TABLE balance_option; -TRUNCATE TABLE balance_question; +TRUNCATE TABLE balance_content; TRUNCATE TABLE balance_vote; TRUNCATE TABLE room; -TRUNCATE TABLE room_question; +TRUNCATE TABLE room_content; ALTER TABLE member ALTER COLUMN ID RESTART WITH 1; ALTER TABLE balance_option ALTER COLUMN ID RESTART WITH 1; -ALTER TABLE balance_question ALTER COLUMN ID RESTART WITH 1; +ALTER TABLE balance_content ALTER COLUMN ID RESTART WITH 1; ALTER TABLE balance_vote ALTER COLUMN ID RESTART WITH 1; ALTER TABLE room ALTER COLUMN ID RESTART WITH 1; -ALTER TABLE room_question ALTER COLUMN ID RESTART WITH 1; +ALTER TABLE room_content ALTER COLUMN ID RESTART WITH 1; INSERT INTO room() VALUES (); INSERT INTO member(nickname, room_id) -VALUES ('mohamedeu al katan', 1), ('deundeun ', 1), ('rupi', 1), ('rapper lee', 1); +VALUES ('mohamedeu al katan', 1), ('deundeun', 1), ('rupi', 1), ('rapper lee', 1); -INSERT INTO room_question(room_id, balance_question_id, created_at) +INSERT INTO room_content(room_id, balance_content_id, created_at) VALUES (1, 2, '2024-07-18 19:50:00.000'), (1, 1, '2024-07-18 20:00:00.000'); -INSERT INTO balance_question(category, content) +INSERT INTO balance_content(category, name) VALUES ('EXAMPLE', '민초 vs 반민초'), ('EXAMPLE', '월 200 백수 vs 월 500 직장인'); -INSERT INTO balance_option(content, balance_question_id) +INSERT INTO balance_option(name, balance_content_id) VALUES ('민초', 1), ('반민초', 1), ('월 200 백수', 2), ('월 200 직장인', 2); INSERT INTO balance_vote(balance_option_id, member_id)