forked from codesquad-members-2021/baseball
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from isaac56/feature/BE/implements-basic-api
[BE] 기본 API 구현 완료
- Loading branch information
Showing
36 changed files
with
898 additions
and
95 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
BE/baseball/src/main/java/team9/baseball/DTO/request/CreateGameDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package team9.baseball.DTO.request; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class CreateGameDTO { | ||
private int away_team_id; | ||
private int home_team_id; | ||
} |
16 changes: 16 additions & 0 deletions
16
BE/baseball/src/main/java/team9/baseball/DTO/request/JoinGameDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package team9.baseball.DTO.request; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import team9.baseball.domain.enums.Venue; | ||
|
||
import javax.validation.constraints.NotNull; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class JoinGameDTO { | ||
private int game_id; | ||
|
||
@NotNull | ||
private Venue my_venue; | ||
} |
13 changes: 13 additions & 0 deletions
13
BE/baseball/src/main/java/team9/baseball/DTO/request/PitchResultDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package team9.baseball.DTO.request; | ||
|
||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import team9.baseball.domain.enums.PitchResult; | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
public class PitchResultDTO { | ||
private PitchResult pitch_result; | ||
} |
36 changes: 36 additions & 0 deletions
36
BE/baseball/src/main/java/team9/baseball/DTO/response/ApiResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package team9.baseball.DTO.response; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
public class ApiResult<T> { | ||
private T data; | ||
private String error; | ||
|
||
private ApiResult(T data, String error) { | ||
this.data = data; | ||
this.error = error; | ||
} | ||
|
||
public static <T> ApiResult<T> succeed(T data) { | ||
return new ApiResult(data, null); | ||
} | ||
|
||
public static ApiResult<?> failed(String errorMessage) { | ||
return new ApiResult<>(null, errorMessage); | ||
} | ||
|
||
public static ApiResult<?> failed(Throwable throwable) { | ||
return failed(throwable.getMessage()); | ||
} | ||
|
||
public T getData() { | ||
return data; | ||
} | ||
|
||
public String getError() { | ||
return error; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
BE/baseball/src/main/java/team9/baseball/DTO/response/BattingHistoryDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package team9.baseball.DTO.response; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import team9.baseball.domain.aggregate.game.BattingHistory; | ||
import team9.baseball.domain.aggregate.team.Team; | ||
|
||
@Getter | ||
@Builder(access = AccessLevel.PRIVATE) | ||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class BattingHistoryDTO { | ||
private int uniform_number; | ||
private String name; | ||
private int appear_count; | ||
private int hit_count; | ||
private double hit_ratio; | ||
private boolean playing; | ||
|
||
|
||
public static BattingHistoryDTO of(Team team, BattingHistory battingHistory, int playingUniformNumber) { | ||
double hitRatio = 0; | ||
if (battingHistory.getAppear() != 0) { | ||
hitRatio = (double) battingHistory.getHits() / (double) battingHistory.getAppear(); | ||
} | ||
|
||
return builder() | ||
.uniform_number(battingHistory.getBatterUniformNumber()) | ||
.name(team.getPlayerName(battingHistory.getBatterUniformNumber())) | ||
.appear_count(battingHistory.getAppear()) | ||
.hit_count(battingHistory.getHits()) | ||
.hit_ratio(hitRatio) | ||
.playing(battingHistory.getBatterUniformNumber() == playingUniformNumber) | ||
.build(); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
BE/baseball/src/main/java/team9/baseball/DTO/response/GameDescriptionDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package team9.baseball.DTO.response; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@AllArgsConstructor | ||
public class GameDescriptionDTO { | ||
private Long id; | ||
private String awayTeam; | ||
private String homeTeam; | ||
private String awayUserEmail; | ||
private String homeUserEmail; | ||
} |
56 changes: 56 additions & 0 deletions
56
BE/baseball/src/main/java/team9/baseball/DTO/response/GameHistoryDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package team9.baseball.DTO.response; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import team9.baseball.domain.aggregate.game.Game; | ||
import team9.baseball.domain.aggregate.game.Inning; | ||
import team9.baseball.domain.aggregate.team.Team; | ||
import team9.baseball.domain.enums.Halves; | ||
|
||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Getter | ||
@Builder(access = AccessLevel.PRIVATE) | ||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class GameHistoryDTO { | ||
private TeamHistoryDTO away_team; | ||
private TeamHistoryDTO home_team; | ||
|
||
public static GameHistoryDTO of(Game game, Team awayTeam, Team homeTeam) { | ||
List<Integer> awayScores = acquireScores(game, Halves.TOP); | ||
List<Integer> homeScores = acquireScores(game, Halves.BOTTOM); | ||
|
||
int awayPlayingUniformNumber = game.getCurrentHalves() == Halves.TOP ? | ||
game.getBatterUniformNumber() : game.getPitcherUniformNumber(); | ||
int homePlayingUniformNumber = game.getCurrentHalves() == Halves.BOTTOM ? | ||
game.getBatterUniformNumber() : game.getPitcherUniformNumber(); | ||
|
||
List<BattingHistoryDTO> awayBattingHistories = acquireBattingHistoryDTOList(game, awayTeam, awayPlayingUniformNumber); | ||
List<BattingHistoryDTO> homeBattingHistories = acquireBattingHistoryDTOList(game, homeTeam, homePlayingUniformNumber); | ||
|
||
return builder() | ||
.away_team(TeamHistoryDTO.of(awayTeam.getName(), awayScores, awayBattingHistories)) | ||
.home_team(TeamHistoryDTO.of(homeTeam.getName(), homeScores, homeBattingHistories)) | ||
.build(); | ||
} | ||
|
||
private static List<Integer> acquireScores(Game game, Halves halves) { | ||
return game.getInningMap().values().stream(). | ||
filter(x -> x.getHalves() == halves) | ||
.sorted(Comparator.comparingInt(Inning::getInning)) | ||
.map(x -> x.getScore()) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private static List<BattingHistoryDTO> acquireBattingHistoryDTOList(Game game, Team team, int playingUniformNumber) { | ||
return game.getBattingHistoryMap().values().stream() | ||
.filter(x -> x.getBatterTeamId() == team.getId()) | ||
.map(x -> BattingHistoryDTO.of(team, x, playingUniformNumber)) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
} |
116 changes: 116 additions & 0 deletions
116
BE/baseball/src/main/java/team9/baseball/DTO/response/GameStatusDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package team9.baseball.DTO.response; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import team9.baseball.domain.aggregate.game.BattingHistory; | ||
import team9.baseball.domain.aggregate.game.Game; | ||
import team9.baseball.domain.aggregate.game.Inning; | ||
import team9.baseball.domain.aggregate.team.Team; | ||
import team9.baseball.domain.enums.Halves; | ||
import team9.baseball.domain.enums.Venue; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Getter | ||
@Builder(access = AccessLevel.PRIVATE) | ||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class GameStatusDTO { | ||
private int strike; | ||
|
||
private int ball; | ||
|
||
private int out; | ||
|
||
private TeamDTO away_team; | ||
|
||
private TeamDTO home_team; | ||
|
||
private String inning; | ||
|
||
private String halves; | ||
|
||
private PlayerDTO pitcher; | ||
|
||
private String pitcher_status; | ||
|
||
private PlayerDTO batter; | ||
|
||
private String batter_status; | ||
|
||
private PlayerDTO base1; | ||
|
||
private PlayerDTO base2; | ||
|
||
private PlayerDTO base3; | ||
|
||
private List<PitchHistoryDTO> pitch_histories; | ||
|
||
private String my_role; | ||
|
||
public static GameStatusDTO of(Game game, Team awayTeam, Team homeTeam, Venue userVenue) { | ||
Team attackTeam = game.acquireAttackTeam(awayTeam, homeTeam); | ||
Team defenseTeam = game.acquireDefenseTeam(awayTeam, homeTeam); | ||
|
||
TeamDTO awayTeamDTO = TeamDTO.of(awayTeam.getName(), game.getTotalScore(Halves.TOP), | ||
acquireCurrentRole(game.getCurrentHalves(), Halves.TOP)); | ||
TeamDTO homeTeamDTO = TeamDTO.of(homeTeam.getName(), game.getTotalScore(Halves.BOTTOM), | ||
acquireCurrentRole(game.getCurrentHalves(), Halves.BOTTOM)); | ||
|
||
|
||
BattingHistory batterHistory = game.acquireBattingHistory(attackTeam.getId(), game.getBatterUniformNumber()); | ||
String batterStatus = acquireBatterStatus(batterHistory); | ||
String pitcherStatus = acquirePitcherStatus(game, defenseTeam.getId(), game.getPitcherUniformNumber()); | ||
|
||
String myRole = userVenue.getHalves() == game.getCurrentHalves() ? "ATTACK" : "DEFENSE"; | ||
|
||
return builder() | ||
.strike(game.getStrikeCount()) | ||
.ball(game.getBallCount()) | ||
.out(game.getOutCount()) | ||
.away_team(awayTeamDTO) | ||
.home_team(homeTeamDTO) | ||
.inning(game.getCurrentInning().toString()) | ||
.halves(game.getCurrentHalves().name()) | ||
.pitcher(PlayerDTO.of(defenseTeam, game.getPitcherUniformNumber())) | ||
.pitcher_status(pitcherStatus) | ||
.batter(PlayerDTO.of(attackTeam, game.getBatterUniformNumber())) | ||
.batter_status(batterStatus) | ||
.base1(PlayerDTO.of(attackTeam, game.getBase1UniformNumber())) | ||
.base2(PlayerDTO.of(attackTeam, game.getBase2UniformNumber())) | ||
.base3(PlayerDTO.of(attackTeam, game.getBase3UniformNumber())) | ||
.pitch_histories(acquirePitchHistories(attackTeam, defenseTeam, game.acquireCurrentInning())) | ||
.my_role(myRole) | ||
.build(); | ||
} | ||
|
||
private static String acquireBatterStatus(BattingHistory batterHistory) { | ||
return String.format("%d타석 %d안타", batterHistory.getAppear(), batterHistory.getHits()); | ||
} | ||
|
||
private static String acquirePitcherStatus(Game game, int pitcherTeamId, int pitcherUniformNumber) { | ||
long pitcherCount = game.getInningMap().values().stream() | ||
.flatMap(inning -> inning.getPitchHistoryList().stream()) | ||
.filter(pitchHistory -> pitchHistory.getPitcherTeamId() == pitcherTeamId && | ||
pitchHistory.getPitcherUniformNumber() == pitcherUniformNumber) | ||
.count(); | ||
|
||
return "#" + pitcherCount; | ||
} | ||
|
||
private static List<PitchHistoryDTO> acquirePitchHistories(Team attackTeam, Team defenseTeam, Inning inning) { | ||
return inning.getPitchHistoryList().stream(). | ||
map(pitchHistory -> PitchHistoryDTO.of(attackTeam, defenseTeam, pitchHistory)). | ||
collect(Collectors.toList()); | ||
} | ||
|
||
private static String acquireCurrentRole(Halves currentHalves, Halves halves) { | ||
if (currentHalves == halves) { | ||
return "ATTACK"; | ||
} | ||
return "DEFENSE"; | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
BE/baseball/src/main/java/team9/baseball/DTO/response/PitchHistoryDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package team9.baseball.DTO.response; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import team9.baseball.domain.aggregate.game.PitchHistory; | ||
import team9.baseball.domain.aggregate.team.Team; | ||
|
||
@Getter | ||
@Builder(access = AccessLevel.PRIVATE) | ||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class PitchHistoryDTO { | ||
private PlayerDTO pitcher; | ||
private PlayerDTO batter; | ||
private String result; | ||
private int strike_count; | ||
private int ball_count; | ||
|
||
public static PitchHistoryDTO of(Team attackTeam, Team defenseTeam, PitchHistory pitchHistory) { | ||
return builder() | ||
.pitcher(PlayerDTO.of(defenseTeam, pitchHistory.getPitcherUniformNumber())) | ||
.batter(PlayerDTO.of(attackTeam, pitchHistory.getBatterUniformNumber())) | ||
.result(pitchHistory.getResult().name()) | ||
.strike_count(pitchHistory.getStrikeCount()) | ||
.ball_count(pitchHistory.getBallCount()) | ||
.build(); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
BE/baseball/src/main/java/team9/baseball/DTO/response/PlayerDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package team9.baseball.DTO.response; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import team9.baseball.domain.aggregate.team.Team; | ||
|
||
@Getter | ||
@Builder(access = AccessLevel.PRIVATE) | ||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class PlayerDTO { | ||
private int team_id; | ||
private int uniform_number; | ||
private String name; | ||
|
||
public static PlayerDTO of(Team team, Integer uniform_number) { | ||
if (uniform_number == null) { | ||
return null; | ||
} | ||
|
||
return builder() | ||
.team_id(team.getId()) | ||
.uniform_number(uniform_number) | ||
.name(team.getPlayerName(uniform_number)) | ||
.build(); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
BE/baseball/src/main/java/team9/baseball/DTO/response/TeamDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package team9.baseball.DTO.response; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@Builder(access = AccessLevel.PRIVATE) | ||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class TeamDTO { | ||
private String name; | ||
private int score; | ||
private String role; | ||
|
||
public static TeamDTO of(String name, int score, String role) { | ||
return builder() | ||
.name(name) | ||
.score(score) | ||
.role(role) | ||
.build(); | ||
} | ||
} |
Oops, something went wrong.