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] 주제, 선택지 조회 기능 구현 #36

Merged
merged 23 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
009f49d
feat: RoomQuestion에 생성 일자 추가 #28
leegwichan Jul 19, 2024
5d970fb
feat: RoomQuestion, BalanceOption의 Repository 구현 #28
leegwichan Jul 19, 2024
c4ae03e
feat: 방 질문 조회 서버스 계층 구현 #28
leegwichan Jul 19, 2024
c1867d3
chore: 컨트롤러 테스트를 위해 Rest-Assured 의존성 추가 #28
leegwichan Jul 19, 2024
81b9d14
feat: 방 질문 조회 API 구현 #28
leegwichan Jul 19, 2024
62a14a9
build: rest-assured을 최신 버전(5.5.0)으로 변경 #28
leegwichan Jul 20, 2024
0a4501b
test: 테스트 케이스 일부 변경 및 추가 #28
leegwichan Jul 20, 2024
da4ed13
test: 일부 어노테이션의 기본값 명시한 것 제거, ServiceTest에서 Mock 환경 사용 #28
leegwichan Jul 20, 2024
f57c30c
style: test 파일 개행 및 들여쓰기 맞춤 #28
leegwichan Jul 20, 2024
8566753
refactor: 용도를 나타내기 위해 AuditingEntity 에서 BaseEntity로 파일 이름 변경 #28
leegwichan Jul 20, 2024
935ff1e
refactor: Repository 를 domain 패키지로 이동 및 필요없는 에노테이션 제거 #28
leegwichan Jul 20, 2024
458cd54
refactor: 지연 로딩의 여파를 막기 위해, 엔티티의 @EqualsAndHashCode, @ToString 제거 #28
leegwichan Jul 20, 2024
de80d87
fix: 서버 문제 트래킹을 명확히 하기 위해, ViolateDataException을 error 수준으로 로깅함 #28
leegwichan Jul 20, 2024
31e3543
refactor: 예외를 아키텍쳐 단위의 패키지로 이동 #28
leegwichan Jul 20, 2024
06b3dba
refactor: 구분을 쉽게 하기 위해 일부 테이블 및 컬럼 이름 변경 #28
leegwichan Jul 20, 2024
9e061d6
fix: RoomContentRepository를 room 패키지로 변경 #28
leegwichan Jul 22, 2024
246538a
fix: 서버 에러 시, 에러 스택 트레이스까지 출력하도록 변경, 일부 `@ResponseStatus` 추가 #28
leegwichan Jul 22, 2024
b042859
refactor: balance 패키지 추가 #28
leegwichan Jul 22, 2024
3d5b963
fix: API 명세서를 반영하여 Response 수정 #28
leegwichan Jul 22, 2024
112e91b
feat: BalanceContentController roomId 유효성 검사 추가
leegwichan Jul 22, 2024
39691e1
test: 테스트를 명확하게 설명하기 위해, Nested class 이름 수정
leegwichan Jul 22, 2024
b7ba2f4
test: given, when, then 주석 추가 #28
leegwichan Jul 22, 2024
2cc2472
test: 테스트 클래스 변경 #28
leegwichan Jul 22, 2024
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
1 change: 1 addition & 0 deletions backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
testImplementation 'io.rest-assured:rest-assured:5.5.0'
}

