Skip to content

Commit

Permalink
[feat] 승부 히스토리 전체 조회하기 api 구현 (#62)
Browse files Browse the repository at this point in the history
* feat: Add ResponseDto

* feat: Add ErrorType

* feat: Get History api
  • Loading branch information
2zerozu authored Jul 15, 2023
1 parent 0d6114f commit 6e40cc9
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 3 deletions.
27 changes: 27 additions & 0 deletions src/main/java/com/universe/uni/controller/HistoryController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.universe.uni.controller;

import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import com.universe.uni.dto.response.GameHistoryResponseDto;
import com.universe.uni.service.HistoryService;

import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/history")
public class HistoryController {
private final HistoryService historyService;

@GetMapping()
@ResponseStatus(HttpStatus.OK)
public List<GameHistoryResponseDto> getHome() {
return historyService.getGameHistory();
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/universe/uni/dto/MissionResultDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.universe.uni.dto;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;

import lombok.Builder;

@JsonPropertyOrder({"result", "time"})
@Builder
public record MissionResultDto(
String result,
String time
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.universe.uni.dto.response;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.universe.uni.dto.MissionResultDto;

import lombok.Builder;

@JsonPropertyOrder({"roundGameId", "date", "result", "title", "image", "myMission", "partnerMission"})
@Builder
public record GameHistoryResponseDto(
int roundGameId,
String date,
String result,
String title,
String image,
MissionResultDto myMission,
MissionResultDto partnerMission
) {
}
11 changes: 8 additions & 3 deletions src/main/java/com/universe/uni/exception/dto/ErrorType.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,15 @@ public enum ErrorType {
"잘못된 endpoint에 요청한 경우입니다."),
NOT_FOUND_USER(HttpStatus.NOT_FOUND, "UE5002",
"조회한 유저가 존재하지 않는 경우 입니다."),
NOT_FOUND_MISSION_CATEGORY_EXCEPTION(HttpStatus.NOT_FOUND, "UE5003", "존재하지 않는 미션 카테고리입니다"),
NOT_FOUND_MISSION_CONTENT(HttpStatus.NOT_FOUND, "UE5004", "해당 카테고리 미션이 존재하지 않습니다."),
NOT_FOUND_ROUND_MISSION(HttpStatus.NOT_FOUND, "UE5005", "해당 라운드 미션이 존재하지 않습니다."),
NOT_FOUND_MISSION_CATEGORY_EXCEPTION(HttpStatus.NOT_FOUND, "UE5003",
"존재하지 않는 미션 카테고리입니다"),
NOT_FOUND_MISSION_CONTENT(HttpStatus.NOT_FOUND, "UE5004",
"해당 카테고리 미션이 존재하지 않습니다."),
NOT_FOUND_ROUND_MISSION(HttpStatus.NOT_FOUND, "UE5005",
"해당 라운드 미션이 존재하지 않습니다."),
NOT_FOUND_WISH_COUPON(HttpStatus.NOT_FOUND, "UE5006", "소원권이 존재하지 않습니다."),
NOT_FOUND_ROUND_GAME(HttpStatus.NOT_FOUND, "UE5007", "해당 라운드 게임이 존재하지 않습니다."),
NOT_FOUND_COUPLE(HttpStatus.NOT_FOUND, "UE5008", "커플이 존재하지 않습니다."),

/**
* 406 Not Acceptable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.universe.uni.repository;

import java.util.List;
import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;
Expand All @@ -11,5 +12,7 @@
public interface RoundMissionRepository extends JpaRepository<RoundMission, Long> {
Optional<RoundMission> findByRoundGameAndUser(RoundGame roundGame, User user);

List<RoundMission> findByRoundGame(RoundGame roundGame);

Optional<RoundMission> findByRoundGameAndUserIsNot(RoundGame roundGame, User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

import org.springframework.data.jpa.repository.JpaRepository;

import com.universe.uni.domain.entity.User;
import com.universe.uni.domain.entity.UserGameHistory;

public interface UserGameHistoryRepository extends JpaRepository<UserGameHistory, Long> {

List<UserGameHistory> findByUserId(Long userId);

List<UserGameHistory> findByUser(User user);
}
101 changes: 101 additions & 0 deletions src/main/java/com/universe/uni/service/HistoryService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.universe.uni.service;

import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.universe.uni.domain.entity.Couple;
import com.universe.uni.domain.entity.MissionCategory;
import com.universe.uni.domain.entity.RoundGame;
import com.universe.uni.domain.entity.RoundMission;
import com.universe.uni.domain.entity.User;
import com.universe.uni.domain.entity.UserGameHistory;
import com.universe.uni.dto.MissionResultDto;
import com.universe.uni.dto.response.GameHistoryResponseDto;
import com.universe.uni.exception.NotFoundException;
import com.universe.uni.exception.dto.ErrorType;
import com.universe.uni.repository.CoupleRepository;
import com.universe.uni.repository.MissionCategoryRepository;
import com.universe.uni.repository.MissionContentRepository;
import com.universe.uni.repository.RoundGameRepository;
import com.universe.uni.repository.RoundMissionRepository;
import com.universe.uni.repository.UserGameHistoryRepository;
import com.universe.uni.repository.UserRepository;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
@Transactional
public class HistoryService {

private final UserGameHistoryRepository userGameHistoryRepository;
private final RoundGameRepository roundGameRepository;
private final MissionCategoryRepository missionCategoryRepository;
private final MissionContentRepository missionContentRepository;
private final RoundMissionRepository roundMissionRepository;
private final CoupleRepository coupleRepository;
private final UserRepository userRepository;

public List<GameHistoryResponseDto> getGameHistory() {
Long userId = 1L; // TODO: replace with actual user id

// User 가져옴
User user = userRepository.findById(userId)
.orElseThrow(() -> new NotFoundException(ErrorType.NOT_FOUND_USER));

// User로 UserGameHistory 가져옴
List<UserGameHistory> userGameHistoryList = userGameHistoryRepository.findByUser(user);

return userGameHistoryList.stream()
.map(userGameHistory -> {
// UserGameHistory의 Game의 id로 RoundGame 조회
RoundGame roundGame = roundGameRepository.findById(userGameHistory.getGame().getId())
.orElseThrow(() -> new NotFoundException(ErrorType.NOT_FOUND_ROUND_GAME));
// RoundGame의 MissionCategory의 id로 MissionCategory 조회
MissionCategory missionCategory = missionCategoryRepository.findById(
roundGame.getMissionCategory().getId())
.orElseThrow(() -> new NotFoundException(ErrorType.NOT_FOUND_MISSION_CATEGORY_EXCEPTION));

// RoundGame으로 RoundMission 리스트 조회
List<RoundMission> roundMissionList = roundMissionRepository.findByRoundGame(roundGame);

return GameHistoryResponseDto.builder()
.roundGameId(roundGame.getId().intValue())
.date(userGameHistory.getUpdatedAt().format(DateTimeFormatter.ISO_DATE))
.result(userGameHistory.getResult().name())
.title(missionCategory.getTitle())
.image(missionCategory.getImage())
.myMission(createMissionResultDto(roundMissionList, userId))
.partnerMission(createMissionResultDto(roundMissionList, getPartnerId(userId)))
.build();
}).collect(Collectors.toList());
}

private Long getPartnerId(Long userId) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new NotFoundException(ErrorType.NOT_FOUND_USER));
Couple couple = user.getCouple();

List<User> userListInCouple = userRepository.findByCouple(couple);
return userListInCouple.stream()
.filter(u -> !u.getId().equals(userId))
.findFirst()
.map(User::getId)
.orElseThrow(() -> new NotFoundException(ErrorType.NOT_FOUND_COUPLE));
}

private MissionResultDto createMissionResultDto(List<RoundMission> roundMissions, Long userId) {
RoundMission roundMission = roundMissions.stream()
.filter(rm -> rm.getUser().getId().equals(userId))
.findFirst()
.orElseThrow(() -> new NotFoundException(ErrorType.NOT_FOUND_ROUND_MISSION));
return MissionResultDto.builder()
.result(roundMission.getResult().name())
.time(roundMission.getUpdatedAt().format(DateTimeFormatter.ISO_LOCAL_TIME))
.build();
}
}

0 comments on commit 6e40cc9

Please sign in to comment.