diff --git a/backend/src/main/java/at/ac/tuwien/sepr/groupphase/backend/repository/BeerPongTableRepository.java b/backend/src/main/java/at/ac/tuwien/sepr/groupphase/backend/repository/BeerPongTableRepository.java index 43dbd7e..8d9ec2d 100644 --- a/backend/src/main/java/at/ac/tuwien/sepr/groupphase/backend/repository/BeerPongTableRepository.java +++ b/backend/src/main/java/at/ac/tuwien/sepr/groupphase/backend/repository/BeerPongTableRepository.java @@ -10,4 +10,21 @@ public interface BeerPongTableRepository extends JpaRepository findAllByTournamentId(long tournamentId); List 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); } diff --git a/backend/src/main/java/at/ac/tuwien/sepr/groupphase/backend/service/impl/BeerPongTableServiceImpl.java b/backend/src/main/java/at/ac/tuwien/sepr/groupphase/backend/service/impl/BeerPongTableServiceImpl.java index b0e7f87..2837209 100644 --- a/backend/src/main/java/at/ac/tuwien/sepr/groupphase/backend/service/impl/BeerPongTableServiceImpl.java +++ b/backend/src/main/java/at/ac/tuwien/sepr/groupphase/backend/service/impl/BeerPongTableServiceImpl.java @@ -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); diff --git a/frontend/src/app/components/beer-pong-table-create/beer-pong-table-create.component.ts b/frontend/src/app/components/beer-pong-table-create/beer-pong-table-create.component.ts index 617538b..470f341 100644 --- a/frontend/src/app/components/beer-pong-table-create/beer-pong-table-create.component.ts +++ b/frontend/src/app/components/beer-pong-table-create/beer-pong-table-create.component.ts @@ -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 }, ); diff --git a/frontend/src/app/components/beer-pong-table-update/beer-pong-table-update.component.ts b/frontend/src/app/components/beer-pong-table-update/beer-pong-table-update.component.ts index 31fe7bb..bbc46f3 100644 --- a/frontend/src/app/components/beer-pong-table-update/beer-pong-table-update.component.ts +++ b/frontend/src/app/components/beer-pong-table-update/beer-pong-table-update.component.ts @@ -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 }, );