1
0
Fork 0
mirror of https://codeberg.org/beerbrawl/beerbrawl.git synced 2024-09-23 01:30:52 +02:00

Merge branch 'feat/#156/add-check-if-table-with-name-already-exists-to-create-and-update' into 'development'

feat/#156/add-check-if-table-with-name-already-exists-to-create-and-update

See merge request 2024ss-se-pr-group/24ss-se-pr-qse-11!114
This commit is contained in:
Matthias Hofmarcher 2024-06-22 09:49:44 +00:00
commit 3b08c89cf9
4 changed files with 39 additions and 2 deletions

View file

@ -10,4 +10,21 @@ public interface BeerPongTableRepository extends JpaRepository<BeerPongTable, Lo
List<BeerPongTable> findAllByTournamentId(long tournamentId);
List<BeerPongTable> findByTournamentIdAndCurrentMatchIsNull(long tournamentId);
/**
* Check if a table with the given name already exists.
*
* @param name of the table
* @return wether the table with the given name exists
*/
Boolean existsByName(String name);
/**
* Check if a table with the given name already exists, whose id isn't the same as the parameters.
*
* @param name of the table
* @param id of the table that should't be matched
* @return wether the table with the given name exists
*/
Boolean existsByNameAndIdNot(String name, Long id);
}

View file

@ -52,6 +52,14 @@ public class BeerPongTableServiceImpl implements BeerPongTableService {
throw new AccessDeniedException("Current user isn't organizer of tournament.");
}
if (beerPongTableRepository.existsByName(beerPongTable.getName())) {
LOGGER.debug(
"Couldn't create beer pong table for tournament with id {}, because a table with the name '{}' already exists.",
beerPongTable.getTournamentId(),
beerPongTable.getName());
throw new PreconditionFailedException("Beer pong table with name already exists.");
}
var entity = new BeerPongTable(beerPongTable.getName(), tournament);
beerPongTableRepository.save(entity);
@ -85,6 +93,14 @@ public class BeerPongTableServiceImpl implements BeerPongTableService {
throw new AccessDeniedException("Current user isn't organizer of tournament.");
}
if (beerPongTableRepository.existsByNameAndIdNot(beerPongTable.getName(), beerPongTableId)) {
LOGGER.debug(
"Couldn't update beer pong table with id {}, because a table with the name '{}' already exists.",
beerPongTableId,
beerPongTable.getName());
throw new PreconditionFailedException("Beer pong table with name already exists.");
}
entity.setName(beerPongTable.getName());
beerPongTableRepository.save(entity);

View file

@ -85,7 +85,9 @@ export class BeerPongTableCreateComponent implements OnInit {
},
error: async (er: HttpErrorResponse) => {
this.snackBar.open(
`There was a problem creating the beer pong table: ${await er.error.text()}`,
typeof er.error === 'string'
? er.error
: `There was a problem creating the beer pong table: ${await er.error.text()}`,
'Close',
{ duration: 3000 },
);

View file

@ -89,7 +89,9 @@ export class BeerPongTableUpdateComponent implements OnInit {
},
error: async (er: HttpErrorResponse) => {
this.snackBar.open(
`There was a problem creating the beer pong table: ${await er.error.text()}`,
typeof er.error === 'string'
? er.error
: `There was a problem creating the beer pong table: ${await er.error.text()}`,
'Close',
{ duration: 3000 },
);