Skip to content
Snippets Groups Projects
Commit 49d8cf92 authored by Diana Gulčíková's avatar Diana Gulčíková
Browse files

Merge branch '17_statistics_endpoint' into 'milestone_2'

17 statistics endpoint

See merge request !33
parents e3e89215 3dc598d6
No related branches found
No related tags found
2 merge requests!60Docker,!3317 statistics endpoint
Pipeline #
package cz.muni.pa165.common_library.dtos;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.Builder;
import lombok.Data;
/**
* Dto fro DriverCar entity.
*/
@Data
@Builder
public class RaceDriverCarDto {
@NotNull
@Schema(description = "driver id", example = "1")
Long driverId;
@NotNull
@Schema(description = "car id", example = "1")
Long carId;
@Schema(description = "driver name", example = "Charles Leclerc")
String driverName;
@Schema(description = "drivers position in the race", example = "1")
int position;
}
package cz.muni.pa165.common_library.dtos; package cz.muni.pa165.common_library.dtos;
import cz.muni.pa165.common_library.racecomponents.Location; import cz.muni.pa165.common_library.racecomponents.Location;
import cz.muni.pa165.common_library.racecomponents.Race;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
...@@ -21,17 +22,35 @@ public class RaceDto { ...@@ -21,17 +22,35 @@ public class RaceDto {
Long id; Long id;
@NotNull @NotNull
@Schema(description = "race location", example = "Monaco") @Schema(description = "race information")
private Location location; private RaceDto.RaceInfo raceInfo;
@NotNull
@Schema(description = "race name", example = "Monaco Grand Prix 2023")
private String name;
@Schema(description = "driver one", example = "{1, 1, Charles Leclerc}") @Schema(description = "driver one", example = "{1, 1, Charles Leclerc}")
private DriverCarDto driverOne; private RaceDriverCarDto driverOne;
@Schema(description = "driver two", example = "{2, 2, Carlos Sainz}") @Schema(description = "driver two", example = "{2, 2, Carlos Sainz}")
private DriverCarDto driverTwo; private RaceDriverCarDto driverTwo;
/**
* Race information.
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public static class RaceInfo {
@NotNull
@Schema(description = "race location", example = "Monaco")
private Location location;
@NotNull
@Schema(description = "race name", example = "Monaco Grand Prix 2023")
private String name;
@NotNull
@Schema(description = "race prize pool", example = "30000000")
private long prizePool;
}
} }
package cz.muni.pa165.common_library.racecomponents; package cz.muni.pa165.common_library.racecomponents;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column; import jakarta.persistence.Column;
import jakarta.persistence.Entity; import jakarta.persistence.Entity;
import jakarta.persistence.EnumType; import jakarta.persistence.EnumType;
...@@ -9,10 +10,16 @@ import jakarta.persistence.GeneratedValue; ...@@ -9,10 +10,16 @@ import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType; import jakarta.persistence.GenerationType;
import jakarta.persistence.Id; import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn; import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.ManyToOne; import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToOne;
import jakarta.persistence.Table; import jakarta.persistence.Table;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Builder; import lombok.Builder;
import lombok.Data; import lombok.Data;
...@@ -33,28 +40,71 @@ public class Race implements Serializable { ...@@ -33,28 +40,71 @@ public class Race implements Serializable {
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id; private Long id;
@NotNull @OneToOne(cascade = CascadeType.ALL)
@Enumerated(EnumType.STRING) @JoinColumn(name = "race_info_id")
private Location location; private RaceInfo raceInfo;
@NotNull @OneToOne(cascade = CascadeType.ALL)
@Column(length = 100, nullable = false) @JoinColumn(name = "driver_info_one_id")
private String name; private RaceDriverinfo driver1;
@ManyToOne(fetch = FetchType.EAGER) @OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "driver_one_id") @JoinColumn(name = "driver_info_two_id")
private Driver driver1; private RaceDriverinfo driver2;
@ManyToOne(fetch = FetchType.EAGER) /**
@JoinColumn(name = "car_one_id") * Information about race driver.
private Car car1; */
@Entity
@Table(name = "racedriverinfo")
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public static class RaceDriverinfo implements Serializable {
@ManyToOne(fetch = FetchType.EAGER) @Id
@JoinColumn(name = "driver_two_id") @GeneratedValue(strategy = GenerationType.IDENTITY)
private Driver driver2; private Long id;
@ManyToOne(fetch = FetchType.EAGER) @ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "car_two_id") @JoinColumn(name = "driver_one_id")
private Car car2; private Driver driver;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "car_one_id")
private Car car;
@Min(1)
@Max(20)
private int finalPosition;
}
/**
* Information about race.
*/
@Entity
@Table(name = "raceinfo")
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public static class RaceInfo implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
@Enumerated(EnumType.STRING)
private Location location;
@NotNull
@Column(length = 100, nullable = false)
private String name;
@NotNull
private Long prizePool;
}
} }
package cz.muni.pa165.race.data.repository; package cz.muni.pa165.race.data.repository;
import cz.muni.pa165.common_library.racecomponents.Location;
import cz.muni.pa165.common_library.racecomponents.Race; import cz.muni.pa165.common_library.racecomponents.Race;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
/** /**
* Race repository. * Race repository.
*/ */
@Repository @Repository
// TODO queries na assignovanie driverov
public interface RaceRepository extends JpaRepository<Race, Long> { public interface RaceRepository extends JpaRepository<Race, Long> {
@Query("SELECT r FROM Race r"
+ " JOIN r.raceInfo ri "
+ " JOIN r.driver1 do "
+ " JOIN r.driver2 dt "
+ "WHERE ri.location = :location AND (do.id = :driverId OR dt.id = :driverId)")
List<Race> findAllRacesOfDriverInLocation(@Param("location") Location raceLocation,
@Param("driverId") long driverId);
} }
package cz.muni.pa165.race.service; package cz.muni.pa165.race.service;
import cz.muni.pa165.common_library.dtos.RaceDriverCarDto;
import cz.muni.pa165.common_library.dtos.RaceDto; import cz.muni.pa165.common_library.dtos.RaceDto;
import cz.muni.pa165.common_library.exception.DatabaseException; import cz.muni.pa165.common_library.exception.DatabaseException;
import cz.muni.pa165.common_library.racecomponents.Car; import cz.muni.pa165.common_library.racecomponents.Car;
...@@ -58,7 +59,7 @@ public class RaceService { ...@@ -58,7 +59,7 @@ public class RaceService {
.orElseThrow(() -> new DatabaseException("Driver not found")); .orElseThrow(() -> new DatabaseException("Driver not found"));
var race = raceRepository.findById(raceId) var race = raceRepository.findById(raceId)
.orElseThrow(() -> new DatabaseException("Race not found")); .orElseThrow(() -> new DatabaseException("Race not found"));
race.setDriver1(driver); race.getDriver1().setDriver(driver);
raceRepository.save(race); raceRepository.save(race);
return "Driver with id: " + driverId + "was succesfully assigned to race with id: " return "Driver with id: " + driverId + "was succesfully assigned to race with id: "
+ raceId + " as driver one"; + raceId + " as driver one";
...@@ -72,7 +73,7 @@ public class RaceService { ...@@ -72,7 +73,7 @@ public class RaceService {
.orElseThrow(() -> new DatabaseException("Driver not found")); .orElseThrow(() -> new DatabaseException("Driver not found"));
var race = raceRepository.findById(raceId) var race = raceRepository.findById(raceId)
.orElseThrow(() -> new DatabaseException("Race not found")); .orElseThrow(() -> new DatabaseException("Race not found"));
race.setDriver2(driver); race.getDriver2().setDriver(driver);
raceRepository.save(race); raceRepository.save(race);
return "Driver with id: " + driverId + "was succesfully assigned to race with id: " return "Driver with id: " + driverId + "was succesfully assigned to race with id: "
+ raceId + " as driver two"; + raceId + " as driver two";
...@@ -102,18 +103,31 @@ public class RaceService { ...@@ -102,18 +103,31 @@ public class RaceService {
private Race convertRaceDto(RaceDto raceDto) { private Race convertRaceDto(RaceDto raceDto) {
var race = Race.builder() var race = Race.builder()
.id(raceDto.getId()) .id(raceDto.getId())
.location(raceDto.getLocation())
.name(raceDto.getName())
.build(); .build();
var raceInfo = Race.RaceInfo
.builder()
.id(raceDto.getId())
.name(raceDto.getRaceInfo().getName())
.location(raceDto.getRaceInfo().getLocation())
.prizePool(raceDto.getRaceInfo().getPrizePool())
.build();
race.setRaceInfo(raceInfo);
if (raceDto.getDriverOne() != null) { if (raceDto.getDriverOne() != null) {
race.setDriver1(getDriver(raceDto.getDriverOne())); race.setDriver1(Race.RaceDriverinfo
race.setCar1(getCar(raceDto.getDriverOne())); .builder()
.driver(getDriver(raceDto.getDriverOne()))
.car(getCar(raceDto.getDriverOne()))
.build());
} }
if (raceDto.getDriverTwo() != null) { if (raceDto.getDriverTwo() != null) {
race.setDriver2(getDriver(raceDto.getDriverTwo())); race.setDriver2(Race.RaceDriverinfo
race.setCar2(getCar(raceDto.getDriverTwo())); .builder()
.driver(getDriver(raceDto.getDriverTwo()))
.car(getCar(raceDto.getDriverTwo()))
.build());
} }
return race; return race;
...@@ -122,36 +136,39 @@ public class RaceService { ...@@ -122,36 +136,39 @@ public class RaceService {
private RaceDto convertRace(Race race) { private RaceDto convertRace(Race race) {
var raceDto = RaceDto.builder() var raceDto = RaceDto.builder()
.id(race.getId()) .id(race.getId())
.name(race.getName()) .raceInfo(RaceDto.RaceInfo.builder()
.location(race.getLocation()) .name(race.getRaceInfo().getName())
.location(race.getRaceInfo().getLocation())
.prizePool(race.getRaceInfo().getPrizePool())
.build())
.build(); .build();
if (race.getCar1() != null && race.getDriver1() != null) { if (race.getDriver1() != null && race.getDriver1().getCar() != null) {
raceDto.setDriverOne(DriverCarDto.builder() raceDto.setDriverOne(RaceDriverCarDto.builder()
.carId(race.getCar1().getId()) .carId(race.getDriver1().getCar().getId())
.driverId(race.getDriver1().getId()) .driverId(race.getDriver1().getId())
.driverName(race.getDriver1().getName() .driverName(race.getDriver1().getDriver().getName()
+ " " + race.getDriver1().getSurname()) + " " + race.getDriver1().getDriver().getSurname())
.build()); .build());
} }
if (race.getCar2() != null && race.getDriver2() != null) { if (race.getDriver2() != null && race.getDriver2().getCar() != null) {
raceDto.setDriverTwo(DriverCarDto.builder() raceDto.setDriverTwo(RaceDriverCarDto.builder()
.carId(race.getCar2().getId()) .carId(race.getDriver2().getCar().getId())
.driverId(race.getDriver2().getId()) .driverId(race.getDriver2().getId())
.driverName(race.getDriver2().getName() .driverName(race.getDriver2().getDriver().getName()
+ " " + race.getDriver2().getSurname()) + " " + race.getDriver2().getDriver().getSurname())
.build()); .build());
} }
return raceDto; return raceDto;
} }
private Driver getDriver(DriverCarDto driverCarDto) { private Driver getDriver(RaceDriverCarDto driverCarDto) {
return driverRepository.findById(driverCarDto.getDriverId()).orElseThrow( return driverRepository.findById(driverCarDto.getDriverId()).orElseThrow(
() -> new DatabaseException("Something went wrong when" () -> new DatabaseException("Something went wrong when"
+ "finding the driver with id: " + driverCarDto.getDriverId() + " in the database.")); + "finding the driver with id: " + driverCarDto.getDriverId() + " in the database."));
} }
private Car getCar(DriverCarDto driverCarDto) { private Car getCar(RaceDriverCarDto driverCarDto) {
return carRepository.findById(driverCarDto.getCarId()).orElseThrow( return carRepository.findById(driverCarDto.getCarId()).orElseThrow(
() -> new DatabaseException("Something went wrong when" () -> new DatabaseException("Something went wrong when"
+ "finding the car with id: " + driverCarDto.getCarId() + " in the database.")); + "finding the car with id: " + driverCarDto.getCarId() + " in the database."));
......
...@@ -5,8 +5,6 @@ import cz.muni.pa165.common_library.dtos.SeasonDto; ...@@ -5,8 +5,6 @@ import cz.muni.pa165.common_library.dtos.SeasonDto;
import cz.muni.pa165.common_library.exception.DatabaseException; import cz.muni.pa165.common_library.exception.DatabaseException;
import cz.muni.pa165.common_library.racecomponents.Race; import cz.muni.pa165.common_library.racecomponents.Race;
import cz.muni.pa165.common_library.racecomponents.Season; import cz.muni.pa165.common_library.racecomponents.Season;
import cz.muni.pa165.race.api.RaceNameDto;
import cz.muni.pa165.race.api.SeasonDto;
import cz.muni.pa165.race.data.repository.RaceRepository; import cz.muni.pa165.race.data.repository.RaceRepository;
import cz.muni.pa165.race.data.repository.SeasonRepository; import cz.muni.pa165.race.data.repository.SeasonRepository;
import java.util.List; import java.util.List;
...@@ -109,7 +107,7 @@ public class SeasonService implements SeasonServiceInterface { ...@@ -109,7 +107,7 @@ public class SeasonService implements SeasonServiceInterface {
.races(season.getRaces() .races(season.getRaces()
.stream() .stream()
.map(x -> RaceNameDto.builder() .map(x -> RaceNameDto.builder()
.name(x.getName()) .name(x.getRaceInfo().getName())
.id(x.getId()) .id(x.getId())
.build()) .build())
.toList()) .toList())
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment