Skip to content

Commit

Permalink
feat: Get home api
Browse files Browse the repository at this point in the history
  • Loading branch information
2zerozu committed Jul 13, 2023
1 parent 9db47bb commit 818971a
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 1 deletion.
26 changes: 26 additions & 0 deletions src/main/java/com/universe/uni/controller/HomeController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.universe.uni.controller;

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.HomeResponseDto;
import com.universe.uni.service.HomeService;

import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/home")
public class HomeController {

private final HomeService homeService;

@GetMapping()
@ResponseStatus(HttpStatus.OK)
public HomeResponseDto getHome() {
return homeService.getHome();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

import lombok.Builder;

@JsonPropertyOrder({"userId", "myScore", "partnerScore", "drawCount", "couple", "shortGame"})
@JsonPropertyOrder({"userId", "roundGameId", "myScore", "partnerScore", "drawCount", "couple", "shortGame"})
@Builder
public record HomeResponseDto(
Long userId,
Long roundGameId,
int myScore,
int partnerScore,
int drawCount,
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/universe/uni/repository/GameRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
public interface GameRepository extends JpaRepository<Game, Long> {

boolean existsByCoupleAndEnable(Couple couple, boolean enable);

Game findByCoupleId(Long coupleId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
import com.universe.uni.domain.entity.RoundGame;

public interface RoundGameRepository extends JpaRepository<RoundGame, Long> {
RoundGame findByGameId(Long gameId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.universe.uni.repository;

import java.util.List;

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

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

public interface UserGameHistoryRepository extends JpaRepository<UserGameHistory, Long> {

List<UserGameHistory> findByUserId(Long userId);
}
85 changes: 85 additions & 0 deletions src/main/java/com/universe/uni/service/HomeService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.universe.uni.service;

import static com.universe.uni.exception.dto.ErrorType.NOT_FOUND_USER;

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

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

import com.universe.uni.domain.GameResult;
import com.universe.uni.domain.entity.Game;
import com.universe.uni.domain.entity.RoundGame;
import com.universe.uni.domain.entity.ShortGame;
import com.universe.uni.domain.entity.User;
import com.universe.uni.domain.entity.UserGameHistory;
import com.universe.uni.dto.CoupleDto;
import com.universe.uni.dto.ShortGameDto;
import com.universe.uni.dto.response.HomeResponseDto;
import com.universe.uni.exception.NotFoundException;
import com.universe.uni.repository.GameRepository;
import com.universe.uni.repository.RoundGameRepository;
import com.universe.uni.repository.UserGameHistoryRepository;
import com.universe.uni.repository.UserRepository;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
@Transactional
public class HomeService {
private final UserRepository userRepository;
private final GameRepository gameRepository;
private final UserGameHistoryRepository userGameHistoryRepository;
private final RoundGameRepository roundGameRepository;

public HomeResponseDto getHome() {
/**
* jwt userId
* game에서 coupleId 조회해서 GameResult.WIN, GameResult.LOSE, GameResult.DRAW 각각 개수 변수에 저장
* userId로 shortGame 가져옴
* shortGame은 nullable합니다.
*/

/** TODO 영주 : 추후 1L 내 userId로 바꾸기*/
Long userId = 1L;

User user = userRepository.findById(userId).orElseThrow(() -> new NotFoundException(NOT_FOUND_USER));

Game game = gameRepository.findByCoupleId(user.getCouple().getId());

List<UserGameHistory> gameHistoryList = userGameHistoryRepository.findByUserId(userId);

RoundGame roundGame = roundGameRepository.findByGameId(game.getId());

int myScore = (int)gameHistoryList.stream().filter(history -> history.getResult() == GameResult.WIN).count();

int partnerScore = (int)gameHistoryList.stream()
.filter(history -> history.getResult() == GameResult.LOSE)
.count();

int drawCount = (int)gameHistoryList.stream().filter(history -> history.getResult() == GameResult.DRAW).count();

Optional<ShortGameDto> shortGameDto = Optional.empty();
if (game instanceof ShortGame) {
shortGameDto = Optional.of(new ShortGameDto((ShortGame)game));
}

CoupleDto coupleDto = CoupleDto.builder()
.id(user.getCouple().getId())
.startDate(String.valueOf(user.getCouple().getStartDate()))
.heartToken(user.getCouple().getHeartToken())
.build();

return HomeResponseDto.builder()
.userId(userId)
.roundGameId(roundGame.getId())
.myScore(myScore)
.partnerScore(partnerScore)
.drawCount(drawCount)
.couple(coupleDto)
.shortGame(shortGameDto.orElse(null))
.build();
}
}

0 comments on commit 818971a

Please sign in to comment.