From e03a719912189c99c306431b4bbd7faea9f0f40d Mon Sep 17 00:00:00 2001 From: Software Magico Date: Sun, 12 May 2024 17:04:33 +0200 Subject: [PATCH] Setting teams on group removes also unties. --- README.md | 2 +- .../kt/core/controller/GroupController.java | 48 ++++++++++--------- .../kt/core/providers/GroupProvider.java | 10 +++- 3 files changed, 34 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 471bb7fcb..9a28fca93 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ [![GitHub commit activity](https://img.shields.io/github/commit-activity/y/softwaremagico/KendoTournamentManager)](https://github.com/softwaremagico/KendoTournamentManager) [![GitHub last commit](https://img.shields.io/github/last-commit/softwaremagico/KendoTournamentManager)](https://github.com/softwaremagico/KendoTournamentManager) [![CircleCI](https://circleci.com/gh/softwaremagico/KendoTournamentManager.svg?style=shield)](https://circleci.com/gh/softwaremagico/KendoTournamentManager) -[![Time](https://img.shields.io/badge/development-615h-blueviolet.svg)]() +[![Time](https://img.shields.io/badge/development-615.5h-blueviolet.svg)]() [![Powered by](https://img.shields.io/badge/powered%20by%20java-orange.svg?logo=OpenJDK&logoColor=white)]() [![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=kendo-tournament-backend&metric=vulnerabilities)](https://sonarcloud.io/summary/new_code?id=kendo-tournament-backend) diff --git a/backend/kendo-tournament-core/src/main/java/com/softwaremagico/kt/core/controller/GroupController.java b/backend/kendo-tournament-core/src/main/java/com/softwaremagico/kt/core/controller/GroupController.java index 58ef3cae4..6de907012 100644 --- a/backend/kendo-tournament-core/src/main/java/com/softwaremagico/kt/core/controller/GroupController.java +++ b/backend/kendo-tournament-core/src/main/java/com/softwaremagico/kt/core/controller/GroupController.java @@ -34,8 +34,6 @@ import com.softwaremagico.kt.core.converters.models.GroupConverterRequest; import com.softwaremagico.kt.core.converters.models.TournamentConverterRequest; import com.softwaremagico.kt.core.exceptions.GroupNotFoundException; -import com.softwaremagico.kt.core.exceptions.TeamNotFoundException; -import com.softwaremagico.kt.core.exceptions.TournamentInvalidException; import com.softwaremagico.kt.core.exceptions.TournamentNotFoundException; import com.softwaremagico.kt.core.providers.DuelProvider; import com.softwaremagico.kt.core.providers.FightProvider; @@ -45,7 +43,6 @@ import com.softwaremagico.kt.logger.ExceptionType; import com.softwaremagico.kt.persistence.entities.Group; import com.softwaremagico.kt.persistence.repositories.GroupRepository; -import com.softwaremagico.kt.persistence.values.TournamentType; import jakarta.transaction.Transactional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @@ -69,10 +66,12 @@ public class GroupController extends BasicInsertableController groupsUpdatedListeners = new HashSet<>(); + public interface GroupsUpdatedListener { void updated(TournamentDTO tournament, String actor); } + @Autowired public GroupController(GroupProvider provider, GroupConverter converter, TournamentConverter tournamentConverter, TournamentProvider tournamentProvider, FightProvider fightProvider, FightConverter fightConverter, @@ -89,31 +88,37 @@ public GroupController(GroupProvider provider, GroupConverter converter, Tournam this.tournamentHandlerSelector = tournamentHandlerSelector; } + public void addGroupUpdatedListeners(GroupsUpdatedListener listener) { groupsUpdatedListeners.add(listener); } + @Override protected GroupConverterRequest createConverterRequest(Group group) { return new GroupConverterRequest(group); } + public List getFromTournament(Integer tournamentId) { return get(tournamentConverter.convert(new TournamentConverterRequest(tournamentProvider.get(tournamentId) .orElseThrow(() -> new TournamentNotFoundException(getClass(), "No tournament found with id '" + tournamentId + "',", ExceptionType.INFO))))); } + public GroupDTO getFromTournament(Integer tournamentId, Integer level, Integer index) { return convert(getProvider().getGroupByLevelAndIndex(tournamentProvider.get(tournamentId) .orElseThrow(() -> new TournamentNotFoundException(getClass(), "No tournament found with id '" + tournamentId + "',", ExceptionType.INFO)), level, index)); } + public List getGroups(TournamentDTO tournament, Integer level) { return getProvider().getGroups(tournamentConverter.reverse(tournament), level); } + @Override public GroupDTO create(GroupDTO groupDTO, String username) { try { @@ -126,11 +131,13 @@ public GroupDTO create(GroupDTO groupDTO, String username) { } } + @Override public void deleteById(Integer id, String username) { delete(get(id), username); } + @Override public void delete(GroupDTO groupDTO, String username) { try { @@ -143,15 +150,18 @@ public void delete(GroupDTO groupDTO, String username) { } } + @Override public void delete(Collection groupDTOs, String username) { groupDTOs.forEach(groupDTO -> delete(groupDTOs, username)); } + public List get(TournamentDTO tournament) { return convertAll(getProvider().getGroups(tournamentConverter.reverse(tournament))); } + @Transactional public GroupDTO update(GroupDTO groupDTO, String username) { validate(groupDTO); @@ -188,6 +198,7 @@ public GroupDTO update(GroupDTO groupDTO, String username) { } } + public GroupDTO addTeams(Integer groupId, List teams, String username) { try { return convert(getProvider().addTeams(groupId, teamConverter.reverseAll(teams), username)); @@ -199,6 +210,7 @@ public GroupDTO addTeams(Integer groupId, List teams, String username) } } + public GroupDTO deleteTeams(Integer groupId, List teams, String username) { try { return convert(getProvider().deleteTeams(groupId, teamConverter.reverseAll(teams), username)); @@ -210,6 +222,7 @@ public GroupDTO deleteTeams(Integer groupId, List teams, String usernam } } + public List deleteTeamsFromTournament(Integer tournamentId, String username) { try { return convertAll(getProvider().deleteTeams(tournamentProvider.get(tournamentId).orElseThrow(() -> @@ -225,6 +238,7 @@ public List deleteTeamsFromTournament(Integer tournamentId, String use } } + public List deleteTeamsFromTournament(Integer tournamentId, List teams, String username) { try { return convertAll(getProvider().deleteTeams(tournamentProvider.get(tournamentId).orElseThrow(() -> @@ -240,6 +254,7 @@ public List deleteTeamsFromTournament(Integer tournamentId, List teams, String username) { try { return convert(getProvider().setTeams(groupId, teamConverter.reverseAll(teams), username)); @@ -251,34 +266,19 @@ public GroupDTO setTeams(Integer groupId, List teams, String username) } } + public GroupDTO setTeams(List teams, String username) { - if (teams.isEmpty()) { - throw new TeamNotFoundException(this.getClass(), "No teams found!"); - } - GroupDTO groupDTO = get(teams.get(0).getTournament()).stream().findAny().orElseThrow(() -> + final GroupDTO groupDTO = get(teams.get(0).getTournament()).stream().findAny().orElseThrow(() -> new GroupNotFoundException(this.getClass(), "No groups found!")); - if (groupDTO.getTournament().getType().equals(TournamentType.CHAMPIONSHIP) - || groupDTO.getTournament().getType().equals(TournamentType.TREE) - || groupDTO.getTournament().getType().equals(TournamentType.CUSTOM_CHAMPIONSHIP)) { - throw new TournamentInvalidException(this.getClass(), "On tournaments with type '" - + groupDTO.getTournament().getType() + "' group must be selected."); - } - final List fights = groupDTO.getFights(); - groupDTO.getFights().clear(); - fightProvider.delete(fightConverter.reverseAll(fights)); - groupDTO.getTeams().clear(); - groupDTO = convert(getProvider().save(reverse(groupDTO))); - groupDTO.setTeams(teams); - groupDTO.setUpdatedBy(username); try { - return convert(getProvider().save(reverse(groupDTO))); + return convert(getProvider().setTeams(groupDTO.getId(), teamConverter.reverseAll(teams), username)); } finally { - final GroupDTO finalGroupDTO = groupDTO; new Thread(() -> groupsUpdatedListeners.forEach(groupsUpdatedListener -> - groupsUpdatedListener.updated(finalGroupDTO.getTournament(), username))).start(); + groupsUpdatedListener.updated(groupDTO.getTournament(), username))).start(); } } + public GroupDTO addUnties(Integer groupId, List duelDTOS, String username) { final GroupDTO groupDTO = get(groupId); duelDTOS.forEach(duelDTO -> { @@ -290,10 +290,12 @@ public GroupDTO addUnties(Integer groupId, List duelDTOS, String userna return convert(getProvider().save(reverse(groupDTO))); } + public long count(TournamentDTO tournament) { return getProvider().count(tournamentConverter.reverse(tournament)); } + public long delete(TournamentDTO tournamentDTO) { return getProvider().delete(tournamentConverter.reverse(tournamentDTO)); } diff --git a/backend/kendo-tournament-core/src/main/java/com/softwaremagico/kt/core/providers/GroupProvider.java b/backend/kendo-tournament-core/src/main/java/com/softwaremagico/kt/core/providers/GroupProvider.java index 11692c694..35d07888c 100644 --- a/backend/kendo-tournament-core/src/main/java/com/softwaremagico/kt/core/providers/GroupProvider.java +++ b/backend/kendo-tournament-core/src/main/java/com/softwaremagico/kt/core/providers/GroupProvider.java @@ -23,6 +23,7 @@ import com.softwaremagico.kt.core.exceptions.NotFoundException; +import com.softwaremagico.kt.core.exceptions.TeamNotFoundException; import com.softwaremagico.kt.logger.ExceptionType; import com.softwaremagico.kt.logger.KendoTournamentLogger; import com.softwaremagico.kt.persistence.entities.Duel; @@ -215,19 +216,24 @@ public long count(Tournament tournament) { } public Group setTeams(Integer groupId, List teams, String username) { + if (teams.isEmpty()) { + throw new TeamNotFoundException(this.getClass(), "No teams found!"); + } + Group group = get(groupId).orElseThrow(() -> new NotFoundException(getClass(), "Group with id '" + groupId + "' not found.", ExceptionType.INFO)); final List fights = new ArrayList<>(group.getFights()); group.getFights().clear(); - fightRepository.deleteAll(fights); final List unties = new ArrayList<>(group.getUnties()); group.getUnties().clear(); - duelRepository.deleteAll(unties); group.getTeams().clear(); group = save(group); + //Unties are unlinked from groups. Can be removed. + duelRepository.deleteAll(unties); + fightRepository.deleteAll(fights); group.setTeams(teams); group.setUpdatedBy(username); return save(group);