Skip to content

Commit

Permalink
fix: Change home screen data
Browse files Browse the repository at this point in the history
  • Loading branch information
jinsu4755 committed Jan 28, 2024
1 parent 56bf877 commit ec4c5c3
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.universe.uni.controller;

import com.universe.uni.controller.docs.HomeControllerContract;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -14,12 +15,14 @@
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/home")
public class HomeController {
public class HomeController implements HomeControllerContract {

private final HomeService homeService;

@Deprecated
@GetMapping()
@ResponseStatus(HttpStatus.OK)
@Override
public HomeResponseDto getHome() {
return homeService.getHome();
}
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/com/universe/uni/controller/HomeControllerV1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.universe.uni.controller;

import com.universe.uni.controller.docs.HomeControllerV1Contract;
import com.universe.uni.dto.response.HomeResponseDto;
import com.universe.uni.service.HomeService;
import lombok.RequiredArgsConstructor;
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;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/home")
public class HomeControllerV1 implements HomeControllerV1Contract {

private final HomeService homeService;

@GetMapping()
@Override
@ResponseStatus(HttpStatus.OK)
public HomeResponseDto getHome() {
return homeService.getHome();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.universe.uni.controller.docs;

import com.universe.uni.dto.response.HomeResponseDto;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseStatus;

@Tag(
name = "Home",
description = "홈 화면에서 필요한 데이터 관련 API 입니다."
)
public interface HomeControllerContract {

@Operation(
summary = "홈 화면 조회",
description = "사용자는 홈에 필요한 정보를 얻을 수 있다.",
responses = {
@ApiResponse(
responseCode = "200",
description = "성공",
content = @Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(implementation = HomeResponseDto.class)
)
)
}
)
@GetMapping()
@ResponseStatus(HttpStatus.OK)
HomeResponseDto getHome();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.universe.uni.controller.docs;

import com.universe.uni.dto.response.HomeResponseDto;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseStatus;

@Tag(
name = "Home",
description = "홈 화면에서 필요한 데이터 관련 API 입니다."
)
public interface HomeControllerV1Contract {
@Operation(
summary = "홈 화면 조회",
description = "사용자는 홈에 필요한 정보를 얻을 수 있다.",
responses = {
@ApiResponse(
responseCode = "200",
description = "성공",
content = @Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(implementation = HomeResponseDto.class)
)
)
}
)
@GetMapping()
@ResponseStatus(HttpStatus.OK)
HomeResponseDto getHome();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@

import lombok.Builder;

@JsonPropertyOrder({"userId", "partnerId", "roundGameId", "myScore", "partnerScore", "drawCount", "dDay", "couple",
@JsonPropertyOrder({"userId", "partnerId","partnerNickname", "roundGameId", "myScore", "partnerScore", "drawCount", "dDay", "couple",
"shortGame"})
@Builder
public record HomeResponseDto(
Long userId,
Long partnerId,
String partnerNickname,
Long roundGameId,
int myScore,
int partnerScore,
Expand Down
95 changes: 48 additions & 47 deletions src/main/java/com/universe/uni/service/HomeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,61 +29,62 @@
@RequiredArgsConstructor
@Transactional
public class HomeService {
private final GameRepository gameRepository;
private final UserGameHistoryRepository userGameHistoryRepository;
private final RoundGameRepository roundGameRepository;
private final UserRepository userRepository;
private final UserUtil userUtil;
private final GameRepository gameRepository;
private final UserGameHistoryRepository userGameHistoryRepository;
private final RoundGameRepository roundGameRepository;
private final UserRepository userRepository;
private final UserUtil userUtil;

public HomeResponseDto getHome() {
User user = userUtil.getCurrentUser();
Couple couple = user.getCouple();
User partner = userRepository.findByCoupleIdAndIdNot(user.getCouple().getId(), user.getId());
public HomeResponseDto getHome() {
User user = userUtil.getCurrentUser();
Couple couple = user.getCouple();
User partner = userRepository.findByCoupleIdAndIdNot(user.getCouple().getId(), user.getId());

Game game = gameRepository.findByCoupleIdAndEnable(couple.getId(), true);
RoundGame roundGame = game != null ? roundGameRepository.findByGameId(game.getId()) : null;
Game game = gameRepository.findByCoupleIdAndEnable(couple.getId(), true);
RoundGame roundGame = game != null ? roundGameRepository.findByGameId(game.getId()) : null;

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

int myScore = calculateScore(gameHistoryList, GameResult.WIN);
int partnerScore = calculateScore(gameHistoryList, GameResult.LOSE);
int drawCount = calculateScore(gameHistoryList, GameResult.DRAW);
int myScore = calculateScore(gameHistoryList, GameResult.WIN);
int partnerScore = calculateScore(gameHistoryList, GameResult.LOSE);
int drawCount = calculateScore(gameHistoryList, GameResult.DRAW);

int dDay = calculateDays(couple);
int dDay = calculateDays(couple);

ShortGameDto shortGameDto = game instanceof ShortGame ? new ShortGameDto((ShortGame)game) : null;
CoupleDto coupleDto = fromCoupleToCoupleDtoMapper(couple);
ShortGameDto shortGameDto = game instanceof ShortGame ? new ShortGameDto((ShortGame) game) : null;
CoupleDto coupleDto = fromCoupleToCoupleDtoMapper(couple);

return HomeResponseDto.builder()
.userId(user.getId())
.partnerId(partner.getId())
.roundGameId(roundGame != null ? roundGame.getId() : null)
.myScore(myScore)
.partnerScore(partnerScore)
.drawCount(drawCount)
.dDay(dDay)
.couple(coupleDto)
.shortGame(shortGameDto)
.build();
}
return HomeResponseDto.builder()
.userId(user.getId())
.partnerId(partner.getId())
.partnerNickname(partner.getNickname())
.roundGameId(roundGame != null ? roundGame.getId() : null)
.myScore(myScore)
.partnerScore(partnerScore)
.drawCount(drawCount)
.dDay(dDay)
.couple(coupleDto)
.shortGame(shortGameDto)
.build();
}

private int calculateScore(List<UserGameHistory> gameHistoryList, GameResult result) {
return gameHistoryList != null
? (int)gameHistoryList.stream().filter(history -> history.getResult() == result).count()
: 0;
}
private int calculateScore(List<UserGameHistory> gameHistoryList, GameResult result) {
return gameHistoryList != null
? (int) gameHistoryList.stream().filter(history -> history.getResult() == result).count()
: 0;
}

private int calculateDays(Couple couple) {
ZonedDateTime localTime = ZonedDateTime.now(ZoneId.of("Asia/Seoul"));
long dDay = ChronoUnit.DAYS.between(couple.getStartDate(), localTime.toLocalDate());
return (int)dDay + 1;
}
private int calculateDays(Couple couple) {
ZonedDateTime localTime = ZonedDateTime.now(ZoneId.of("Asia/Seoul"));
long dDay = ChronoUnit.DAYS.between(couple.getStartDate(), localTime.toLocalDate());
return (int) dDay + 1;
}

private CoupleDto fromCoupleToCoupleDtoMapper(Couple couple) {
return CoupleDto.builder()
.id(couple.getId())
.startDate(String.valueOf(couple.getStartDate()))
.heartToken(couple.getHeartToken())
.build();
}
private CoupleDto fromCoupleToCoupleDtoMapper(Couple couple) {
return CoupleDto.builder()
.id(couple.getId())
.startDate(String.valueOf(couple.getStartDate()))
.heartToken(couple.getHeartToken())
.build();
}
}

0 comments on commit ec4c5c3

Please sign in to comment.