Skip to content

Commit

Permalink
feat: New Battle update logic
Browse files Browse the repository at this point in the history
  • Loading branch information
giovanni-orciuolo committed Feb 3, 2024
1 parent 3b7ba8c commit 1e77274
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import it.polimi.codekatabattle.exceptions.OAuthException;
import it.polimi.codekatabattle.models.dto.BattleDTO;
import it.polimi.codekatabattle.models.dto.BattleEntryDTO;
import it.polimi.codekatabattle.models.dto.BattleParticipantUpdateDTO;
import it.polimi.codekatabattle.models.dto.BattleUpdateDTO;
import it.polimi.codekatabattle.models.github.GHUser;
import it.polimi.codekatabattle.services.AuthService;
import it.polimi.codekatabattle.services.BattleService;
Expand Down Expand Up @@ -135,14 +137,35 @@ public ResponseEntity<Battle> leave(
)
public ResponseEntity<Battle> updateById(
@PathVariable("id") Long id,
@Valid @RequestBody BattleDTO battle,
@Valid @RequestBody BattleUpdateDTO battle,
@Parameter(hidden = true) @RequestHeader("Authorization") String accessToken,
@Parameter(hidden = true) @RequestHeader(value = "Origin", required = false) String origin
) throws OAuthException, EntityNotFoundException {
GHUser user = this.authService.getUserInfo(accessToken, this.authService.getAuthOriginFromOriginHeader(origin));
return ResponseEntity.ok().body(this.battleService.updateById(id, battle, user));
}

@PutMapping(
path = "/{battleId}/participant/{battleParticipantId}",
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE
)
@Operation(
summary = "Update battle",
description = "Update a battle participant by providing battle id, participant id and new battle participant data",
security = @SecurityRequirement(name = "github")
)
public ResponseEntity<Battle> updateBattleParticipantById(
@PathVariable("battleId") Long battleId,
@PathVariable("battleParticipantId") Long battleParticipantId,
@Valid @RequestBody BattleParticipantUpdateDTO battleParticipant,
@Parameter(hidden = true) @RequestHeader("Authorization") String accessToken,
@Parameter(hidden = true) @RequestHeader(value = "Origin", required = false) String origin
) throws OAuthException, EntityNotFoundException {
GHUser user = this.authService.getUserInfo(accessToken, this.authService.getAuthOriginFromOriginHeader(origin));
return ResponseEntity.ok().body(this.battleService.updateBattleParticipantById(battleId, battleParticipantId, battleParticipant, user));
}