tasks.named('test') {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ddangkong.controller.balance.content;

import ddangkong.controller.balance.content.dto.BalanceContentResponse;
import ddangkong.service.balance.content.BalanceContentService;
import jakarta.validation.constraints.Positive;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
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")
@Validated
@RequiredArgsConstructor
public class BalanceContentController {

private final BalanceContentService balanceContentService;

@GetMapping("/balances/rooms/{roomId}/question")
public BalanceContentResponse getBalanceContent(@PathVariable @Positive Long roomId) {
return balanceContentService.findRecentBalanceContent(roomId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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.option.BalanceOption;
import lombok.Builder;

public record BalanceContentResponse(
Long contentId,
Category category,
String question,
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));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ddangkong.controller.balance.option.dto;

import ddangkong.domain.balance.option.BalanceOption;

public record BalanceOptionResponse(
Long optionId,
String name
) {
public static BalanceOptionResponse from(BalanceOption balanceOption) {
return new BalanceOptionResponse(balanceOption.getId(), balanceOption.getName());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ddangkong.controller.exception;

import ddangkong.service.excpetion.BusinessLogicException;
import ddangkong.service.excpetion.ViolateDataException;
import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
Expand All @@ -12,25 +14,45 @@
@Slf4j
public class GlobalExceptionHandler {

private static final String SERVER_ERROR_MESSAGE = "서버 오류가 발생했습니다. 관리자에게 문의하세요.";

@ExceptionHandler
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ErrorResponse handleBindingException(BindException e) {
log.warn(e.getMessage());

return new ErrorResponse(e.getBindingResult());
}

@ExceptionHandler
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ErrorResponse handleConstraintViolationException(ConstraintViolationException e) {
log.warn(e.getMessage());

return new ErrorResponse(e.getConstraintViolations());
}

@ExceptionHandler
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ErrorResponse handleBusinessLogicException(BusinessLogicException e) {
log.warn(e.getMessage());

return new ErrorResponse(e.getMessage());
}

@ExceptionHandler
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ErrorResponse handleViolateDataException(ViolateDataException e) {
log.error(e.getMessage(), e);

return new ErrorResponse(SERVER_ERROR_MESSAGE);
}

@ExceptionHandler
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ErrorResponse handleException(Exception e) {
log.error(e.getMessage());
log.error(e.getMessage(), e);

return new ErrorResponse("서버 오류가 발생했습니다. 관리자에게 문의하세요.");
return new ErrorResponse(SERVER_ERROR_MESSAGE);
}
}
19 changes: 19 additions & 0 deletions backend/src/main/java/ddangkong/domain/BaseEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ddangkong.domain;

import jakarta.persistence.Column;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.MappedSuperclass;
import java.time.LocalDateTime;
import lombok.Getter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
@Getter
public class BaseEntity {

@CreatedDate
@Column(updatable = false, nullable = false)
private LocalDateTime createdAt;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ddangkong.domain.question;
package ddangkong.domain.balance.content;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
Expand All @@ -8,17 +8,13 @@
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@EqualsAndHashCode
@ToString
public class BalanceQuestion {
public class BalanceContent {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -29,5 +25,5 @@ public class BalanceQuestion {
private Category category;

@Column(nullable = false)
private String content;
private String name;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ddangkong.domain.question;
package ddangkong.domain.balance.content;

public enum Category {
EXAMPLE,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
package ddangkong.domain.room;
package ddangkong.domain.balance.content;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@EqualsAndHashCode
@ToString
public class Room {

@Id
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ddangkong.domain.room;
package ddangkong.domain.balance.content;

import ddangkong.domain.question.BalanceQuestion;
import ddangkong.domain.BaseEntity;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
Expand All @@ -9,17 +9,13 @@
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@EqualsAndHashCode
@ToString
public class RoomQuestion {
public class RoomContent extends BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -30,6 +26,6 @@ public class RoomQuestion {
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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ddangkong.domain.balance.content;

import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;

public interface RoomContentRepository extends JpaRepository<RoomContent, Long> {

Optional<RoomContent> findTopByRoomIdOrderByCreatedAtDesc(Long roomId);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ddangkong.domain.option;
package ddangkong.domain.balance.option;

import ddangkong.domain.question.BalanceQuestion;
import ddangkong.domain.balance.content.BalanceContent;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
Expand All @@ -10,26 +10,22 @@
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@EqualsAndHashCode
@ToString
public class BalanceOption {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ddangkong.domain.balance.option;

import ddangkong.domain.balance.content.BalanceContent;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;

public interface BalanceOptionRepository extends JpaRepository<BalanceOption, Long> {

List<BalanceOption> findByBalanceContent(BalanceContent balanceContent);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ddangkong.domain.vote;
package ddangkong.domain.balance.vote;

import ddangkong.domain.balance.option.BalanceOption;
import ddangkong.domain.member.Member;
import ddangkong.domain.option.BalanceOption;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
Expand All @@ -10,16 +10,12 @@
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@EqualsAndHashCode
@ToString
public class BalanceVote {

@Id
Expand Down
6 changes: 1 addition & 5 deletions backend/src/main/java/ddangkong/domain/member/Member.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ddangkong.domain.member;

import ddangkong.domain.room.Room;
import ddangkong.domain.balance.content.Room;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
Expand All @@ -10,16 +10,12 @@
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@EqualsAndHashCode
@ToString
public class Member {

@Id
Expand Down
Loading