From 5ddabf73228b2291453d4fbb020bb44f407a3db3 Mon Sep 17 00:00:00 2001 From: "DESKTOP-E5VRH4Q\\cnddk" Date: Tue, 23 Jul 2024 18:02:58 +0900 Subject: [PATCH 1/8] =?UTF-8?q?refactor:=20=EC=98=88=EC=99=B8=20=EC=B2=98?= =?UTF-8?q?=EB=A6=AC=20=ED=9A=8C=EC=9D=98=20=EA=B2=B0=EA=B3=BC=EC=97=90=20?= =?UTF-8?q?=EB=94=B0=EB=9D=BC=20exception=20=ED=8C=A8=ED=82=A4=EC=A7=80=20?= =?UTF-8?q?=EB=B6=84=EB=A6=AC=20=EB=B0=8F=20=EC=9D=B4=EB=A6=84=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 --- .../controller/exception/GlobalExceptionHandler.java | 8 ++++---- .../java/ddangkong/exception/BadRequestException.java | 8 ++++++++ .../ddangkong/exception/InternalServerErrorException.java | 8 ++++++++ .../service/balance/content/BalanceContentService.java | 8 ++++---- .../service/excpetion/BusinessLogicException.java | 8 -------- .../ddangkong/service/excpetion/ViolateDataException.java | 8 -------- .../balance/content/BalanceContentServiceTest.java | 4 ++-- 7 files changed, 26 insertions(+), 26 deletions(-) create mode 100644 backend/src/main/java/ddangkong/exception/BadRequestException.java create mode 100644 backend/src/main/java/ddangkong/exception/InternalServerErrorException.java delete mode 100644 backend/src/main/java/ddangkong/service/excpetion/BusinessLogicException.java delete mode 100644 backend/src/main/java/ddangkong/service/excpetion/ViolateDataException.java diff --git a/backend/src/main/java/ddangkong/controller/exception/GlobalExceptionHandler.java b/backend/src/main/java/ddangkong/controller/exception/GlobalExceptionHandler.java index 1814c07e4..8522ea8c1 100644 --- a/backend/src/main/java/ddangkong/controller/exception/GlobalExceptionHandler.java +++ b/backend/src/main/java/ddangkong/controller/exception/GlobalExceptionHandler.java @@ -1,7 +1,7 @@ package ddangkong.controller.exception; -import ddangkong.service.excpetion.BusinessLogicException; -import ddangkong.service.excpetion.ViolateDataException; +import ddangkong.exception.BadRequestException; +import ddangkong.exception.InternalServerErrorException; import jakarta.validation.ConstraintViolationException; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; @@ -34,7 +34,7 @@ public ErrorResponse handleConstraintViolationException(ConstraintViolationExcep @ExceptionHandler @ResponseStatus(HttpStatus.BAD_REQUEST) - public ErrorResponse handleBusinessLogicException(BusinessLogicException e) { + public ErrorResponse handleBusinessLogicException(BadRequestException e) { log.warn(e.getMessage()); return new ErrorResponse(e.getMessage()); @@ -42,7 +42,7 @@ public ErrorResponse handleBusinessLogicException(BusinessLogicException e) { @ExceptionHandler @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) - public ErrorResponse handleViolateDataException(ViolateDataException e) { + public ErrorResponse handleViolateDataException(InternalServerErrorException e) { log.error(e.getMessage(), e); return new ErrorResponse(SERVER_ERROR_MESSAGE); diff --git a/backend/src/main/java/ddangkong/exception/BadRequestException.java b/backend/src/main/java/ddangkong/exception/BadRequestException.java new file mode 100644 index 000000000..39849d34f --- /dev/null +++ b/backend/src/main/java/ddangkong/exception/BadRequestException.java @@ -0,0 +1,8 @@ +package ddangkong.exception; + +public class BadRequestException extends RuntimeException { + + public BadRequestException(String message) { + super(message); + } +} diff --git a/backend/src/main/java/ddangkong/exception/InternalServerErrorException.java b/backend/src/main/java/ddangkong/exception/InternalServerErrorException.java new file mode 100644 index 000000000..bfe12bc14 --- /dev/null +++ b/backend/src/main/java/ddangkong/exception/InternalServerErrorException.java @@ -0,0 +1,8 @@ +package ddangkong.exception; + +public class InternalServerErrorException extends RuntimeException { + + public InternalServerErrorException(String message) { + super(message); + } +} diff --git a/backend/src/main/java/ddangkong/service/balance/content/BalanceContentService.java b/backend/src/main/java/ddangkong/service/balance/content/BalanceContentService.java index a3a4b6114..f5f26e6b3 100644 --- a/backend/src/main/java/ddangkong/service/balance/content/BalanceContentService.java +++ b/backend/src/main/java/ddangkong/service/balance/content/BalanceContentService.java @@ -5,8 +5,8 @@ import ddangkong.domain.balance.content.RoomContentRepository; import ddangkong.domain.balance.option.BalanceOption; import ddangkong.domain.balance.option.BalanceOptionRepository; -import ddangkong.service.excpetion.BusinessLogicException; -import ddangkong.service.excpetion.ViolateDataException; +import ddangkong.exception.BadRequestException; +import ddangkong.exception.InternalServerErrorException; import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -36,7 +36,7 @@ public BalanceContentResponse findRecentBalanceContent(Long roomId) { private BalanceContent findRecentContent(Long roomId) { return roomContentRepository.findTopByRoomIdOrderByCreatedAtDesc(roomId) - .orElseThrow(() -> new BusinessLogicException("해당 방의 질문이 존재하지 않습니다.")) + .orElseThrow(() -> new BadRequestException("해당 방의 질문이 존재하지 않습니다.")) .getBalanceContent(); } @@ -48,7 +48,7 @@ private List findBalanceOptions(BalanceContent balanceContent) { private void validateBalanceOptions(List balanceOptions) { if (balanceOptions.size() != BALANCE_OPTION_SIZE) { - throw new ViolateDataException("밸런스 게임의 선택지가 %d개입니다".formatted(balanceOptions.size())); + throw new InternalServerErrorException("밸런스 게임의 선택지가 %d개입니다".formatted(balanceOptions.size())); } } } diff --git a/backend/src/main/java/ddangkong/service/excpetion/BusinessLogicException.java b/backend/src/main/java/ddangkong/service/excpetion/BusinessLogicException.java deleted file mode 100644 index d2b522cb4..000000000 --- a/backend/src/main/java/ddangkong/service/excpetion/BusinessLogicException.java +++ /dev/null @@ -1,8 +0,0 @@ -package ddangkong.service.excpetion; - -public class BusinessLogicException extends RuntimeException { - - public BusinessLogicException(String message) { - super(message); - } -} diff --git a/backend/src/main/java/ddangkong/service/excpetion/ViolateDataException.java b/backend/src/main/java/ddangkong/service/excpetion/ViolateDataException.java deleted file mode 100644 index 48192f9b1..000000000 --- a/backend/src/main/java/ddangkong/service/excpetion/ViolateDataException.java +++ /dev/null @@ -1,8 +0,0 @@ -package ddangkong.service.excpetion; - -public class ViolateDataException extends RuntimeException { - - public ViolateDataException(String message) { - super(message); - } -} diff --git a/backend/src/test/java/ddangkong/service/balance/content/BalanceContentServiceTest.java b/backend/src/test/java/ddangkong/service/balance/content/BalanceContentServiceTest.java index fc87fda26..a6903306a 100644 --- a/backend/src/test/java/ddangkong/service/balance/content/BalanceContentServiceTest.java +++ b/backend/src/test/java/ddangkong/service/balance/content/BalanceContentServiceTest.java @@ -7,7 +7,7 @@ import ddangkong.controller.balance.option.dto.BalanceOptionResponse; import ddangkong.domain.balance.content.Category; import ddangkong.service.BaseServiceTest; -import ddangkong.service.excpetion.BusinessLogicException; +import ddangkong.exception.BadRequestException; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -40,7 +40,7 @@ class 현재_방의_밸런스_게임_내용_조회 { void 방이_없을_경우_예외를_던진다() { // when & then assertThatThrownBy(() -> balanceContentService.findRecentBalanceContent(NOT_EXIST_ROOM_ID)) - .isInstanceOf(BusinessLogicException.class) + .isInstanceOf(BadRequestException.class) .hasMessage("해당 방의 질문이 존재하지 않습니다."); } } From f876f7869da57e35df238c50808d4f7c6a48c726 Mon Sep 17 00:00:00 2001 From: "DESKTOP-E5VRH4Q\\cnddk" Date: Tue, 23 Jul 2024 18:10:02 +0900 Subject: [PATCH 2/8] =?UTF-8?q?feat:=20=EB=B3=80=EA=B2=BD=EB=90=9C=20ERD?= =?UTF-8?q?=20(v1.1)=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/ddangkong/domain/balance/content/Room.java | 9 +++++++++ .../ddangkong/domain/balance/content/RoomContent.java | 4 ++++ backend/src/test/resources/init-test.sql | 10 +++++----- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/backend/src/main/java/ddangkong/domain/balance/content/Room.java b/backend/src/main/java/ddangkong/domain/balance/content/Room.java index 7dd029e80..e0a0e1a4c 100644 --- a/backend/src/main/java/ddangkong/domain/balance/content/Room.java +++ b/backend/src/main/java/ddangkong/domain/balance/content/Room.java @@ -1,5 +1,6 @@ package ddangkong.domain.balance.content; +import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; @@ -13,7 +14,15 @@ @Getter public class Room { + private static final int DEFAULT_TOTAL_ROUND = 5; + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; + + @Column(nullable = false) + private int currentRound; + + @Column(nullable = false) + private int totalRound = DEFAULT_TOTAL_ROUND; } diff --git a/backend/src/main/java/ddangkong/domain/balance/content/RoomContent.java b/backend/src/main/java/ddangkong/domain/balance/content/RoomContent.java index 3f99b1f84..f3f39364e 100644 --- a/backend/src/main/java/ddangkong/domain/balance/content/RoomContent.java +++ b/backend/src/main/java/ddangkong/domain/balance/content/RoomContent.java @@ -1,6 +1,7 @@ package ddangkong.domain.balance.content; import ddangkong.domain.BaseEntity; +import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; import jakarta.persistence.GeneratedValue; @@ -28,4 +29,7 @@ public class RoomContent extends BaseEntity { @ManyToOne(optional = false, fetch = FetchType.LAZY) @JoinColumn(name = "balance_content_id") private BalanceContent balanceContent; + + @Column(nullable = false) + private int round; } diff --git a/backend/src/test/resources/init-test.sql b/backend/src/test/resources/init-test.sql index a583b8f4b..a9da177f2 100644 --- a/backend/src/test/resources/init-test.sql +++ b/backend/src/test/resources/init-test.sql @@ -1,5 +1,5 @@ -INSERT INTO room () -VALUES (); +INSERT INTO room (current_round, total_round) +VALUES (2, 5); INSERT INTO member (nickname, room_id) VALUES ('mohamedeu al katan', 1), @@ -11,9 +11,9 @@ INSERT INTO balance_content (category, name) VALUES ('EXAMPLE', '민초 vs 반민초'), ('EXAMPLE', '월 200 백수 vs 월 500 직장인'); -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 room_content (room_id, balance_content_id, round, created_at) +VALUES (1, 2, 1, '2024-07-18 19:50:00.000'), + (1, 1, 2, '2024-07-18 20:00:00.000'); INSERT INTO balance_option (name, balance_content_id) VALUES ('민초', 1), From e2963a6d40e1174cfe9bfd17733f5f71c757e109 Mon Sep 17 00:00:00 2001 From: "DESKTOP-E5VRH4Q\\cnddk" Date: Tue, 23 Jul 2024 18:52:34 +0900 Subject: [PATCH 3/8] =?UTF-8?q?feat:=20=EB=B3=80=EA=B2=BD=EB=90=9C=20'?= =?UTF-8?q?=EC=A3=BC=EC=A0=9C,=20=EC=84=A0=ED=83=9D=EC=A7=80=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C'=20API=20=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../content/dto/BalanceContentResponse.java | 14 +++++++++----- .../domain/balance/content/RoomContent.java | 16 ++++++++++++++++ .../balance/content/BalanceContentService.java | 12 ++++++------ .../content/BalanceContentControllerTest.java | 2 +- .../content/BalanceContentServiceTest.java | 4 ++-- 5 files changed, 34 insertions(+), 14 deletions(-) diff --git a/backend/src/main/java/ddangkong/controller/balance/content/dto/BalanceContentResponse.java b/backend/src/main/java/ddangkong/controller/balance/content/dto/BalanceContentResponse.java index 66543da91..9f56359b7 100644 --- a/backend/src/main/java/ddangkong/controller/balance/content/dto/BalanceContentResponse.java +++ b/backend/src/main/java/ddangkong/controller/balance/content/dto/BalanceContentResponse.java @@ -1,26 +1,30 @@ package ddangkong.controller.balance.content.dto; import ddangkong.controller.balance.option.dto.BalanceOptionResponse; -import ddangkong.domain.balance.content.BalanceContent; import ddangkong.domain.balance.content.Category; +import ddangkong.domain.balance.content.RoomContent; import ddangkong.domain.balance.option.BalanceOption; import lombok.Builder; public record BalanceContentResponse( Long contentId, Category category, + int totalRound, + int currentRound, String question, BalanceOptionResponse firstOption, BalanceOptionResponse secondOption ) { @Builder - private BalanceContentResponse(BalanceContent balanceContent, + private BalanceContentResponse(RoomContent roomContent, BalanceOption firstOption, BalanceOption secondOption) { - this(balanceContent.getId(), - balanceContent.getCategory(), - balanceContent.getName(), + this(roomContent.getContentId(), + roomContent.getContentCategory(), + roomContent.getTotalRound(), + roomContent.getRound(), + roomContent.getContentName(), BalanceOptionResponse.from(firstOption), BalanceOptionResponse.from(secondOption)); } diff --git a/backend/src/main/java/ddangkong/domain/balance/content/RoomContent.java b/backend/src/main/java/ddangkong/domain/balance/content/RoomContent.java index f3f39364e..dc04a7606 100644 --- a/backend/src/main/java/ddangkong/domain/balance/content/RoomContent.java +++ b/backend/src/main/java/ddangkong/domain/balance/content/RoomContent.java @@ -32,4 +32,20 @@ public class RoomContent extends BaseEntity { @Column(nullable = false) private int round; + + public Long getContentId() { + return balanceContent.getId(); + } + + public Category getContentCategory() { + return balanceContent.getCategory(); + } + + public String getContentName() { + return balanceContent.getName(); + } + + public int getTotalRound() { + return room.getTotalRound(); + } } diff --git a/backend/src/main/java/ddangkong/service/balance/content/BalanceContentService.java b/backend/src/main/java/ddangkong/service/balance/content/BalanceContentService.java index f5f26e6b3..7630c0f9c 100644 --- a/backend/src/main/java/ddangkong/service/balance/content/BalanceContentService.java +++ b/backend/src/main/java/ddangkong/service/balance/content/BalanceContentService.java @@ -2,6 +2,7 @@ import ddangkong.controller.balance.content.dto.BalanceContentResponse; import ddangkong.domain.balance.content.BalanceContent; +import ddangkong.domain.balance.content.RoomContent; import ddangkong.domain.balance.content.RoomContentRepository; import ddangkong.domain.balance.option.BalanceOption; import ddangkong.domain.balance.option.BalanceOptionRepository; @@ -24,20 +25,19 @@ public class BalanceContentService { @Transactional(readOnly = true) public BalanceContentResponse findRecentBalanceContent(Long roomId) { - BalanceContent balanceContent = findRecentContent(roomId); - List balanceOptions = findBalanceOptions(balanceContent); + RoomContent roomContent = findRecentRoomContent(roomId); + List balanceOptions = findBalanceOptions(roomContent.getBalanceContent()); return BalanceContentResponse.builder() - .balanceContent(balanceContent) + .roomContent(roomContent) .firstOption(balanceOptions.get(0)) .secondOption(balanceOptions.get(1)) .build(); } - private BalanceContent findRecentContent(Long roomId) { + private RoomContent findRecentRoomContent(Long roomId) { return roomContentRepository.findTopByRoomIdOrderByCreatedAtDesc(roomId) - .orElseThrow(() -> new BadRequestException("해당 방의 질문이 존재하지 않습니다.")) - .getBalanceContent(); + .orElseThrow(() -> new BadRequestException("해당 방의 질문이 존재하지 않습니다.")); } private List findBalanceOptions(BalanceContent balanceContent) { diff --git a/backend/src/test/java/ddangkong/controller/balance/content/BalanceContentControllerTest.java b/backend/src/test/java/ddangkong/controller/balance/content/BalanceContentControllerTest.java index 67d24b7d6..722b8d86f 100644 --- a/backend/src/test/java/ddangkong/controller/balance/content/BalanceContentControllerTest.java +++ b/backend/src/test/java/ddangkong/controller/balance/content/BalanceContentControllerTest.java @@ -16,7 +16,7 @@ class BalanceContentControllerTest extends BaseControllerTest { class 현재_방의_내용_조회 { private static final BalanceContentResponse EXPECTED_RESPONSE = new BalanceContentResponse( - 1L, Category.EXAMPLE, "민초 vs 반민초", + 1L, Category.EXAMPLE, 5, 2, "민초 vs 반민초", new BalanceOptionResponse(1L, "민초"), new BalanceOptionResponse(2L, "반민초")); diff --git a/backend/src/test/java/ddangkong/service/balance/content/BalanceContentServiceTest.java b/backend/src/test/java/ddangkong/service/balance/content/BalanceContentServiceTest.java index a6903306a..c79234c55 100644 --- a/backend/src/test/java/ddangkong/service/balance/content/BalanceContentServiceTest.java +++ b/backend/src/test/java/ddangkong/service/balance/content/BalanceContentServiceTest.java @@ -6,8 +6,8 @@ import ddangkong.controller.balance.content.dto.BalanceContentResponse; import ddangkong.controller.balance.option.dto.BalanceOptionResponse; import ddangkong.domain.balance.content.Category; -import ddangkong.service.BaseServiceTest; import ddangkong.exception.BadRequestException; +import ddangkong.service.BaseServiceTest; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -17,7 +17,7 @@ class BalanceContentServiceTest extends BaseServiceTest { private static final Long PROGRESS_ROOM_ID = 1L; private static final Long NOT_EXIST_ROOM_ID = 2L; private static final BalanceContentResponse BALANCE_CONTENT_RESPONSE = new BalanceContentResponse( - 1L, Category.EXAMPLE, "민초 vs 반민초", + 1L, Category.EXAMPLE, 5, 2, "민초 vs 반민초", new BalanceOptionResponse(1L, "민초"), new BalanceOptionResponse(2L, "반민초")); From 2b575f3bffea931efb6f7f05f0401c99f68f4a67 Mon Sep 17 00:00:00 2001 From: "DESKTOP-E5VRH4Q\\cnddk" Date: Tue, 23 Jul 2024 18:59:26 +0900 Subject: [PATCH 4/8] =?UTF-8?q?style:=20=EB=B0=94=EB=80=90=20Exception=20?= =?UTF-8?q?=EC=9D=B4=EB=A6=84=EC=97=90=20=EB=94=B0=EB=9D=BC,=20GlobalExcep?= =?UTF-8?q?tionHandler=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EB=AA=85=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/exception/GlobalExceptionHandler.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/src/main/java/ddangkong/controller/exception/GlobalExceptionHandler.java b/backend/src/main/java/ddangkong/controller/exception/GlobalExceptionHandler.java index 8522ea8c1..b7b004835 100644 --- a/backend/src/main/java/ddangkong/controller/exception/GlobalExceptionHandler.java +++ b/backend/src/main/java/ddangkong/controller/exception/GlobalExceptionHandler.java @@ -34,7 +34,7 @@ public ErrorResponse handleConstraintViolationException(ConstraintViolationExcep @ExceptionHandler @ResponseStatus(HttpStatus.BAD_REQUEST) - public ErrorResponse handleBusinessLogicException(BadRequestException e) { + public ErrorResponse handleBadRequestException(BadRequestException e) { log.warn(e.getMessage()); return new ErrorResponse(e.getMessage()); @@ -42,7 +42,7 @@ public ErrorResponse handleBusinessLogicException(BadRequestException e) { @ExceptionHandler @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) - public ErrorResponse handleViolateDataException(InternalServerErrorException e) { + public ErrorResponse handleInternalServerErrorException(InternalServerErrorException e) { log.error(e.getMessage(), e); return new ErrorResponse(SERVER_ERROR_MESSAGE); From 4b6ac2a5f4515bd82590fcea863e8055943fc43d Mon Sep 17 00:00:00 2001 From: "DESKTOP-E5VRH4Q\\cnddk" Date: Tue, 23 Jul 2024 19:07:29 +0900 Subject: [PATCH 5/8] =?UTF-8?q?style:=20total=5Fround=EA=B0=80=20current?= =?UTF-8?q?=5Fround=20=EB=B3=B4=EB=8B=A4=20=EC=95=9E=EC=97=90=20=EC=9E=88?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95=20#52?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/ddangkong/domain/balance/content/Room.java | 4 ++-- backend/src/test/resources/init-test.sql | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/backend/src/main/java/ddangkong/domain/balance/content/Room.java b/backend/src/main/java/ddangkong/domain/balance/content/Room.java index e0a0e1a4c..5175c79c3 100644 --- a/backend/src/main/java/ddangkong/domain/balance/content/Room.java +++ b/backend/src/main/java/ddangkong/domain/balance/content/Room.java @@ -21,8 +21,8 @@ public class Room { private Long id; @Column(nullable = false) - private int currentRound; + private int totalRound = DEFAULT_TOTAL_ROUND; @Column(nullable = false) - private int totalRound = DEFAULT_TOTAL_ROUND; + private int currentRound; } diff --git a/backend/src/test/resources/init-test.sql b/backend/src/test/resources/init-test.sql index a9da177f2..1c62304cf 100644 --- a/backend/src/test/resources/init-test.sql +++ b/backend/src/test/resources/init-test.sql @@ -1,5 +1,5 @@ -INSERT INTO room (current_round, total_round) -VALUES (2, 5); +INSERT INTO room (total_round, current_round) +VALUES (5, 2); INSERT INTO member (nickname, room_id) VALUES ('mohamedeu al katan', 1), From a58038389fb03ccb20a189f955872449da5923ac Mon Sep 17 00:00:00 2001 From: "DESKTOP-E5VRH4Q\\cnddk" Date: Wed, 24 Jul 2024 12:59:42 +0900 Subject: [PATCH 6/8] =?UTF-8?q?feat:=20=EB=B3=80=EA=B2=BD=EB=90=9C=20?= =?UTF-8?q?=EC=A0=95=EC=B1=85=EC=97=90=20=EB=94=B0=EB=9D=BC,=20=EB=B0=B8?= =?UTF-8?q?=EB=9F=B0=EC=8A=A4=20=EA=B2=8C=EC=9E=84=EC=9D=98=20=ED=98=84?= =?UTF-8?q?=EC=9E=AC=20=EB=82=B4=EC=9A=A9=20=EC=A1=B0=ED=9A=8C=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20=EC=88=98=EC=A0=95=20#52?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../content/RoomContentRepository.java | 2 +- .../balance/content/RoomRepository.java | 6 ++++ .../content/BalanceContentService.java | 14 +++++++--- .../content/RoomContentRepositoryTest.java | 11 ++++++-- .../content/BalanceContentServiceTest.java | 28 ++++++++++++------- backend/src/test/resources/init-test.sql | 2 +- 6 files changed, 44 insertions(+), 19 deletions(-) create mode 100644 backend/src/main/java/ddangkong/domain/balance/content/RoomRepository.java diff --git a/backend/src/main/java/ddangkong/domain/balance/content/RoomContentRepository.java b/backend/src/main/java/ddangkong/domain/balance/content/RoomContentRepository.java index ea9f4ce27..d27a04dda 100644 --- a/backend/src/main/java/ddangkong/domain/balance/content/RoomContentRepository.java +++ b/backend/src/main/java/ddangkong/domain/balance/content/RoomContentRepository.java @@ -5,5 +5,5 @@ public interface RoomContentRepository extends JpaRepository { - Optional findTopByRoomIdOrderByCreatedAtDesc(Long roomId); + Optional findByRoomAndRound(Room room, int round); } diff --git a/backend/src/main/java/ddangkong/domain/balance/content/RoomRepository.java b/backend/src/main/java/ddangkong/domain/balance/content/RoomRepository.java new file mode 100644 index 000000000..e9b0759ec --- /dev/null +++ b/backend/src/main/java/ddangkong/domain/balance/content/RoomRepository.java @@ -0,0 +1,6 @@ +package ddangkong.domain.balance.content; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface RoomRepository extends JpaRepository { +} diff --git a/backend/src/main/java/ddangkong/service/balance/content/BalanceContentService.java b/backend/src/main/java/ddangkong/service/balance/content/BalanceContentService.java index 7630c0f9c..9a4888740 100644 --- a/backend/src/main/java/ddangkong/service/balance/content/BalanceContentService.java +++ b/backend/src/main/java/ddangkong/service/balance/content/BalanceContentService.java @@ -2,8 +2,10 @@ import ddangkong.controller.balance.content.dto.BalanceContentResponse; import ddangkong.domain.balance.content.BalanceContent; +import ddangkong.domain.balance.content.Room; import ddangkong.domain.balance.content.RoomContent; import ddangkong.domain.balance.content.RoomContentRepository; +import ddangkong.domain.balance.content.RoomRepository; import ddangkong.domain.balance.option.BalanceOption; import ddangkong.domain.balance.option.BalanceOptionRepository; import ddangkong.exception.BadRequestException; @@ -19,13 +21,15 @@ public class BalanceContentService { private static final int BALANCE_OPTION_SIZE = 2; + private final RoomRepository roomRepository; + private final RoomContentRepository roomContentRepository; private final BalanceOptionRepository balanceOptionRepository; @Transactional(readOnly = true) public BalanceContentResponse findRecentBalanceContent(Long roomId) { - RoomContent roomContent = findRecentRoomContent(roomId); + RoomContent roomContent = findCurrentRoomContent(roomId); List balanceOptions = findBalanceOptions(roomContent.getBalanceContent()); return BalanceContentResponse.builder() @@ -35,9 +39,11 @@ public BalanceContentResponse findRecentBalanceContent(Long roomId) { .build(); } - private RoomContent findRecentRoomContent(Long roomId) { - return roomContentRepository.findTopByRoomIdOrderByCreatedAtDesc(roomId) - .orElseThrow(() -> new BadRequestException("해당 방의 질문이 존재하지 않습니다.")); + private RoomContent findCurrentRoomContent(Long roomId) { + Room room = roomRepository.findById(roomId) + .orElseThrow(() -> new BadRequestException("해당 방이 존재하지 않습니다.")); + return roomContentRepository.findByRoomAndRound(room, room.getCurrentRound()) + .orElseThrow(() -> new BadRequestException("해당 방의 현재 진행중인 질문이 존재하지 않습니다.")); } private List findBalanceOptions(BalanceContent balanceContent) { diff --git a/backend/src/test/java/ddangkong/domain/balance/content/RoomContentRepositoryTest.java b/backend/src/test/java/ddangkong/domain/balance/content/RoomContentRepositoryTest.java index 91d270ebf..067644f0d 100644 --- a/backend/src/test/java/ddangkong/domain/balance/content/RoomContentRepositoryTest.java +++ b/backend/src/test/java/ddangkong/domain/balance/content/RoomContentRepositoryTest.java @@ -12,16 +12,21 @@ class RoomContentRepositoryTest extends BaseRepositoryTest { @Autowired private RoomContentRepository roomContentRepository; + @Autowired + private RoomRepository roomRepository; + @Nested - class 방의_최신_질문_조회 { + class 방의_해당_라운드_질문_조회 { @Test - void 방의_가장_최신의_질문을_조회할_수_있다() { + void 방의_해당_라운드의_질문을_조회할_수_있다() { // given Long roomId = 1L; + Room room = roomRepository.findById(roomId).get(); + int round = 2; // when - RoomContent actual = roomContentRepository.findTopByRoomIdOrderByCreatedAtDesc(roomId).get(); + RoomContent actual = roomContentRepository.findByRoomAndRound(room, round).get(); // then assertThat(actual.getId()).isEqualTo(2L); diff --git a/backend/src/test/java/ddangkong/service/balance/content/BalanceContentServiceTest.java b/backend/src/test/java/ddangkong/service/balance/content/BalanceContentServiceTest.java index c79234c55..1845abc9b 100644 --- a/backend/src/test/java/ddangkong/service/balance/content/BalanceContentServiceTest.java +++ b/backend/src/test/java/ddangkong/service/balance/content/BalanceContentServiceTest.java @@ -14,21 +14,21 @@ class BalanceContentServiceTest extends BaseServiceTest { - private static final Long PROGRESS_ROOM_ID = 1L; - private static final Long NOT_EXIST_ROOM_ID = 2L; - private static final BalanceContentResponse BALANCE_CONTENT_RESPONSE = new BalanceContentResponse( - 1L, Category.EXAMPLE, 5, 2, "민초 vs 반민초", - new BalanceOptionResponse(1L, "민초"), - new BalanceOptionResponse(2L, "반민초")); - @Autowired private BalanceContentService balanceContentService; - @Nested class 현재_방의_밸런스_게임_내용_조회 { + private static final Long PROGRESS_ROOM_ID = 1L; + private static final Long NOT_EXIST_ROOM_ID = 3L; + private static final Long NOT_PROGRESSED_ROOM_ID = 2L; + private static final BalanceContentResponse BALANCE_CONTENT_RESPONSE = new BalanceContentResponse( + 1L, Category.EXAMPLE, 5, 2, "민초 vs 반민초", + new BalanceOptionResponse(1L, "민초"), + new BalanceOptionResponse(2L, "반민초")); + @Test - void 방의_최신_밸런스_게임_내용을_조회할_수_있다() { + void 방의_진행_중인_밸런스_게임_내용을_조회할_수_있다() { // when BalanceContentResponse actual = balanceContentService.findRecentBalanceContent(PROGRESS_ROOM_ID); @@ -41,7 +41,15 @@ class 현재_방의_밸런스_게임_내용_조회 { // when & then assertThatThrownBy(() -> balanceContentService.findRecentBalanceContent(NOT_EXIST_ROOM_ID)) .isInstanceOf(BadRequestException.class) - .hasMessage("해당 방의 질문이 존재하지 않습니다."); + .hasMessage("해당 방이 존재하지 않습니다."); + } + + @Test + void 방의_현재_라운드의_질문이_없을_경우_예외를_던진다() { + // when & then + assertThatThrownBy(() -> balanceContentService.findRecentBalanceContent(NOT_PROGRESSED_ROOM_ID)) + .isInstanceOf(BadRequestException.class) + .hasMessage("해당 방의 현재 진행중인 질문이 존재하지 않습니다."); } } } diff --git a/backend/src/test/resources/init-test.sql b/backend/src/test/resources/init-test.sql index 1c62304cf..4f1cdee0e 100644 --- a/backend/src/test/resources/init-test.sql +++ b/backend/src/test/resources/init-test.sql @@ -1,5 +1,5 @@ INSERT INTO room (total_round, current_round) -VALUES (5, 2); +VALUES (5, 2), (5, 1); INSERT INTO member (nickname, room_id) VALUES ('mohamedeu al katan', 1), From 8900a2946aef602d6a935dfca34a0965b74a8d43 Mon Sep 17 00:00:00 2001 From: "DESKTOP-E5VRH4Q\\cnddk" Date: Wed, 24 Jul 2024 13:02:33 +0900 Subject: [PATCH 7/8] =?UTF-8?q?style:=20=EC=A4=91=EB=B3=B5=EC=9D=84=20?= =?UTF-8?q?=EC=A4=84=EC=9D=B4=EA=B8=B0=20=EC=9C=84=ED=95=B4,=20exception?= =?UTF-8?q?=20=EC=9D=B4=EB=A6=84=20=EB=B3=80=EA=B2=BD=20#52?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit InternalServerErrorException 에서 InternalServerException 으로 변경 --- .../controller/exception/GlobalExceptionHandler.java | 4 ++-- .../ddangkong/exception/InternalServerErrorException.java | 8 -------- .../java/ddangkong/exception/InternalServerException.java | 8 ++++++++ .../service/balance/content/BalanceContentService.java | 4 ++-- 4 files changed, 12 insertions(+), 12 deletions(-) delete mode 100644 backend/src/main/java/ddangkong/exception/InternalServerErrorException.java create mode 100644 backend/src/main/java/ddangkong/exception/InternalServerException.java diff --git a/backend/src/main/java/ddangkong/controller/exception/GlobalExceptionHandler.java b/backend/src/main/java/ddangkong/controller/exception/GlobalExceptionHandler.java index b7b004835..382fd267e 100644 --- a/backend/src/main/java/ddangkong/controller/exception/GlobalExceptionHandler.java +++ b/backend/src/main/java/ddangkong/controller/exception/GlobalExceptionHandler.java @@ -1,7 +1,7 @@ package ddangkong.controller.exception; import ddangkong.exception.BadRequestException; -import ddangkong.exception.InternalServerErrorException; +import ddangkong.exception.InternalServerException; import jakarta.validation.ConstraintViolationException; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; @@ -42,7 +42,7 @@ public ErrorResponse handleBadRequestException(BadRequestException e) { @ExceptionHandler @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) - public ErrorResponse handleInternalServerErrorException(InternalServerErrorException e) { + public ErrorResponse handleInternalServerErrorException(InternalServerException e) { log.error(e.getMessage(), e); return new ErrorResponse(SERVER_ERROR_MESSAGE); diff --git a/backend/src/main/java/ddangkong/exception/InternalServerErrorException.java b/backend/src/main/java/ddangkong/exception/InternalServerErrorException.java deleted file mode 100644 index bfe12bc14..000000000 --- a/backend/src/main/java/ddangkong/exception/InternalServerErrorException.java +++ /dev/null @@ -1,8 +0,0 @@ -package ddangkong.exception; - -public class InternalServerErrorException extends RuntimeException { - - public InternalServerErrorException(String message) { - super(message); - } -} diff --git a/backend/src/main/java/ddangkong/exception/InternalServerException.java b/backend/src/main/java/ddangkong/exception/InternalServerException.java new file mode 100644 index 000000000..43f60da4e --- /dev/null +++ b/backend/src/main/java/ddangkong/exception/InternalServerException.java @@ -0,0 +1,8 @@ +package ddangkong.exception; + +public class InternalServerException extends RuntimeException { + + public InternalServerException(String message) { + super(message); + } +} diff --git a/backend/src/main/java/ddangkong/service/balance/content/BalanceContentService.java b/backend/src/main/java/ddangkong/service/balance/content/BalanceContentService.java index 9a4888740..775a2937e 100644 --- a/backend/src/main/java/ddangkong/service/balance/content/BalanceContentService.java +++ b/backend/src/main/java/ddangkong/service/balance/content/BalanceContentService.java @@ -9,7 +9,7 @@ import ddangkong.domain.balance.option.BalanceOption; import ddangkong.domain.balance.option.BalanceOptionRepository; import ddangkong.exception.BadRequestException; -import ddangkong.exception.InternalServerErrorException; +import ddangkong.exception.InternalServerException; import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -54,7 +54,7 @@ private List findBalanceOptions(BalanceContent balanceContent) { private void validateBalanceOptions(List balanceOptions) { if (balanceOptions.size() != BALANCE_OPTION_SIZE) { - throw new InternalServerErrorException("밸런스 게임의 선택지가 %d개입니다".formatted(balanceOptions.size())); + throw new InternalServerException("밸런스 게임의 선택지가 %d개입니다".formatted(balanceOptions.size())); } } } From 885370613a8ff742282b78acfef5f3c022ba246b Mon Sep 17 00:00:00 2001 From: "DESKTOP-E5VRH4Q\\cnddk" Date: Wed, 24 Jul 2024 13:09:24 +0900 Subject: [PATCH 8/8] =?UTF-8?q?feat:=20currentRound=EC=9D=98=20=EA=B8=B0?= =?UTF-8?q?=EB=B3=B8=20=EA=B0=92=20=EC=84=A4=EC=A0=95=20#52?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/ddangkong/domain/balance/content/Room.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/backend/src/main/java/ddangkong/domain/balance/content/Room.java b/backend/src/main/java/ddangkong/domain/balance/content/Room.java index 5175c79c3..f6d221539 100644 --- a/backend/src/main/java/ddangkong/domain/balance/content/Room.java +++ b/backend/src/main/java/ddangkong/domain/balance/content/Room.java @@ -15,6 +15,7 @@ public class Room { private static final int DEFAULT_TOTAL_ROUND = 5; + private static final int DEFAULT_CURRENT_ROUND = 1; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -24,5 +25,5 @@ public class Room { private int totalRound = DEFAULT_TOTAL_ROUND; @Column(nullable = false) - private int currentRound; + private int currentRound = DEFAULT_CURRENT_ROUND; }