Skip to content

Commit

Permalink
refactor: 구분을 쉽게 하기 위해 일부 테이블 및 컬럼 이름 변경 #28
Browse files Browse the repository at this point in the history
- BalanceQuestion -> BalanceContent로 변경
- 필드명 content, title 등을 name으로 바꿈
  • Loading branch information
leegwichan committed Jul 20, 2024
1 parent 31e3543 commit 06b3dba
Show file tree
Hide file tree
Showing 17 changed files with 120 additions and 117 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

This file was deleted.

This file was deleted.

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

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
Expand All @@ -14,7 +14,7 @@
@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
public class BalanceQuestion {
public class BalanceContent {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -25,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.content;

public enum Category {
EXAMPLE,
Expand Down
Original file line number Diff line number Diff line change
@@ -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<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;

import ddangkong.domain.question.BalanceQuestion;
import ddangkong.domain.content.BalanceContent;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
Expand All @@ -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;
}
Original file line number Diff line number Diff line change
@@ -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<BalanceOption, Long> {

List<BalanceOption> findByBalanceQuestion(BalanceQuestion balanceQuestion);
List<BalanceOption> findByBalanceContent(BalanceContent balanceContent);
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)
Expand All @@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<BalanceOption> balanceOptions = findBalanceOptions(balanceQuestion);
public BalanceContentResponse findRecentBalanceContent(Long roomId) {
BalanceContent balanceContent = findRecentContent(roomId);
List<BalanceOption> 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<BalanceOption> findBalanceOptions(BalanceQuestion balanceQuestion) {
List<BalanceOption> balanceOptions = balanceOptionRepository.findByBalanceQuestion(balanceQuestion);
private List<BalanceOption> findBalanceOptions(BalanceContent balanceContent) {
List<BalanceOption> balanceOptions = balanceOptionRepository.findByBalanceContent(balanceContent);
validateBalanceOptions(balanceOptions);
return balanceOptions;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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, "반민초"));
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
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;

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);
}
Expand Down
Loading

0 comments on commit 06b3dba

Please sign in to comment.