From f910c6590b5a852ee65a7069a7aed5cd5abce640 Mon Sep 17 00:00:00 2001 From: chanhengseang Date: Mon, 26 May 2025 18:05:37 -0700 Subject: [PATCH] re-assign --- .../com/srr/dto/TeamPlayerReassignDto.java | 38 +++++++++++++++++ .../com/srr/rest/TeamPlayerController.java | 10 +++++ .../com/srr/service/TeamPlayerService.java | 8 ++++ .../service/impl/TeamPlayerServiceImpl.java | 41 +++++++++++++++++++ 4 files changed, 97 insertions(+) create mode 100644 sport/src/main/java/com/srr/dto/TeamPlayerReassignDto.java diff --git a/sport/src/main/java/com/srr/dto/TeamPlayerReassignDto.java b/sport/src/main/java/com/srr/dto/TeamPlayerReassignDto.java new file mode 100644 index 00000000..4fad79c3 --- /dev/null +++ b/sport/src/main/java/com/srr/dto/TeamPlayerReassignDto.java @@ -0,0 +1,38 @@ +/* + * Copyright 2019-2025 Zheng Jie + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.srr.dto; + +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import javax.validation.constraints.NotNull; + +/** + * @author Chanheng + * @website https://eladmin.vip + * @date 2025-05-26 + **/ +@Data +public class TeamPlayerReassignDto { + + @NotNull + @ApiModelProperty(value = "Team player ID to reassign") + private Long teamPlayerId; + + @NotNull + @ApiModelProperty(value = "Target team ID") + private Long targetTeamId; +} diff --git a/sport/src/main/java/com/srr/rest/TeamPlayerController.java b/sport/src/main/java/com/srr/rest/TeamPlayerController.java index 5560e7f6..bad7d72c 100644 --- a/sport/src/main/java/com/srr/rest/TeamPlayerController.java +++ b/sport/src/main/java/com/srr/rest/TeamPlayerController.java @@ -16,6 +16,7 @@ package com.srr.rest; import com.srr.dto.TeamPlayerDto; +import com.srr.dto.TeamPlayerReassignDto; import com.srr.service.TeamPlayerService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @@ -24,6 +25,7 @@ import me.zhengjie.annotation.Log; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; /** @@ -53,4 +55,12 @@ public class TeamPlayerController { public ResponseEntity checkIn(@PathVariable Long id) { return new ResponseEntity<>(teamPlayerService.checkIn(id), HttpStatus.OK); } + + @PostMapping("/reassign") + @Log("Reassign player to another team") + @ApiOperation("Reassign player to another team") + @PreAuthorize("@el.check('event:admin')") + public ResponseEntity reassignPlayer(@Validated @RequestBody TeamPlayerReassignDto dto) { + return new ResponseEntity<>(teamPlayerService.reassignPlayer(dto), HttpStatus.OK); + } } diff --git a/sport/src/main/java/com/srr/service/TeamPlayerService.java b/sport/src/main/java/com/srr/service/TeamPlayerService.java index 51632fba..5da3698d 100644 --- a/sport/src/main/java/com/srr/service/TeamPlayerService.java +++ b/sport/src/main/java/com/srr/service/TeamPlayerService.java @@ -17,6 +17,7 @@ package com.srr.service; import com.srr.domain.TeamPlayer; import com.srr.dto.TeamPlayerDto; +import com.srr.dto.TeamPlayerReassignDto; import org.springframework.data.domain.Pageable; import me.zhengjie.utils.PageResult; @@ -50,4 +51,11 @@ public interface TeamPlayerService { * @return List of TeamPlayerDto objects */ List findByEventId(Long eventId); + + /** + * Reassign a player from one team to another + * @param dto Contains teamPlayerId and targetTeamId + * @return The updated TeamPlayerDto + */ + TeamPlayerDto reassignPlayer(TeamPlayerReassignDto dto); } diff --git a/sport/src/main/java/com/srr/service/impl/TeamPlayerServiceImpl.java b/sport/src/main/java/com/srr/service/impl/TeamPlayerServiceImpl.java index 5e395946..0c9da130 100644 --- a/sport/src/main/java/com/srr/service/impl/TeamPlayerServiceImpl.java +++ b/sport/src/main/java/com/srr/service/impl/TeamPlayerServiceImpl.java @@ -15,10 +15,13 @@ */ package com.srr.service.impl; +import com.srr.domain.Team; import com.srr.domain.TeamPlayer; import com.srr.dto.TeamPlayerDto; +import com.srr.dto.TeamPlayerReassignDto; import com.srr.dto.mapstruct.TeamPlayerMapper; import com.srr.repository.TeamPlayerRepository; +import com.srr.repository.TeamRepository; import com.srr.service.TeamPlayerService; import lombok.RequiredArgsConstructor; import me.zhengjie.exception.BadRequestException; @@ -39,6 +42,7 @@ import java.util.stream.Collectors; public class TeamPlayerServiceImpl implements TeamPlayerService { private final TeamPlayerRepository teamPlayerRepository; + private final TeamRepository teamRepository; private final TeamPlayerMapper teamPlayerMapper; @Override @@ -73,4 +77,41 @@ public class TeamPlayerServiceImpl implements TeamPlayerService { .map(teamPlayerMapper::toDto) .collect(Collectors.toList()); } + + @Override + @Transactional + public TeamPlayerDto reassignPlayer(TeamPlayerReassignDto dto) { + // Find the team player to reassign + TeamPlayer teamPlayer = teamPlayerRepository.findById(dto.getTeamPlayerId()) + .orElseThrow(() -> new EntityNotFoundException(TeamPlayer.class, "id", dto.getTeamPlayerId().toString())); + + // Find the target team + Team targetTeam = teamRepository.findById(dto.getTargetTeamId()) + .orElseThrow(() -> new EntityNotFoundException(Team.class, "id", dto.getTargetTeamId().toString())); + + // Store the original team for potential deletion + Team originalTeam = teamPlayer.getTeam(); + + // Check if target team is in the same event as the original team + if (!originalTeam.getEvent().getId().equals(targetTeam.getEvent().getId())) { + throw new BadRequestException("Cannot reassign player to a team in a different event"); + } + + // Check if the target team is full + if (targetTeam.getTeamPlayers().size() >= targetTeam.getTeamSize()) { + throw new BadRequestException("Target team is already full"); + } + + // Reassign the player to the new team + teamPlayer.setTeam(targetTeam); + teamPlayerRepository.save(teamPlayer); + + // Check if original team is now empty, and delete if it is + if (originalTeam.getTeamPlayers().stream() + .noneMatch(tp -> !tp.getId().equals(teamPlayer.getId()))) { + teamRepository.delete(originalTeam); + } + + return teamPlayerMapper.toDto(teamPlayer); + } }