Skip to content

Commit 06b3dba

Browse files
committed
refactor: 구분을 쉽게 하기 위해 일부 테이블 및 컬럼 이름 변경 #28
- BalanceQuestion -> BalanceContent로 변경 - 필드명 content, title 등을 name으로 바꿈
1 parent 31e3543 commit 06b3dba

17 files changed

+120
-117
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package ddangkong.controller.content;
2+
3+
import ddangkong.controller.content.dto.BalanceContentResponse;
4+
import ddangkong.service.content.BalanceContentService;
5+
import lombok.RequiredArgsConstructor;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.PathVariable;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
@RestController
12+
@RequestMapping("/api")
13+
@RequiredArgsConstructor
14+
public class BalanceContentController {
15+
16+
private final BalanceContentService balanceContentService;
17+
18+
@GetMapping("/balances/rooms/{roomId}/question")
19+
public BalanceContentResponse getBalanceContent(@PathVariable Long roomId) {
20+
return balanceContentService.findRecentBalanceContent(roomId);
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package ddangkong.controller.content.dto;
2+
3+
import ddangkong.controller.option.dto.BalanceOptionResponse;
4+
import ddangkong.domain.content.BalanceContent;
5+
import ddangkong.domain.content.Category;
6+
import ddangkong.domain.option.BalanceOption;
7+
import lombok.Builder;
8+
9+
public record BalanceContentResponse(
10+
Long questionId,
11+
Category category,
12+
String title,
13+
BalanceOptionResponse firstOption,
14+
BalanceOptionResponse secondOption
15+
) {
16+
17+
@Builder
18+
private BalanceContentResponse(BalanceContent balanceContent,
19+
BalanceOption firstOption,
20+
BalanceOption secondOption) {
21+
this(balanceContent.getId(),
22+
balanceContent.getCategory(),
23+
balanceContent.getName(),
24+
BalanceOptionResponse.from(firstOption),
25+
BalanceOptionResponse.from(secondOption));
26+
}
27+
}

backend/src/main/java/ddangkong/controller/option/dto/BalanceOptionResponse.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ public record BalanceOptionResponse(
77
String content
88
) {
99
public static BalanceOptionResponse from(BalanceOption balanceOption) {
10-
return new BalanceOptionResponse(balanceOption.getId(), balanceOption.getContent());
10+
return new BalanceOptionResponse(balanceOption.getId(), balanceOption.getName());
1111
}
1212
}

backend/src/main/java/ddangkong/controller/question/BalanceQuestionController.java

-22
This file was deleted.

backend/src/main/java/ddangkong/controller/question/dto/BalanceQuestionResponse.java

-24
This file was deleted.

backend/src/main/java/ddangkong/domain/question/BalanceQuestion.java backend/src/main/java/ddangkong/domain/content/BalanceContent.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ddangkong.domain.question;
1+
package ddangkong.domain.content;
22

33
import jakarta.persistence.Column;
44
import jakarta.persistence.Entity;
@@ -14,7 +14,7 @@
1414
@Entity
1515
@NoArgsConstructor(access = AccessLevel.PROTECTED)
1616
@Getter
17-
public class BalanceQuestion {
17+
public class BalanceContent {
1818

1919
@Id
2020
@GeneratedValue(strategy = GenerationType.IDENTITY)
@@ -25,5 +25,5 @@ public class BalanceQuestion {
2525
private Category category;
2626

2727
@Column(nullable = false)
28-
private String content;
28+
private String name;
2929
}

backend/src/main/java/ddangkong/domain/question/Category.java backend/src/main/java/ddangkong/domain/content/Category.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ddangkong.domain.question;
1+
package ddangkong.domain.content;
22

33
public enum Category {
44
EXAMPLE,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package ddangkong.domain.content;
2+
3+
import ddangkong.domain.room.RoomContent;
4+
import java.util.Optional;
5+
import org.springframework.data.jpa.repository.JpaRepository;
6+
7+
public interface RoomContentRepository extends JpaRepository<RoomContent, Long> {
8+
9+
Optional<RoomContent> findTopByRoomIdOrderByCreatedAtDesc(Long roomId);
10+
}

backend/src/main/java/ddangkong/domain/option/BalanceOption.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package ddangkong.domain.option;
22

3-
import ddangkong.domain.question.BalanceQuestion;
3+
import ddangkong.domain.content.BalanceContent;
44
import jakarta.persistence.Column;
55
import jakarta.persistence.Entity;
66
import jakarta.persistence.FetchType;
@@ -23,9 +23,9 @@ public class BalanceOption {
2323
private Long id;
2424

2525
@Column(nullable = false)
26-
private String content;
26+
private String name;
2727

2828
@ManyToOne(optional = false, fetch = FetchType.LAZY)
29-
@JoinColumn(name = "balance_question_id", nullable = false)
30-
private BalanceQuestion balanceQuestion;
29+
@JoinColumn(name = "balance_content_id", nullable = false)
30+
private BalanceContent balanceContent;
3131
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package ddangkong.domain.option;
22

3-
import ddangkong.domain.question.BalanceQuestion;
3+
import ddangkong.domain.content.BalanceContent;
44
import java.util.List;
55
import org.springframework.data.jpa.repository.JpaRepository;
66

77
public interface BalanceOptionRepository extends JpaRepository<BalanceOption, Long> {
88

9-
List<BalanceOption> findByBalanceQuestion(BalanceQuestion balanceQuestion);
9+
List<BalanceOption> findByBalanceContent(BalanceContent balanceContent);
1010
}

backend/src/main/java/ddangkong/domain/question/RoomQuestionRepository.java

-10
This file was deleted.

backend/src/main/java/ddangkong/domain/room/RoomQuestion.java backend/src/main/java/ddangkong/domain/room/RoomContent.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package ddangkong.domain.room;
22

33
import ddangkong.domain.BaseEntity;
4-
import ddangkong.domain.question.BalanceQuestion;
4+
import ddangkong.domain.content.BalanceContent;
55
import jakarta.persistence.Entity;
66
import jakarta.persistence.FetchType;
77
import jakarta.persistence.GeneratedValue;
@@ -16,7 +16,7 @@
1616
@Entity
1717
@NoArgsConstructor(access = AccessLevel.PROTECTED)
1818
@Getter
19-
public class RoomQuestion extends BaseEntity {
19+
public class RoomContent extends BaseEntity {
2020

2121
@Id
2222
@GeneratedValue(strategy = GenerationType.IDENTITY)
@@ -27,6 +27,6 @@ public class RoomQuestion extends BaseEntity {
2727
private Room room;
2828

2929
@ManyToOne(optional = false, fetch = FetchType.LAZY)
30-
@JoinColumn(name = "balance_question_id")
31-
private BalanceQuestion balanceQuestion;
30+
@JoinColumn(name = "balance_content_id")
31+
private BalanceContent balanceContent;
3232
}

backend/src/main/java/ddangkong/service/question/BalanceQuestionService.java backend/src/main/java/ddangkong/service/content/BalanceContentService.java

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
package ddangkong.service.question;
1+
package ddangkong.service.content;
22

3-
import ddangkong.controller.question.dto.BalanceQuestionResponse;
3+
import ddangkong.controller.content.dto.BalanceContentResponse;
44
import ddangkong.domain.option.BalanceOption;
55
import ddangkong.domain.option.BalanceOptionRepository;
6-
import ddangkong.domain.question.BalanceQuestion;
7-
import ddangkong.domain.question.RoomQuestionRepository;
6+
import ddangkong.domain.content.BalanceContent;
7+
import ddangkong.domain.content.RoomContentRepository;
88
import ddangkong.service.excpetion.BusinessLogicException;
99
import ddangkong.service.excpetion.ViolateDataException;
1010
import java.util.List;
@@ -14,34 +14,34 @@
1414

1515
@Service
1616
@RequiredArgsConstructor
17-
public class BalanceQuestionService {
17+
public class BalanceContentService {
1818

1919
private static final int BALANCE_OPTION_SIZE = 2;
2020

21-
private final RoomQuestionRepository roomQuestionRepository;
21+
private final RoomContentRepository roomContentRepository;
2222

2323
private final BalanceOptionRepository balanceOptionRepository;
2424

2525
@Transactional(readOnly = true)
26-
public BalanceQuestionResponse findRecentBalanceQuestion(Long roomId) {
27-
BalanceQuestion balanceQuestion = findRecentQuestion(roomId);
28-
List<BalanceOption> balanceOptions = findBalanceOptions(balanceQuestion);
26+
public BalanceContentResponse findRecentBalanceContent(Long roomId) {
27+
BalanceContent balanceContent = findRecentContent(roomId);
28+
List<BalanceOption> balanceOptions = findBalanceOptions(balanceContent);
2929

30-
return BalanceQuestionResponse.builder()
31-
.question(balanceQuestion)
30+
return BalanceContentResponse.builder()
31+
.balanceContent(balanceContent)
3232
.firstOption(balanceOptions.get(0))
3333
.secondOption(balanceOptions.get(1))
3434
.build();
3535
}
3636

37-
private BalanceQuestion findRecentQuestion(Long roomId) {
38-
return roomQuestionRepository.findTopByRoomIdOrderByCreatedAtDesc(roomId)
37+
private BalanceContent findRecentContent(Long roomId) {
38+
return roomContentRepository.findTopByRoomIdOrderByCreatedAtDesc(roomId)
3939
.orElseThrow(() -> new BusinessLogicException("해당 방의 질문이 존재하지 않습니다."))
40-
.getBalanceQuestion();
40+
.getBalanceContent();
4141
}
4242

43-
private List<BalanceOption> findBalanceOptions(BalanceQuestion balanceQuestion) {
44-
List<BalanceOption> balanceOptions = balanceOptionRepository.findByBalanceQuestion(balanceQuestion);
43+
private List<BalanceOption> findBalanceOptions(BalanceContent balanceContent) {
44+
List<BalanceOption> balanceOptions = balanceOptionRepository.findByBalanceContent(balanceContent);
4545
validateBalanceOptions(balanceOptions);
4646
return balanceOptions;
4747
}

backend/src/test/java/ddangkong/controller/question/BalanceQuestionControllerTest.java backend/src/test/java/ddangkong/controller/content/BalanceContentControllerTest.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
package ddangkong.controller.question;
1+
package ddangkong.controller.content;
22

33
import static org.assertj.core.api.Assertions.assertThat;
44

55
import ddangkong.controller.BaseControllerTest;
66
import ddangkong.controller.option.dto.BalanceOptionResponse;
7-
import ddangkong.controller.question.dto.BalanceQuestionResponse;
8-
import ddangkong.domain.question.Category;
7+
import ddangkong.controller.content.dto.BalanceContentResponse;
8+
import ddangkong.domain.content.Category;
99
import io.restassured.RestAssured;
1010
import org.junit.jupiter.api.Nested;
1111
import org.junit.jupiter.api.Test;
1212

13-
class BalanceQuestionControllerTest extends BaseControllerTest {
13+
class BalanceContentControllerTest extends BaseControllerTest {
1414

15-
private static final BalanceQuestionResponse EXPECTED_RESPONSE = new BalanceQuestionResponse(
15+
private static final BalanceContentResponse EXPECTED_RESPONSE = new BalanceContentResponse(
1616
1L, Category.EXAMPLE, "민초 vs 반민초",
1717
new BalanceOptionResponse(1L, "민초"),
1818
new BalanceOptionResponse(2L, "반민초"));
@@ -22,11 +22,11 @@ class 방의_질문_조회 {
2222

2323
@Test
2424
void 현재_방의_질문을_조회할_수_있다() {
25-
BalanceQuestionResponse actual = RestAssured.given().log().all()
25+
BalanceContentResponse actual = RestAssured.given().log().all()
2626
.when().get("/api/balances/rooms/1/question")
2727
.then().log().all()
2828
.statusCode(200)
29-
.extract().as(BalanceQuestionResponse.class);
29+
.extract().as(BalanceContentResponse.class);
3030

3131
assertThat(actual).isEqualTo(EXPECTED_RESPONSE);
3232
}

backend/src/test/java/ddangkong/domain/question/RoomQuestionRepositoryTest.java backend/src/test/java/ddangkong/domain/content/RoomContentRepositoryTest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
package ddangkong.domain.question;
1+
package ddangkong.domain.content;
22

33
import static org.assertj.core.api.Assertions.assertThat;
44

55
import ddangkong.domain.BaseRepositoryTest;
6-
import ddangkong.domain.room.RoomQuestion;
6+
import ddangkong.domain.room.RoomContent;
77
import org.junit.jupiter.api.Nested;
88
import org.junit.jupiter.api.Test;
99
import org.springframework.beans.factory.annotation.Autowired;
1010

1111
class RoomQuestionRepositoryTest extends BaseRepositoryTest {
1212

1313
@Autowired
14-
private RoomQuestionRepository roomQuestionRepository;
14+
private RoomContentRepository roomContentRepository;
1515

1616
@Nested
1717
class 방의_최신_질문_조회 {
1818

1919
@Test
2020
void 방의_가장_최신의_질문을_조회할_수_있다() {
21-
RoomQuestion actual = roomQuestionRepository.findTopByRoomIdOrderByCreatedAtDesc(1L).get();
21+
RoomContent actual = roomContentRepository.findTopByRoomIdOrderByCreatedAtDesc(1L).get();
2222

2323
assertThat(actual.getId()).isEqualTo(2L);
2424
}

0 commit comments

Comments
 (0)