Skip to content

Commit

Permalink
merge: develop의 변경 사항과 충돌 해결
Browse files Browse the repository at this point in the history
RoomService, init.sql 및 일부 테스트 데이터 변경
  • Loading branch information
leegwichan committed Jul 26, 2024
2 parents 9c2fdcb + b36cca4 commit 7132cbc
Show file tree
Hide file tree
Showing 83 changed files with 1,328 additions and 171 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/be-cd-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,5 @@ jobs:
- name: Run new Docker container
run: |
sudo nohup docker run --name $CONTAINER_NAME \
sudo nohup docker run -p 80:8080 --name $CONTAINER_NAME \
$DOCKER_REPOSITORY_NAME:latest > ~/dev-nohup.out 2>&1 &
5 changes: 1 addition & 4 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
FROM openjdk:17-jdk-slim

ARG JAR_FILE=build/libs/*.jar
ARG SPRING_PROFILE

ENV SPRING_PROFILE=${SPRING_PROFILE}

COPY ${JAR_FILE} app.jar

ENTRYPOINT [ "java", "-jar", "-Duser.timezone=Asia/Seoul", "-Dspring.profiles.active=${SPRING_PROFILE}", "app.jar" ]
ENTRYPOINT [ "java", "-jar", "-Duser.timezone=Asia/Seoul", "-Dspring.profiles.active=dev", "app.jar" ]
8 changes: 5 additions & 3 deletions backend/deploy/docker-compose-dev.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: "2.29.0"
version: "3.8"

services:
ddangkong-api-dev:
Expand All @@ -7,6 +7,8 @@ services:
context: ../
dockerfile: Dockerfile
args:
- SPRINT_PROFILE=dev
SPRING_PROFILE: dev
environment:
- SPRING_PROFILE=dev
ports:
- "8080:8080"
- "80:8080"
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ddangkong.controller.balance.content.dto;

import ddangkong.controller.balance.option.dto.BalanceOptionGroupResponse;

public record BalanceContentGroupResponse(
BalanceOptionGroupResponse firstOption,
BalanceOptionGroupResponse secondOption
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ddangkong.controller.balance.content.dto;

import ddangkong.controller.balance.option.dto.BalanceOptionTotalResponse;

public record BalanceContentTotalResponse(
BalanceOptionTotalResponse firstOption,
BalanceOptionTotalResponse secondOption
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ddangkong.controller.balance.option.dto;

import ddangkong.domain.balance.option.BalanceOption;
import ddangkong.domain.balance.vote.BalanceVote;
import java.util.List;

public record BalanceOptionGroupResponse(
Long optionId,
String name,
List<String> members,
int memberCount,
int percent
) {
public static BalanceOptionGroupResponse of(BalanceOption balanceOption,
List<BalanceVote> balanceVotes,
int totalSize) {
List<String> members = balanceVotes.stream()
.map(BalanceVote::getMemberNickname)
.toList();
return new BalanceOptionGroupResponse(balanceOption.getId(),
balanceOption.getName(),
members,
balanceVotes.size(),
(int) Math.round(balanceVotes.size() * 1.0 / totalSize * 100));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ddangkong.controller.balance.option.dto;

import ddangkong.domain.balance.option.BalanceOption;

public record BalanceOptionTotalResponse(
Long optionId,
String name,
int percent
) {
public static BalanceOptionTotalResponse of(BalanceOption balanceOption,
Long totalSize,
Long optionSize) {
return new BalanceOptionTotalResponse(balanceOption.getId(),
balanceOption.getName(),
(int) Math.round(optionSize * 1.0 / totalSize * 100));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ddangkong.controller.balance.vote;

import ddangkong.controller.balance.vote.dto.BalanceVoteResultResponse;
import ddangkong.controller.balance.vote.dto.BalanceVoteRequest;
import ddangkong.controller.balance.vote.dto.BalanceVoteResponse;
import ddangkong.service.balance.vote.BalanceVoteService;
Expand All @@ -8,6 +9,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand All @@ -23,6 +25,12 @@ public class BalanceVoteController {

private final BalanceVoteService balanceVoteService;

@GetMapping("/balances/rooms/{roomId}/contents/{contentId}/vote-result")
public BalanceVoteResultResponse getBalanceRoundResult(@PathVariable @Positive Long roomId,
@PathVariable @Positive Long contentId) {
return balanceVoteService.findBalanceVoteResult(roomId, contentId);
}

@PostMapping("/balances/rooms/{roomId}/contents/{contentId}/votes")
@ResponseStatus(HttpStatus.CREATED)
public BalanceVoteResponse createBalanceVote(@PathVariable @Positive Long roomId,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ddangkong.controller.balance.vote.dto;

import ddangkong.controller.balance.content.dto.BalanceContentGroupResponse;
import ddangkong.controller.balance.content.dto.BalanceContentTotalResponse;

public record BalanceVoteResultResponse(
BalanceContentGroupResponse group,
BalanceContentTotalResponse total
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ddangkong.domain.balance.content;

import ddangkong.domain.balance.option.BalanceOption;
import ddangkong.exception.BadRequestException;
import org.springframework.data.jpa.repository.JpaRepository;

public interface BalanceContentRepository extends JpaRepository<BalanceContent, Long> {

default BalanceContent getById(Long id) {
return findById(id)
.orElseThrow(() -> new BadRequestException("해당 질문 컨텐츠가 존재하지 않습니다."));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public interface BalanceOptionRepository extends JpaRepository<BalanceOption, Long> {

List<BalanceOption> findByBalanceContent(BalanceContent balanceContent);
List<BalanceOption> findAllByBalanceContent(BalanceContent balanceContent);

default BalanceOption getById(Long id) {
return findById(id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ public BalanceVote(BalanceOption balanceOption, Member member) {
public Long getOptionId() {
return balanceOption.getId();
}

public String getMemberNickname() {
return member.getNickname();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package ddangkong.domain.balance.vote;

import ddangkong.domain.balance.option.BalanceOption;
import ddangkong.domain.balance.room.Room;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;

public interface BalanceVoteRepository extends JpaRepository<BalanceVote, Long> {
Long countByBalanceOption(BalanceOption balanceOption);

List<BalanceVote> findByMemberRoomAndBalanceOption(Room room, BalanceOption balanceOption);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public interface MemberRepository extends JpaRepository<Member, Long> {

List<Member> findByRoom(Room room);
List<Member> findAllByRoom(Room room);

default Member getById(Long id) {
return findById(id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private RoomContent findCurrentRoomContent(Long roomId) {
}

private List<BalanceOption> findBalanceOptions(BalanceContent balanceContent) {
List<BalanceOption> balanceOptions = balanceOptionRepository.findByBalanceContent(balanceContent);
List<BalanceOption> balanceOptions = balanceOptionRepository.findAllByBalanceContent(balanceContent);
validateBalanceOptions(balanceOptions);
return balanceOptions;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class RoomService {
public RoomMembersResponse findAllRoomMember(Long roomId) {
Room room = roomRepository.getById(roomId);

List<RoomMemberResponse> response = memberRepository.findByRoom(room)
List<RoomMemberResponse> response = memberRepository.findAllByRoom(room)
.stream()
.map(RoomMemberResponse::new)
.toList();
Expand Down Expand Up @@ -80,7 +80,7 @@ private RoomContent findCurrentRoomContent(Room room) {
}

private List<BalanceOption> findBalanceOptions(BalanceContent balanceContent) {
List<BalanceOption> balanceOptions = balanceOptionRepository.findByBalanceContent(balanceContent);
List<BalanceOption> balanceOptions = balanceOptionRepository.findAllByBalanceContent(balanceContent);
validateBalanceOptions(balanceOptions);
return balanceOptions;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
package ddangkong.service.balance.vote;

import ddangkong.controller.balance.content.dto.BalanceContentGroupResponse;
import ddangkong.controller.balance.content.dto.BalanceContentTotalResponse;
import ddangkong.controller.balance.option.dto.BalanceOptionGroupResponse;
import ddangkong.controller.balance.option.dto.BalanceOptionTotalResponse;
import ddangkong.controller.balance.vote.dto.BalanceVoteResultResponse;
import ddangkong.controller.balance.vote.dto.BalanceVoteRequest;
import ddangkong.controller.balance.vote.dto.BalanceVoteResponse;
import ddangkong.domain.balance.content.BalanceContent;
import ddangkong.domain.balance.content.BalanceContentRepository;
import ddangkong.domain.balance.option.BalanceOption;
import ddangkong.domain.balance.option.BalanceOptionRepository;
import ddangkong.domain.balance.room.Room;
import ddangkong.domain.balance.room.RoomContent;
import ddangkong.domain.balance.room.RoomContentRepository;
import ddangkong.domain.balance.room.RoomRepository;
import ddangkong.domain.balance.vote.BalanceVote;
import ddangkong.domain.balance.vote.BalanceVoteRepository;
import ddangkong.domain.member.Member;
import ddangkong.domain.member.MemberRepository;
import ddangkong.exception.BadRequestException;
import ddangkong.exception.InternalServerException;
import java.util.List;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -17,12 +31,20 @@
@RequiredArgsConstructor
public class BalanceVoteService {

private static final int BALANCE_OPTION_SIZE = 2;

private final BalanceVoteRepository balanceVoteRepository;

private final MemberRepository memberRepository;

private final BalanceOptionRepository balanceOptionRepository;

private final BalanceContentRepository balanceContentRepository;

private final RoomContentRepository roomContentRepository;

private final RoomRepository roomRepository;

@Transactional
public BalanceVoteResponse createBalanceVote(BalanceVoteRequest request, Long roomId, Long contentId) {
BalanceOption balanceOption = findValidOption(request.optionId(), contentId);
Expand Down Expand Up @@ -52,4 +74,70 @@ private Member findValidMember(Long memberId, Long roomId) {
}
return member;
}

@Transactional(readOnly = true)
public BalanceVoteResultResponse findBalanceVoteResult(Long roomId, Long balanceContentId) {
Room room = roomRepository.getById(roomId);
List<BalanceOption> balanceOptions = findBalanceOptions(room, balanceContentId);

BalanceOption firstOption = balanceOptions.get(0);
BalanceOption secondOption = balanceOptions.get(1);

BalanceContentGroupResponse group = getBalanceContentGroupResponse(room, firstOption, secondOption);
BalanceContentTotalResponse total = getBalanceContentTotalResponse(firstOption, secondOption);

return new BalanceVoteResultResponse(group, total);
}

private BalanceContentGroupResponse getBalanceContentGroupResponse(Room room,
BalanceOption firstOption,
BalanceOption secondOption) {
List<BalanceVote> firstOptionVotes = balanceVoteRepository
.findByMemberRoomAndBalanceOption(room, firstOption);
List<BalanceVote> secondOptionVotes = balanceVoteRepository
.findByMemberRoomAndBalanceOption(room, secondOption);

int roomMemberSize = firstOptionVotes.size() + secondOptionVotes.size();
return new BalanceContentGroupResponse(
BalanceOptionGroupResponse.of(firstOption, firstOptionVotes, roomMemberSize),
BalanceOptionGroupResponse.of(secondOption, secondOptionVotes, roomMemberSize)
);
}

private BalanceContentTotalResponse getBalanceContentTotalResponse(BalanceOption firstOption,
BalanceOption secondOption) {
Long firstOptionCount = balanceVoteRepository.countByBalanceOption(firstOption);
Long secondOptionCount = balanceVoteRepository.countByBalanceOption(secondOption);
return new BalanceContentTotalResponse(
BalanceOptionTotalResponse.of(firstOption,
firstOptionCount + secondOptionCount,
firstOptionCount),
BalanceOptionTotalResponse.of(secondOption,
firstOptionCount + secondOptionCount,
secondOptionCount)
);
}

private List<BalanceOption> findBalanceOptions(Room room, Long balanceContentId) {
RoomContent roomContent = roomContentRepository.findByRoomAndRound(room, room.getCurrentRound())
.orElseThrow(() -> new BadRequestException("해당 방의 현재 진행중인 질문이 존재하지 않습니다."));
validateBalanceContent(balanceContentId, roomContent.getBalanceContent());

BalanceContent balanceContent = balanceContentRepository.getById(balanceContentId);
List<BalanceOption> balanceOptions = balanceOptionRepository.findAllByBalanceContent(balanceContent);
validateBalanceOptions(balanceOptions);
return balanceOptions;
}

private void validateBalanceContent(Long balanceContentId, BalanceContent balanceContent) {
if (!Objects.equals(balanceContent.getId(), balanceContentId)) {
throw new BadRequestException("현재 진행중인 질문의 컨텐츠와 일치하지 않는 요청입니다.");
}
}

private void validateBalanceOptions(List<BalanceOption> balanceOptions) {
if (balanceOptions.size() != BALANCE_OPTION_SIZE) {
throw new InternalServerException("밸런스 게임의 선택지가 %d개입니다".formatted(balanceOptions.size()));
}
}
}
17 changes: 17 additions & 0 deletions backend/src/main/resources/sql/data-dev.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ VALUES ('EXAMPLE', '민초 vs 반민초'),
('EXAMPLE', '개구리 맛 초콜릿 vs 초콜릿 맛 개구리'),
('EXAMPLE', '언제 죽을 지 알기 vs 어떻게 죽을 지 알기');


INSERT INTO balance_option (name, balance_content_id)
VALUES ('민초', 1),
('반민초', 1),
Expand All @@ -16,3 +17,19 @@ VALUES ('민초', 1),
('초콜릿 맛 개구리', 4),
('언제 죽을 지 알기', 5),
('어떻게 죽을 지 알기', 5);


INSERT INTO room(total_round, current_round)
VALUES (5, 1);


INSERT INTO room_content(room_id, balance_content_id, round, created_at)
VALUES (1, 1, 1, '2024-07-25 13:24:09'),
(1, 2, 2, '2024-07-25 13:24:09'),
(1, 3, 3, '2024-07-25 13:24:09'),
(1, 4, 4, '2024-07-25 13:24:09'),
(1, 5, 5, '2024-07-25 13:24:09');


INSERT INTO member(room_id, nickname, is_master)
VALUES (1, '콩콩', true);
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class BalanceContentServiceTest extends BaseServiceTest {
class 현재_방의_밸런스_게임_내용_조회 {

private static final Long PROGRESS_ROOM_ID = 1L;
private static final Long NOT_EXIST_ROOM_ID = 3L;
private static final Long NOT_EXIST_ROOM_ID = 99999999L;
private static final Long NOT_PROGRESSED_ROOM_ID = 2L;
private static final BalanceContentResponse BALANCE_CONTENT_RESPONSE = new BalanceContentResponse(
1L, Category.EXAMPLE, 5, 2, "민초 vs 반민초",
Expand Down
Loading

0 comments on commit 7132cbc

Please sign in to comment.