Skip to content
Snippets Groups Projects
Commit a8f14cb7 authored by Oto Stanko's avatar Oto Stanko
Browse files

Merge branch '13_fixes_by_diana' into 13_fixes_oto

parents a2e85324 91ea4192
No related branches found
No related tags found
3 merge requests!60Docker,!2913 fixes oto,!2713 fixes by tomas
Showing with 80 additions and 14 deletions
package cz.muni.pa165.common_library;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.time.LocalDateTime;
import lombok.Getter;
public class ExError {
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd hh:mm:ss")
private final LocalDateTime timestamp = LocalDateTime.now();
@Getter
private final String message;
public ExError(String message) {
this.message = message;
}
}
package cz.muni.pa165.common_library.racecomponents;
import jakarta.annotation.Nonnull;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
......@@ -8,6 +9,7 @@ import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Null;
import java.io.Serializable;
import java.util.List;
import lombok.AllArgsConstructor;
......@@ -31,9 +33,9 @@ public class Season implements Serializable {
private Long id;
@NotNull
private int year;
private int seasonYear;
@NotNull
@OneToMany(fetch = FetchType.LAZY)
@Nonnull
@OneToMany(fetch = FetchType.EAGER)
private List<Race> races;
}
......@@ -48,6 +48,11 @@
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.7.1</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>17</maven.compiler.source>
......
package cz.muni.pa165.race;
import cz.muni.pa165.common_library.ExError;
import cz.muni.pa165.common_library.exceptions.DatabaseException;
import jakarta.persistence.EntityNotFoundException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;
@RestControllerAdvice
public class RestExceptionHandler {
@ExceptionHandler(EntityNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public ResponseEntity<ExError> handleEntityNotFoundException(EntityNotFoundException ex) {
System.out.println();
return new ResponseEntity<>(
new ExError(ex.getMessage()), new HttpHeaders(), HttpStatus.NOT_FOUND);
}
@ExceptionHandler(DatabaseException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public ResponseEntity<ExError> handleDatabaseException(DatabaseException ex) {
return new ResponseEntity<>(
new ExError(ex.getMessage()), new HttpHeaders(), HttpStatus.NOT_FOUND);
}
@ExceptionHandler(NoHandlerFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public ResponseEntity<ExError> handleDatabaseException(NoHandlerFoundException ex) {
return new ResponseEntity<>(
new ExError(ex.getMessage()), new HttpHeaders(), HttpStatus.NOT_FOUND);
}
}
......@@ -18,7 +18,7 @@ import lombok.NoArgsConstructor;
public class RaceDto {
@Schema(description = "race id", example = "1")
long id;
Long id;
@NotNull
@Schema(description = "race location", example = "Monaco")
......
......@@ -21,7 +21,7 @@ import lombok.NonNull;
public class SeasonDto {
@Schema(description = "season id", example = "1")
long id;
Long id;
@NotNull
@Min(1946)
......
......@@ -2,9 +2,7 @@ package cz.muni.pa165.race.data.repository;
import cz.muni.pa165.common_library.racecomponents.Race;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
/**
* Race repository.
......
......@@ -5,6 +5,7 @@ import cz.muni.pa165.race.api.SeasonDto;
import cz.muni.pa165.race.service.SeasonServiceInterface;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import java.util.List;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
......@@ -45,9 +46,9 @@ public class SeasonController {
}
@Operation(summary = "Get all seasons")
@GetMapping(path = "/", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Season> getAllSeasons() {
return ResponseEntity.ok(new Season());
@GetMapping(path = "/")
public ResponseEntity<List<SeasonDto>> getAllSeasons() {
return ResponseEntity.ok(seasonService.getAllSeasons());
}
@Operation(summary = "Delete a season")
......
......@@ -37,6 +37,7 @@ public class RaceService {
* @return inserted race.
*/
public RaceDto postRace(RaceDto raceDto) {
raceDto.setId(null);
return convertRace(raceRepository.save(convertRaceDto(raceDto)));
}
......
......@@ -33,6 +33,7 @@ public class SeasonService implements SeasonServiceInterface {
* @return inserted season.
*/
public SeasonDto postSeason(SeasonDto seasonDto) {
seasonDto.setId(null);
return seasonConverter(seasonRepository.save(seasonDtoConverter(seasonDto)));
}
......@@ -81,20 +82,22 @@ public class SeasonService implements SeasonServiceInterface {
}
Season seasonDtoConverter(SeasonDto seasonDto) {
return Season.builder()
var a = Season.builder()
.id(seasonDto.getId())
.year(seasonDto.getYear())
.seasonYear(seasonDto.getYear())
.races(seasonDto.getRaces()
.stream()
.map(this::getRace)
.toList())
.build();
System.out.println();
return a;
}
SeasonDto seasonConverter(Season season) {
return SeasonDto.builder()
.id(season.getId())
.year(season.getYear())
.year(season.getSeasonYear())
.races(season.getRaces()
.stream()
.map(x -> RaceNameDto.builder()
......@@ -108,6 +111,6 @@ public class SeasonService implements SeasonServiceInterface {
private Race getRace(RaceNameDto raceNameDto) {
return raceRepository.findById(raceNameDto.getId()).orElseThrow(
() -> new DatabaseException("Something went wrong when"
+ "finding the race with id: " + raceNameDto.getId() + " in the database."));
+ " finding the race with id: " + raceNameDto.getId() + " in the database."));
}
}
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