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

Reafactor and reorganize project structure #7

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,110 changes: 1,110 additions & 0 deletions 5danauoblacima2024-public.postman-collection.json

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions levi9cloud.postman-environment.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"id": "0665a3d1-fd0c-411f-b680-053adbfca671",
"name": "Levi9Cloud",
"values": [
{
"key": "baseURL",
"value": "localhost:8080",
"type": "default",
"enabled": true
}
],
"_postman_variable_scope": "environment",
"_postman_exported_at": "2024-11-18T20:34:09.462Z",
"_postman_exported_using": "Postman/11.20.0"
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.luka.levi9.exception;
package com.luka.levi9.common.exception;

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

@ResponseStatus(HttpStatus.CONFLICT)
public class PlayerAlreadyExistsException extends RuntimeException {
public class AlreadyExistsException extends RuntimeException {

private static final long serialVersionUID = 5321395506214256957L;

public PlayerAlreadyExistsException(String message) {
public AlreadyExistsException(String message) {
super(message);
}

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

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.luka.levi9.exception;
package com.luka.levi9.common.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
Expand All @@ -7,7 +7,7 @@

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

private static final long serialVersionUID = -7317190995382988861L;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.luka.levi9.model;
package com.luka.levi9.player;

import org.hibernate.annotations.NamedQuery;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.luka.levi9.team.Team;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
Expand All @@ -11,12 +15,15 @@
import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@Entity
@Table(uniqueConstraints = @UniqueConstraint(columnNames = { "nickname" }))
@NoArgsConstructor
@NamedQuery(name = "Player.findAll", query = "SELECT p FROM Player p")
@Table(uniqueConstraints = @UniqueConstraint(columnNames = { "nickname" }))
public class Player {

@Id
Expand All @@ -29,5 +36,11 @@ public class Player {
private int elo;
private int hoursPlayed;
@ManyToOne
@JsonIgnore
private Team team;

@JsonProperty("teamId")
public String getTeamId() {
return team != null ? team.getId() : null;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package com.luka.levi9.controller;
package com.luka.levi9.player;

import java.util.List;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand All @@ -7,18 +9,15 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.luka.levi9.model.Player;
import com.luka.levi9.service.PlayerService;
import lombok.AllArgsConstructor;

@RestController
@AllArgsConstructor
@RequestMapping("/players")
public class PlayerController {

private final PlayerService playerService;

public PlayerController(PlayerService playerService) {
this.playerService = playerService;
}


@PostMapping("/create")
public Player createPlayer(@RequestBody Player player) {
Expand All @@ -29,4 +28,9 @@ public Player createPlayer(@RequestBody Player player) {
public Player getPlayer(@PathVariable String id) {
return playerService.getPlayer(id);
}

@GetMapping()
public List<Player> getPlayers() {
return playerService.getPlayers();
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.luka.levi9.repository;
package com.luka.levi9.player;

import java.util.Optional;

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

import com.luka.levi9.model.Player;

public interface PlayerRepository extends JpaRepository<Player, String> {

Optional<Player> findByNickname(String nickname);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.luka.levi9.service;
package com.luka.levi9.player;

import java.util.List;

import org.springframework.stereotype.Service;

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

import lombok.AllArgsConstructor;

Expand All @@ -17,7 +17,7 @@ public class PlayerService {

public Player createPlayer(Player player) {
if (playerRepository.findByNickname(player.getNickname()).isPresent())
throw new PlayerAlreadyExistsException("Player with nickname " + player.getNickname() + " already exists.");
throw new AlreadyExistsException("Player with nickname " + player.getNickname() + " already exists.");

return playerRepository.save(player);
}
Expand All @@ -26,4 +26,8 @@ public Player getPlayer(String id) {
return playerRepository.findById(id)
.orElseThrow(() -> new NotFoundException("Player with id " + id + " not found."));
}

public List<Player> getPlayers() {
return playerRepository.findAll();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.luka.levi9.model;
package com.luka.levi9.team;

import java.util.List;

import com.luka.levi9.player.Player;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.luka.levi9.controller;
package com.luka.levi9.team;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand All @@ -7,10 +7,6 @@
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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.luka.levi9.dto;
package com.luka.levi9.team;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.luka.levi9.repository;
package com.luka.levi9.team;

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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package com.luka.levi9.service;
package com.luka.levi9.team;

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 com.luka.levi9.common.exception.NotFoundException;
import com.luka.levi9.common.exception.ValidationException;
import com.luka.levi9.player.Player;
import com.luka.levi9.player.PlayerRepository;

import lombok.AllArgsConstructor;

Expand All @@ -25,7 +22,7 @@ public class TeamService {
@Transactional
public Team createTeam(TeamCreationRequest teamDto) {
if (teamRepository.findByTeamName(teamDto.getTeamName()).isPresent())
throw new TeamValidationException("Team with name " + teamDto.getTeamName() + " already exists.");
throw new ValidationException("Team with name " + teamDto.getTeamName() + " already exists.");

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

Expand All @@ -36,13 +33,13 @@ public Team createTeam(TeamCreationRequest teamDto) {

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

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

players.stream().filter(player -> player.getTeam() != null).findAny().ifPresent(player -> {
throw new TeamValidationException(
throw new ValidationException(
"Player with ID " + player.getId() + " is already in a team and cannot join another.");
});
}
Expand Down
Binary file added zadatak-5-dana-u-oblacima-2024.pdf
Binary file not shown.
Loading