@DeleteMapping(
path = "/{id}",
produces = MediaType.APPLICATION_JSON_VALUE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public class BattleParticipant extends BaseEntity {
private String username;

@Column
private float score = 0;
private Integer score = null;

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public class TournamentParticipant extends BaseEntity {
private String username;

@Column
private float score;
private Integer score;

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public class BattleDTO {
@NotBlank
private String description;

@Future
@JsonFormat(pattern=DATETIME_FORMAT)
private LocalDateTime startsAt;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package it.polimi.codekatabattle.models.dto;

import jakarta.validation.constraints.PositiveOrZero;
import lombok.Data;

@Data
public class BattleParticipantUpdateDTO {

@PositiveOrZero
private Integer score;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package it.polimi.codekatabattle.models.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

import java.time.LocalDateTime;

import static it.polimi.codekatabattle.config.APIConstants.DATETIME_FORMAT;

@Data
public class BattleUpdateDTO {

@JsonFormat(pattern=DATETIME_FORMAT)
private LocalDateTime startsAt;

@JsonFormat(pattern=DATETIME_FORMAT)
private LocalDateTime endsAt;

private Boolean enableManualEvaluation = false;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package it.polimi.codekatabattle.repositories;

import it.polimi.codekatabattle.entities.BattleParticipant;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface BattleParticipantRepository extends JpaRepository<BattleParticipant, Long> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import it.polimi.codekatabattle.entities.BattleEntry;
import it.polimi.codekatabattle.models.dto.BattleDTO;
import it.polimi.codekatabattle.models.dto.BattleEntryDTO;
import it.polimi.codekatabattle.models.dto.BattleParticipantUpdateDTO;
import it.polimi.codekatabattle.models.dto.BattleUpdateDTO;
import it.polimi.codekatabattle.models.github.GHUser;
import jakarta.persistence.EntityNotFoundException;
import jakarta.validation.ValidationException;
Expand All @@ -20,7 +22,9 @@ public interface BattleService extends CrudService<Battle> {

Battle leave(Long battleId, GHUser user) throws EntityNotFoundException, ValidationException;

Battle updateById(Long battleId, BattleDTO battle, GHUser updater) throws EntityNotFoundException, ValidationException;
Battle updateById(Long battleId, BattleUpdateDTO battle, GHUser updater) throws EntityNotFoundException, ValidationException;

Battle updateBattleParticipantById(Long battleId, Long battleParticipantId, BattleParticipantUpdateDTO battleParticipantUpdate, GHUser updater) throws EntityNotFoundException, ValidationException;

Battle deleteById(Long battleId, GHUser deleter) throws EntityNotFoundException, ValidationException, IOException;

Expand All @@ -29,4 +33,5 @@ public interface BattleService extends CrudService<Battle> {
void deleteBattleRepository(Battle battle) throws IOException;

BattleEntry submit(Long battleId, BattleEntryDTO battleEntry, GitHub github) throws EntityNotFoundException, ValidationException, IOException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
import it.polimi.codekatabattle.models.BattleTest;
import it.polimi.codekatabattle.models.dto.BattleDTO;
import it.polimi.codekatabattle.models.dto.BattleEntryDTO;
import it.polimi.codekatabattle.models.dto.BattleParticipantUpdateDTO;
import it.polimi.codekatabattle.models.dto.BattleUpdateDTO;
import it.polimi.codekatabattle.models.github.GHUser;
import it.polimi.codekatabattle.repositories.BattleEntryRepository;
import it.polimi.codekatabattle.repositories.BattleParticipantRepository;
import it.polimi.codekatabattle.repositories.BattleRepository;
import it.polimi.codekatabattle.services.BattleService;
import it.polimi.codekatabattle.services.ScoreService;
Expand All @@ -32,15 +35,18 @@ public class BattleServiceImpl extends CrudServiceImpl<Battle> implements Battle

private final BattleRepository battleRepository;

private final BattleParticipantRepository battleParticipantRepository;

private final BattleEntryRepository battleEntryRepository;

private final TournamentService tournamentService;

private final ScoreService scoreService;

public BattleServiceImpl(BattleRepository battleRepository, BattleEntryRepository battleEntryRepository, TournamentService tournamentService, ScoreService scoreService) {
public BattleServiceImpl(BattleRepository battleRepository, BattleParticipantRepository battleParticipantRepository, BattleEntryRepository battleEntryRepository, TournamentService tournamentService, ScoreService scoreService) {
super(battleRepository);
this.battleRepository = battleRepository;
this.battleParticipantRepository = battleParticipantRepository;
this.battleEntryRepository = battleEntryRepository;
this.tournamentService = tournamentService;
this.scoreService = scoreService;
Expand Down Expand Up @@ -124,25 +130,49 @@ public Battle leave(Long battleId, GHUser user) throws EntityNotFoundException,

@Override
@Transactional
public Battle updateById(Long battleId, BattleDTO battle, GHUser updater) throws EntityNotFoundException, ValidationException {
public Battle updateById(@NotNull Long battleId, @NotNull BattleUpdateDTO battle, @NotNull GHUser updater) throws EntityNotFoundException, ValidationException {
Battle battleToUpdate = this.findById(battleId)
.orElseThrow(() -> new EntityNotFoundException("Battle not found by id " + battleId));

if (!battleToUpdate.getCreator().equals(updater.getLogin())) {
throw new ValidationException("Only the creator of the battle can update it");
}
if (battleToUpdate.hasStarted()) {
throw new ValidationException("Battle has already started, can't be updated");

battleToUpdate.setStartsAt(battle.getStartsAt());
battleToUpdate.setEndsAt(battle.getEndsAt());
battleToUpdate.setEnableManualEvaluation(battle.getEnableManualEvaluation());

return this.save(battleToUpdate);
}

@Override
@Transactional
public Battle updateBattleParticipantById(
@NotNull Long battleId,
@NotNull Long battleParticipantId,
@NotNull BattleParticipantUpdateDTO battleParticipantUpdate,
@NotNull GHUser updater
) throws EntityNotFoundException, ValidationException {
Battle battleToUpdate = this.findById(battleId)
.orElseThrow(() -> new EntityNotFoundException("Battle not found by id " + battleId));

if (!battleToUpdate.getCreator().equals(updater.getLogin())) {
throw new ValidationException("Only the creator of the battle can update it");
}
if (battleToUpdate.hasEnded()) {
throw new ValidationException("Battle has ended, can't be updated");
}
if (!battleToUpdate.getEnableManualEvaluation()) {
throw new ValidationException("Manual evaluation is not enabled for this battle");
}

battleToUpdate.setTitle(battle.getTitle());
battleToUpdate.setDescription(battle.getDescription());
battleToUpdate.setStartsAt(battle.getStartsAt());
battleToUpdate.setEndsAt(battle.getEndsAt());
battleToUpdate.setLanguage(battle.getLanguage());
BattleParticipant participantToUpdate = battleToUpdate.getParticipants().stream()
.filter(p -> p.getId().equals(battleParticipantId))
.findFirst()
.orElseThrow(() -> new EntityNotFoundException("Battle participant not found by id " + battleParticipantId));

participantToUpdate.setScore(participantToUpdate.getScore() + battleParticipantUpdate.getScore());
this.battleParticipantRepository.save(participantToUpdate);

return this.save(battleToUpdate);
}
Expand Down Expand Up @@ -228,7 +258,6 @@ public BattleEntry submit(Long battleId, BattleEntryDTO battleEntry, GitHub gith
be.setBattle(battle);
be.setParticipant(participant);
be.setStatus(BattleEntryStatus.QUEUED);
be.setScore(0);
this.battleEntryRepository.save(be);

this.scoreService.processBattleEntry(be, URI.create(battleEntry.getArtifactUrl()).toURL());
Expand Down

0 comments on commit 1e77274

Please sign in to comment.