mirror of
https://github.com/PaulReichmuth/timekeep-backend.git
synced 2025-12-22 14:41:57 +00:00
begin service structure
This commit is contained in:
@@ -11,7 +11,7 @@
|
||||
<method v="2">
|
||||
<option name="RunConfigurationTask" enabled="true" run_configuration_name="resources/compose.yaml: Compose Deployment" run_configuration_type="docker-deploy" />
|
||||
<option name="Make" enabled="true" />
|
||||
<option name="ToolBeforeRunTask" enabled="true" actionId="Tool_External Tools_delay 2s" />
|
||||
<option name="ToolBeforeRunTask" enabled="true" actionId="Tool_External Tools_delay 5s" />
|
||||
</method>
|
||||
</configuration>
|
||||
</component>
|
||||
@@ -6,6 +6,7 @@ plugins {
|
||||
id("java-library")
|
||||
`maven-publish`
|
||||
id("io.freefair.lombok") version "9.0.0"
|
||||
id("io.spring.dependency-management") version "1.1.7"
|
||||
}
|
||||
|
||||
repositories {
|
||||
@@ -14,6 +15,7 @@ repositories {
|
||||
maven {
|
||||
url = uri("https://repo.maven.apache.org/maven2/")
|
||||
}
|
||||
maven { url = uri("https://repo.spring.io/snapshot") }
|
||||
}
|
||||
|
||||
dependencies {
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
package de.pnreichmuth.timekeep_backend.controllers;
|
||||
|
||||
import de.pnreichmuth.timekeep_backend.entities.Team;
|
||||
import de.pnreichmuth.timekeep_backend.exceptions.TeamNotFoundException;
|
||||
import de.pnreichmuth.timekeep_backend.services.TeamService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.rmi.NoSuchObjectException;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@@ -13,8 +15,11 @@ import java.util.List;
|
||||
@Slf4j
|
||||
public class TeamRestController {
|
||||
|
||||
@Autowired
|
||||
TeamService teamService;
|
||||
|
||||
@PostMapping("createTeam")
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
public Team createTeam(@RequestBody Team team){
|
||||
teamService.createTeam(team);
|
||||
return team;
|
||||
@@ -26,17 +31,15 @@ public class TeamRestController {
|
||||
}
|
||||
|
||||
@DeleteMapping("all")
|
||||
@ResponseStatus(HttpStatus.NO_CONTENT)
|
||||
public void deleteAllTeams(){
|
||||
teamService.deleteAllTeams();
|
||||
}
|
||||
|
||||
@DeleteMapping("deleteTeam")
|
||||
@ResponseStatus(HttpStatus.NO_CONTENT)
|
||||
public void deleteTeam(@RequestBody Team team){
|
||||
try{
|
||||
teamService.deleteTeam(team.getTeamName());
|
||||
} catch (NoSuchObjectException e) {
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
teamService.deleteTeam(team.getTeamName());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package de.pnreichmuth.timekeep_backend.exceptions;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
@Slf4j
|
||||
@ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "RACER_NOT_FOUND")
|
||||
public class RacerNotFoundException extends RuntimeException {
|
||||
public RacerNotFoundException(String message) {
|
||||
super(message);
|
||||
log.error(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package de.pnreichmuth.timekeep_backend.exceptions;
|
||||
|
||||
import de.pnreichmuth.timekeep_backend.entities.Team;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
@ResponseStatus(value = HttpStatus.CONFLICT, reason = "TEAM_EXISTS")
|
||||
@Slf4j
|
||||
public class TeamExistsException extends RuntimeException {
|
||||
public TeamExistsException(String message, Team team) {
|
||||
super(message);
|
||||
log.warn(message, team.getTeamName());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package de.pnreichmuth.timekeep_backend.exceptions;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
@Slf4j
|
||||
@ResponseStatus(HttpStatus.NOT_FOUND)
|
||||
public class TeamNotFoundException extends RuntimeException {
|
||||
public TeamNotFoundException(String message) {
|
||||
super(message);
|
||||
log.error(message);
|
||||
}
|
||||
}
|
||||
@@ -6,11 +6,12 @@ import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
@SuppressWarnings("ALL")
|
||||
@Repository
|
||||
public interface RacerRepository extends JpaRepository<@NonNull Racer,@NonNull UUID> {
|
||||
Racer findByName(String name);
|
||||
Optional<Racer> getRacerByName(String name);
|
||||
List<Racer> findAllByName(String name);
|
||||
}
|
||||
|
||||
@@ -5,11 +5,12 @@ import lombok.NonNull;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
@Repository
|
||||
public interface StationRepository extends JpaRepository<@NonNull Station, @NonNull UUID> {
|
||||
Station findByLocation(String location);
|
||||
Station findByName(String name);
|
||||
Station findByPasswordHash(String passwordHash);
|
||||
Optional<Station> findByLocation(String location);
|
||||
Optional<Station> findByName(String name);
|
||||
Optional<Station> findByPasswordHash(String passwordHash);
|
||||
}
|
||||
|
||||
@@ -7,14 +7,14 @@ import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
@Repository
|
||||
public interface TeamRepository extends JpaRepository<@NonNull Team, @NonNull UUID> {
|
||||
Team getTeamByTeamName(String name);
|
||||
Optional<Team> getTeamByTeamName(String name);
|
||||
List<Team> getTeamsByFirstSemesterTeamIsTrue();
|
||||
List<Team> getTeamsByPassedStationsContains(Station station);
|
||||
void deleteTeamByTeamName(String name);
|
||||
|
||||
boolean existsByTeamName(String name);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
package de.pnreichmuth.timekeep_backend.services;
|
||||
|
||||
import de.pnreichmuth.timekeep_backend.entities.Racer;
|
||||
import de.pnreichmuth.timekeep_backend.exceptions.RacerNotFoundException;
|
||||
import de.pnreichmuth.timekeep_backend.exceptions.TeamNotFoundException;
|
||||
import de.pnreichmuth.timekeep_backend.repositories.RacerRepository;
|
||||
import de.pnreichmuth.timekeep_backend.repositories.TeamRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class RacerService {
|
||||
private final RacerRepository racerRepository;
|
||||
|
||||
public Racer createRacer(Racer racer){
|
||||
return racerRepository.save(racer);
|
||||
}
|
||||
|
||||
public Racer createRacer(String name, boolean isFirstSemester){
|
||||
Racer racer = new Racer();
|
||||
racer.setName(name);
|
||||
racer.setIsFirstSemester(isFirstSemester);
|
||||
return racerRepository.save(racer);
|
||||
}
|
||||
|
||||
public Racer getRacer(String name){
|
||||
Racer racer = racerRepository.getRacerByName(name).orElse(null);
|
||||
if(racer == null) throw new RacerNotFoundException("Racer "+name+" not found");
|
||||
return racer;
|
||||
}
|
||||
|
||||
public UUID getRacerIdByName(String name){
|
||||
Racer racer = racerRepository.getRacerByName(name).orElse(null);
|
||||
if(racer == null) throw new RacerNotFoundException("Racer "+name+" not found");
|
||||
return racer.getId();
|
||||
}
|
||||
|
||||
public Racer addTelNumber(String name,String telNumber){
|
||||
Racer racer = racerRepository.getRacerByName(name).orElse(null);
|
||||
if(racer == null) throw new RacerNotFoundException("Racer "+name+" not found");
|
||||
racer.setPhoneNumber(telNumber);
|
||||
return racerRepository.save(racer);
|
||||
}
|
||||
|
||||
public Racer addTelNumber(UUID id, String telNumber){
|
||||
Racer racer = racerRepository.findById(id).orElse(null);
|
||||
if(racer == null) throw new RacerNotFoundException("Racer not found");
|
||||
racer.setPhoneNumber(telNumber);
|
||||
return racerRepository.save(racer);
|
||||
}
|
||||
|
||||
public void deleteRacer(UUID id) throws RacerNotFoundException {
|
||||
Racer racer = racerRepository.findById(id).orElse(null);
|
||||
if(racer == null) throw new RacerNotFoundException("Racer not found");
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,13 @@
|
||||
package de.pnreichmuth.timekeep_backend.services;
|
||||
|
||||
import de.pnreichmuth.timekeep_backend.entities.Team;
|
||||
import de.pnreichmuth.timekeep_backend.exceptions.TeamExistsException;
|
||||
import de.pnreichmuth.timekeep_backend.exceptions.TeamNotFoundException;
|
||||
import de.pnreichmuth.timekeep_backend.repositories.TeamRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.rmi.NoSuchObjectException;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@@ -22,7 +23,9 @@ public class TeamService {
|
||||
}
|
||||
|
||||
public Team getTeam(String name){
|
||||
return teamRepository.getTeamByTeamName(name);
|
||||
Team team = teamRepository.getTeamByTeamName(name).orElse(null);
|
||||
if(team == null) throw new TeamNotFoundException("Team "+name+" not found");
|
||||
return team;
|
||||
}
|
||||
|
||||
public List<Team> getTeams(){
|
||||
@@ -36,6 +39,9 @@ public class TeamService {
|
||||
}
|
||||
|
||||
public void createTeam(Team team){
|
||||
if(teamRepository.existsByTeamName(team.getTeamName())){
|
||||
throw new TeamExistsException("A team by this name already exists.", team);
|
||||
}
|
||||
teamRepository.save(team);
|
||||
log.info("Team created: {}", team);
|
||||
}
|
||||
@@ -47,13 +53,13 @@ public class TeamService {
|
||||
log.info("Team created: {}", tempTeam);
|
||||
}
|
||||
|
||||
public void deleteTeam(UUID id) throws NoSuchObjectException{
|
||||
if(!teamRepository.existsById(id)) throw new NoSuchObjectException("Team not found");
|
||||
public void deleteTeam(UUID id) throws TeamNotFoundException {
|
||||
if(!teamRepository.existsById(id)) throw new TeamNotFoundException("Team not found");
|
||||
teamRepository.deleteById(id);
|
||||
}
|
||||
|
||||
public void deleteTeam(String name) throws NoSuchObjectException{
|
||||
if(!teamRepository.existsByTeamName(name)) throw new NoSuchObjectException("Team not found");
|
||||
public void deleteTeam(String name) throws TeamNotFoundException {
|
||||
if(!teamRepository.existsByTeamName(name)) throw new TeamNotFoundException("Team not found");
|
||||
teamRepository.deleteTeamByTeamName(name);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user