mirror of https://github.com/elunez/eladmin
re-assign
parent
d181fb2e63
commit
f910c6590b
|
@ -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;
|
||||
}
|
|
@ -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<TeamPlayerDto> 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<TeamPlayerDto> reassignPlayer(@Validated @RequestBody TeamPlayerReassignDto dto) {
|
||||
return new ResponseEntity<>(teamPlayerService.reassignPlayer(dto), HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<TeamPlayerDto> 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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue