Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

경기 조회 API #151

Merged
merged 17 commits into from
Apr 16, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package team.gsmgogo.domain.match.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import team.gsmgogo.domain.match.controller.dto.response.MatchResponse;
import team.gsmgogo.domain.match.service.MatchService;

@RestController
@RequiredArgsConstructor
@RequestMapping("/match")
public class MatchController {
private final MatchService matchService;

@GetMapping
public ResponseEntity<MatchResponse> getMatches(
@RequestParam("m") Integer month,
@RequestParam("d") Integer day
){
return ResponseEntity.ok(matchService.execute(month, day));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package team.gsmgogo.domain.match.controller.dto.response;

import java.time.LocalDateTime;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import team.gsmgogo.domain.match.enums.MatchLevelType;
import team.gsmgogo.domain.match.enums.TeamClassType;
import team.gsmgogo.domain.team.enums.TeamType;
import team.gsmgogo.domain.user.enums.GradeEnum;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy.class)
public class MatchInfoDto {
private Long matchId;

private TeamType matchType;

private MatchLevelType matchLevel;

@JsonProperty("team_a_id")
private Long teamAId;

@JsonProperty("team_a_name")
private String teamAName;

@JsonProperty("team_a_grade")
private GradeEnum teamAGrade;

@JsonProperty("team_a_class_type")
private TeamClassType teamAClassType;

@JsonProperty("team_b_id")
private Long teamBId;

@JsonProperty("team_b_name")
private String teamBName;

@JsonProperty("team_b_grade")
private GradeEnum teamBGrade;

@JsonProperty("team_b_class_type")
private TeamClassType teamBClassType;

@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
private LocalDateTime matchStartAt;

@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
private LocalDateTime matchEndAt;

@JsonProperty("is_vote")
private boolean isVote;

@JsonProperty("team_a_bet")
private Long teamABet;

@JsonProperty("team_b_bet")
private Long teamBBet;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package team.gsmgogo.domain.match.controller.dto.response;

import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;

@Getter
@NoArgsConstructor
@AllArgsConstructor
@JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy.class)
public class MatchResponse {
private List<MatchInfoDto> matches;
private List<MatchResultDto> matchResult;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package team.gsmgogo.domain.match.controller.dto.response;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import team.gsmgogo.domain.match.enums.MatchLevelType;
import team.gsmgogo.domain.match.enums.TeamClassType;
import team.gsmgogo.domain.team.enums.TeamType;
import team.gsmgogo.domain.user.enums.GradeEnum;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy.class)
public class MatchResultDto {
private Long matchId;

private TeamType matchType;

private MatchLevelType matchLevel;

@JsonProperty("team_a_id")
private Long teamAId;

@JsonProperty("team_a_name")
private String teamAName;

@JsonProperty("team_a_grade")
private GradeEnum teamAGrade;

@JsonProperty("team_a_class_type")
private TeamClassType teamAClassType;

@JsonProperty("team_b_id")
private Long teamBId;

@JsonProperty("team_b_name")
private String teamBName;

@JsonProperty("team_b_grade")
private GradeEnum teamBGrade;

@JsonProperty("team_b_class_type")
private TeamClassType teamBClassType;

@JsonProperty("is_vote")
private boolean isVote;

@JsonProperty("team_a_bet")
private Long teamABet;

@JsonProperty("team_b_bet")
private Long teamBBet;

@JsonProperty("team_a_score")
private Integer teamAScore;

@JsonProperty("team_b_score")
private Integer teamBScore;

@JsonProperty("bet_team_a_score")
private Integer betTeamAScore;

@JsonProperty("bet_team_b_score")
private Integer betTeamBScore;

private Long earnedPoint;

private Long losePoint;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package team.gsmgogo.domain.match.service;

import team.gsmgogo.domain.match.controller.dto.response.MatchResponse;

public interface MatchService {
MatchResponse execute(int month, int day);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package team.gsmgogo.domain.match.service.impl;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

import org.springframework.stereotype.Service;

import lombok.RequiredArgsConstructor;
import team.gsmgogo.domain.bet.entity.BetEntity;
import team.gsmgogo.domain.bet.repository.BetJpaRepository;
import team.gsmgogo.domain.match.controller.dto.response.MatchInfoDto;
import team.gsmgogo.domain.match.controller.dto.response.MatchResponse;
import team.gsmgogo.domain.match.controller.dto.response.MatchResultDto;
import team.gsmgogo.domain.match.entity.MatchEntity;
import team.gsmgogo.domain.match.repository.MatchQueryDslRepository;
import team.gsmgogo.domain.match.service.MatchService;
import team.gsmgogo.domain.matchresult.entity.MatchResultEntity;
import team.gsmgogo.domain.matchresult.repository.MatchResultQueryDslRepository;
import team.gsmgogo.domain.user.entity.UserEntity;
import team.gsmgogo.global.common.CalculatePoint;
import team.gsmgogo.global.common.CalculatePointRequest;
import team.gsmgogo.global.common.CalculatePointResponse;
import team.gsmgogo.global.facade.UserFacade;

@Service
@RequiredArgsConstructor
public class MatchServiceImpl implements MatchService {
private final MatchQueryDslRepository matchQueryDslRepository;
private final MatchResultQueryDslRepository matchResultQueryDslRepository;
private final BetJpaRepository betJpaRepository;
private final UserFacade userFacade;

@Override
public MatchResponse execute(int month, int day) {
UserEntity currentUser = userFacade.getCurrentUser();
List<MatchEntity> matches = matchQueryDslRepository.findByMonthAndDay(month, day);
List<MatchResultEntity> matchResults = matchResultQueryDslRepository.findByMonthAndDay(month, day);
Comment on lines +36 to +38
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

경시 시작 시간, 끝나는 시간 값을 이용하여

matches: 경기 시작 전 or 경기 중인 경기
matchResults: 경기 시간이 끝난 경기

이렇게 조회되도록 변경 부탁드려요! 현재 상황은 경기 시간이 끝난 경기가 하나 있는데 matchs와 matchResults 모두 하나씩 나옵니다!

시간을 계산하여 한 쪽에서만 나오도록 변경 부탁드려요!

List<BetEntity> bettings = betJpaRepository.findByUser(currentUser);

List<MatchInfoDto> matchList = matches.stream()
.map(match -> MatchInfoDto.builder()
.matchId(match.getMatchId())
.matchType(match.getMatchType())
.matchLevel(match.getMatchLevel())
.teamAId(match.getTeamA().getTeamId())
.teamAName(match.getTeamA().getTeamName())
.teamAGrade(match.getTeamAGrade())
.teamAClassType(match.getTeamAClassType())
.teamBId(match.getTeamB().getTeamId())
.teamBName(match.getTeamB().getTeamName())
.teamBGrade(match.getTeamBGrade())
.teamBClassType(match.getTeamBClassType())
.matchStartAt(match.getStartAt())
.matchEndAt(match.getEndAt())
.isVote(bettings.stream().anyMatch(bet -> bet.getMatch() == match))
.teamABet(match.getTeamABet())
.teamBBet(match.getTeamBBet())
.build()).toList();

List<MatchResultDto> endedMatches = matchResults.stream()
.map(matchResult -> {
MatchEntity match = matchResult.getMatch();
BetEntity betting = bettings.stream()
.filter(bet -> bet.getMatch() == match)
.findFirst().orElse(null);

CalculatePointResponse calculatePoint = new CalculatePointResponse();

if (betting != null){
CalculatePointRequest request = new CalculatePointRequest(
betting.getBetPoint(),
matchResult.getTeamAScore(),
matchResult.getTeamBScore(),
betting.getBetScoreA(),
betting.getBetScoreB(),
match.getTeamABet(),
match.getTeamBBet()
);
calculatePoint = new CalculatePoint().execute(request);
}

return MatchResultDto.builder()
.matchId(match.getMatchId())
.matchType(match.getMatchType())
.matchLevel(match.getMatchLevel())
.teamAId(match.getTeamA().getTeamId())
.teamAName(match.getTeamA().getTeamName())
.teamAGrade(match.getTeamAGrade())
.teamAClassType(match.getTeamAClassType())
.teamBId(match.getTeamB().getTeamId())
.teamBName(match.getTeamB().getTeamName())
.teamBGrade(match.getTeamBGrade())
.teamBClassType(match.getTeamBClassType())
.isVote(betting != null)
.teamABet(match.getTeamABet())
.teamBBet(match.getTeamBBet())
.teamAScore(matchResult.getTeamAScore())
.teamBScore(matchResult.getTeamBScore())
.earnedPoint(calculatePoint.getEarnedPoint())
.losePoint(calculatePoint.getLosePoint())
.betTeamAScore(betting != null ? betting.getBetScoreA() : null)
.betTeamBScore(betting != null ? betting.getBetScoreB() : null)
.build();
}).toList();

return new MatchResponse(matchList, endedMatches);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package team.gsmgogo.global.common;

public class CalculatePoint {
public CalculatePointResponse execute(CalculatePointRequest req){
CalculatePointResponse response = null;

long winTeamPoint = Math.max(req.getAllAPoint(), req.getAllBPoint());
long loseTeamPoint = Math.min(req.getAllAPoint(), req.getAllBPoint());

// 스코어 예측 성공
if (req.getAScore() == req.getBetAScore() && req.getBScore() == req.getBetBScore()){
Long pointResult = (long) Math.ceil(((req.getBetPoint() * ((double) loseTeamPoint / winTeamPoint)) + req.getBetPoint()) * 1.5);
response = new CalculatePointResponse(pointResult, null);
}

// 승부 예측 성공
if ((req.getAScore() > req.getBScore() && req.getBetAScore() > req.getBetBScore()) ||
(req.getAScore() < req.getBScore() && req.getBetAScore() < req.getBetBScore())){
Long pointResult = (long) Math.ceil(((req.getBetPoint() * ((double) loseTeamPoint / winTeamPoint)) + req.getBetPoint()) * 0.9);
response = new CalculatePointResponse(pointResult, null);
}

// 예측 실패
if (response == null) {
response = new CalculatePointResponse(null, req.getBetPoint());
}

return response;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package team.gsmgogo.global.common;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class CalculatePointRequest {
private Long betPoint;
private int aScore;
private int bScore;
private int betAScore;
private int betBScore;
private Long allAPoint;
private Long allBPoint;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package team.gsmgogo.global.common;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class CalculatePointResponse {
private Long earnedPoint = null;
private Long losePoint = null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

import java.util.Optional;

public interface BlackListJpaRepository extends CrudRepository<BlackListRedisEntity, String> {
public interface BlackListJpaRepository extends CrudRepository<BlackListRedisEntity, Long> {
Optional<BlackListRedisEntity> findByAccessToken(String accessToken);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package team.gsmgogo.domain.bet.repository;

import java.util.List;

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

import team.gsmgogo.domain.bet.entity.BetEntity;
import team.gsmgogo.domain.match.entity.MatchEntity;
import team.gsmgogo.domain.user.entity.UserEntity;
import team.gsmgogo.domain.match.entity.MatchEntity;

public interface BetJpaRepository extends JpaRepository<BetEntity, Long> {
List<BetEntity> findByUser(UserEntity user);
boolean existsByUserAndMatch(UserEntity user, MatchEntity matchEntity);
}
Loading
Loading