Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add team create and retrieve functionalities #6

Merged
merged 9 commits into from
Nov 20, 2024
33 changes: 33 additions & 0 deletions src/main/java/com/luka/levi9/controller/TeamController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.luka.levi9.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.luka.levi9.dto.TeamCreationRequest;
import com.luka.levi9.model.Team;
import com.luka.levi9.service.TeamService;

import lombok.AllArgsConstructor;

@RestController
@AllArgsConstructor
@RequestMapping("/teams")
public class TeamController {

private final TeamService teamService;

@PostMapping()
public Team createTeam(@RequestBody TeamCreationRequest request) {
return teamService.createTeam(request);
}

@GetMapping("/{id}")
public Team getTeam(@PathVariable String id) {
return teamService.getTeam(id);
}

}
16 changes: 16 additions & 0 deletions src/main/java/com/luka/levi9/dto/TeamCreationRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.luka.levi9.dto;

import java.util.List;

import com.fasterxml.jackson.annotation.JsonAlias;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class TeamCreationRequest {
private String teamName;
@JsonAlias("players")
private List<String> playerIds;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(HttpStatus.NOT_FOUND)
public class PlayerNotFoundException extends RuntimeException {
public class NotFoundException extends RuntimeException {

private static final long serialVersionUID = -1616826993751458020L;

public PlayerNotFoundException(String message) {
public NotFoundException(String message) {
super(message);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.luka.levi9.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

import lombok.experimental.StandardException;

@StandardException
@ResponseStatus(HttpStatus.BAD_REQUEST)
public class TeamValidationException extends RuntimeException {

private static final long serialVersionUID = -7317190995382988861L;
}
12 changes: 12 additions & 0 deletions src/main/java/com/luka/levi9/repository/TeamRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.luka.levi9.repository;

import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;

import com.luka.levi9.model.Team;

public interface TeamRepository extends JpaRepository<Team, String> {

Optional<Team> findByTeamName(String teamName);
}
11 changes: 5 additions & 6 deletions src/main/java/com/luka/levi9/service/PlayerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@
import org.springframework.stereotype.Service;

import com.luka.levi9.exception.PlayerAlreadyExistsException;
import com.luka.levi9.exception.PlayerNotFoundException;
import com.luka.levi9.exception.NotFoundException;
import com.luka.levi9.model.Player;
import com.luka.levi9.repository.PlayerRepository;

import lombok.AllArgsConstructor;

@Service
@AllArgsConstructor
public class PlayerService {

private final PlayerRepository playerRepository;

public PlayerService(PlayerRepository playerRepository) {
this.playerRepository = playerRepository;
}

public Player createPlayer(Player player) {
if (playerRepository.findByNickname(player.getNickname()).isPresent())
throw new PlayerAlreadyExistsException("Player with nickname " + player.getNickname() + " already exists.");
Expand All @@ -25,6 +24,6 @@ public Player createPlayer(Player player) {

public Player getPlayer(String id) {
return playerRepository.findById(id)
.orElseThrow(() -> new PlayerNotFoundException("Player with id " + id + " not found."));
.orElseThrow(() -> new NotFoundException("Player with id " + id + " not found."));
}
}
65 changes: 65 additions & 0 deletions src/main/java/com/luka/levi9/service/TeamService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.luka.levi9.service;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.luka.levi9.dto.TeamCreationRequest;
import com.luka.levi9.exception.NotFoundException;
import com.luka.levi9.exception.TeamValidationException;
import com.luka.levi9.model.Player;
import com.luka.levi9.model.Team;
import com.luka.levi9.repository.PlayerRepository;
import com.luka.levi9.repository.TeamRepository;

import lombok.AllArgsConstructor;

@Service
@AllArgsConstructor
public class TeamService {

private final TeamRepository teamRepository;
private final PlayerRepository playerRepository;

@Transactional
public Team createTeam(TeamCreationRequest teamDto) {
if (teamRepository.findByTeamName(teamDto.getTeamName()).isPresent())
throw new TeamValidationException("Team with name " + teamDto.getTeamName() + " already exists.");

List<Player> players = playerRepository.findAllById(teamDto.getPlayerIds());

validatePlayers(teamDto, players);

return saveTeam(teamDto, players);
}

private void validatePlayers(TeamCreationRequest teamDto, List<Player> players) {
if (players.size() != teamDto.getPlayerIds().size())
throw new TeamValidationException("Some players were not found.");

if (players.size() != 5)
throw new TeamValidationException("A team must have exactly 5 players.");

players.stream().filter(player -> player.getTeam() != null).findAny().ifPresent(player -> {
throw new TeamValidationException(
"Player with ID " + player.getId() + " is already in a team and cannot join another.");
});
}

private Team saveTeam(TeamCreationRequest teamDto, List<Player> players) {
Team newTeam = new Team();
newTeam.setTeamName(teamDto.getTeamName());
newTeam.setPlayers(players);

players.forEach(player -> player.setTeam(newTeam));

return teamRepository.save(newTeam);
}

public Team getTeam(String id) {
return teamRepository.findById(id)
.orElseThrow(() -> new NotFoundException("Team with id " + id + " not found."));
}

}
Loading