Skip to content
Snippets Groups Projects
Commit cabac1fa authored by Ján Dovjak's avatar Ján Dovjak
Browse files

feat: updateGame API

parent 731b8570
No related branches found
No related tags found
No related merge requests found
package cz.muni.fi.pa165.icehockeymanager.dto;
import lombok.Getter;
import lombok.Setter;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.PositiveOrZero;
public class GameUpdateDTO {
@NotNull
@Getter
@Setter
private Long id;
@PositiveOrZero
@Getter
@Setter
private int homeTeamScore;
@PositiveOrZero
@Getter
@Setter
private int awayTeamScore;
}
package cz.muni.fi.pa165.icehockeymanager.facades;
import cz.muni.fi.pa165.icehockeymanager.dto.GameCreateDTO;
import cz.muni.fi.pa165.icehockeymanager.dto.GameUpdateDTO;
import cz.muni.fi.pa165.icehockeymanager.dto.TeamCreateDTO;
/**
......@@ -24,11 +25,9 @@ public interface LeagueManagerFacade {
/**
* Updates the game score and also the winner if one is determined by the new score.
* @param gameId ID of the game to be updated.
* @param homeTeamScore the new score of the home team.
* @param awayTeamScore the new score of the away team.
* @param game DTO for updating game
*/
void updateGameScore(long gameId, int homeTeamScore, int awayTeamScore);
void updateGameScore(GameUpdateDTO game);
/**
* Sets the winner of a game.
......
......@@ -74,6 +74,11 @@ public class IceHockeyManagerApi {
leagueManagerFacade.scheduleGame(game);
}
@PutMapping("/game/update")
public final void updateGame(@Valid @RequestBody GameUpdateDTO game) {
leagueManagerFacade.updateGameScore(game);
}
@GetMapping("/game/team/{id}")
public final Collection<GameDTO> getGamesForTeam(@PathVariable Long id) {
return userFacade.getGamesForTeam(id);
......
package cz.muni.fi.pa165.icehockeymanager.facades;
import cz.muni.fi.pa165.icehockeymanager.dto.GameCreateDTO;
import cz.muni.fi.pa165.icehockeymanager.dto.GameUpdateDTO;
import cz.muni.fi.pa165.icehockeymanager.dto.TeamCreateDTO;
import cz.muni.fi.pa165.icehockeymanager.dto.TeamDTO;
import cz.muni.fi.pa165.icehockeymanager.exceptions.UnknownGameException;
......@@ -48,11 +49,11 @@ public class LeagueManagerFacadeImpl implements LeagueManagerFacade {
}
@Override
public void updateGameScore(long gameId, int homeTeamScore, int awayTeamScore) {
Game game = gameService.findGame(gameId).orElseThrow(
public void updateGameScore(GameUpdateDTO gameDTO) {
Game game = gameService.findGame(gameDTO.getId()).orElseThrow(
() -> new UnknownGameException("Game not found")
);
gameService.updateGameScore(game, homeTeamScore, awayTeamScore);
gameService.updateGameScore(game, gameDTO.getHomeTeamScore(), gameDTO.getAwayTeamScore());
}
@Override
......
......@@ -2,6 +2,7 @@ package cz.muni.fi.pa165.icehockeymanager.facades;
import cz.muni.fi.pa165.icehockeymanager.config.PersistanceApplicationConfig;
import cz.muni.fi.pa165.icehockeymanager.dto.GameCreateDTO;
import cz.muni.fi.pa165.icehockeymanager.dto.GameUpdateDTO;
import cz.muni.fi.pa165.icehockeymanager.dto.TeamCreateDTO;
import cz.muni.fi.pa165.icehockeymanager.dto.TeamDTO;
import cz.muni.fi.pa165.icehockeymanager.model.Game;
......@@ -71,6 +72,14 @@ public class LeagueManagerFacadeImplTest {
return gameCreateDTO;
}
public GameUpdateDTO buildGameUpdateDTO(Game game, int homeScore, int awayScore) {
var gameUpdate = new GameUpdateDTO();
gameUpdate.setId(game.getId());
gameUpdate.setAwayTeamScore(awayScore);
gameUpdate.setHomeTeamScore(homeScore);
return gameUpdate;
}
@Test
public void scheduleGame() {
var edmonton = buildTeamDTO(1L, "Edmonton Oilers");
......@@ -95,8 +104,9 @@ public class LeagueManagerFacadeImplTest {
@Test
public void updateGameScore() {
var game = buildGameMock(1L);
var gameUpdate = buildGameUpdateDTO(game, 1, 2);
when(gameService.findGame(1L)).thenReturn(Optional.ofNullable(game));
leagueManagerFacade.updateGameScore(1L, 1, 2);
leagueManagerFacade.updateGameScore(gameUpdate);
verify(gameService).updateGameScore(game, 1, 2);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment