diff --git a/car/src/main/java/cz/muni/pa165/car/rest/CarController.java b/car/src/main/java/cz/muni/pa165/car/rest/CarController.java index 8dd02ec82a53c8306e3b962cea4889eb16a37001..893170fd6bd088c974d2bbb69d14bb7f352d0242 100644 --- a/car/src/main/java/cz/muni/pa165/car/rest/CarController.java +++ b/car/src/main/java/cz/muni/pa165/car/rest/CarController.java @@ -10,6 +10,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.DeleteMapping; 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; @@ -63,13 +64,13 @@ public class CarController { /** * Retrieves a car by its ID. * - * @param carId the ID of the car to retrieve + * @param id the ID of the car to retrieve * @return a ResponseEntity containing the DTO of the retrieved car */ @Operation(summary = "Get a car") - @GetMapping(path = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity<CarDto> getCar(@RequestParam Long carId) { - return ResponseEntity.ok(carService.getCarById(carId)); + @GetMapping(path = "/id={id}", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity<CarDto> getCar(@PathVariable("id") Long id) { + return ResponseEntity.ok(carService.getCarById(id)); } /** diff --git a/common_library/src/main/java/cz/muni/pa165/common_library/dtos/RaceDriverCarDto.java b/common_library/src/main/java/cz/muni/pa165/common_library/dtos/RaceDriverCarDto.java index a950fc9846908df7655d8e9d907bcc5b430eb793..0e635504e632573c917679554c2b16ccd4e31935 100644 --- a/common_library/src/main/java/cz/muni/pa165/common_library/dtos/RaceDriverCarDto.java +++ b/common_library/src/main/java/cz/muni/pa165/common_library/dtos/RaceDriverCarDto.java @@ -12,17 +12,12 @@ import lombok.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; + Integer position; } diff --git a/common_library/src/main/java/cz/muni/pa165/common_library/exception/BadRequestException.java b/common_library/src/main/java/cz/muni/pa165/common_library/exception/BadRequestException.java new file mode 100644 index 0000000000000000000000000000000000000000..4fd9c87517850fa07a1b72735a1ba73a78ea4825 --- /dev/null +++ b/common_library/src/main/java/cz/muni/pa165/common_library/exception/BadRequestException.java @@ -0,0 +1,11 @@ +package cz.muni.pa165.common_library.exception; + +/** + * Exception when request is not valid. + */ +public class BadRequestException extends RuntimeException { + + public BadRequestException(String message) { + super(message); + } +} diff --git a/common_library/src/main/resources.mv.db b/common_library/src/main/resources.mv.db index fbeb7a5e41998599f27bf4c92dc39049abf06e62..1182c1e131cc2956fe8d996155055527cf19a6b7 100644 Binary files a/common_library/src/main/resources.mv.db and b/common_library/src/main/resources.mv.db differ diff --git a/common_library/src/main/resources.trace.db b/common_library/src/main/resources.trace.db index c23aec91212598ef4d254120d1666357f7e0e0b2..1821ae2dbbf3854b6affcf9d2504b39511a2392a 100644 --- a/common_library/src/main/resources.trace.db +++ b/common_library/src/main/resources.trace.db @@ -489,3 +489,146 @@ org.h2.jdbc.JdbcSQLNonTransientConnectionException: Database is already closed ( at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.base/java.lang.Thread.run(Thread.java:833) +2023-04-16 00:27:08 database: flush +org.h2.message.DbException: General error: "org.h2.mvstore.MVStoreException: The file is locked: C:/Users/diana/pa165/pa165-formula-one-team/common_library/src/main/resources.mv.db [2.1.214/7]" [50000-214] + at org.h2.message.DbException.get(DbException.java:212) + at org.h2.message.DbException.convert(DbException.java:395) + at org.h2.mvstore.db.Store.lambda$new$0(Store.java:125) + at org.h2.mvstore.MVStore.handleException(MVStore.java:3318) + at org.h2.mvstore.MVStore.panic(MVStore.java:593) + at org.h2.mvstore.MVStore.<init>(MVStore.java:469) + at org.h2.mvstore.MVStore$Builder.open(MVStore.java:4082) + at org.h2.mvstore.db.Store.<init>(Store.java:136) + at org.h2.engine.Database.<init>(Database.java:324) + at org.h2.engine.Engine.openSession(Engine.java:92) + at org.h2.engine.Engine.openSession(Engine.java:222) + at org.h2.engine.Engine.createSession(Engine.java:201) + at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:338) + at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:122) + at org.h2.Driver.connect(Driver.java:59) + at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) + at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:359) + at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:201) + at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:470) + at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:561) + at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:100) + at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) + at org.springframework.jdbc.datasource.DataSourceUtils.fetchConnection(DataSourceUtils.java:159) + at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:117) + at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80) + at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:330) + at org.springframework.boot.jdbc.EmbeddedDatabaseConnection.isEmbedded(EmbeddedDatabaseConnection.java:168) + at org.springframework.boot.autoconfigure.orm.jpa.HibernateDefaultDdlAutoProvider.getDefaultDdlAuto(HibernateDefaultDdlAutoProvider.java:42) + at org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration.lambda$getVendorProperties$1(HibernateJpaConfiguration.java:129) + at org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings.getDdlAuto(HibernateSettings.java:41) + at org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties.determineDdlAuto(HibernateProperties.java:118) + at org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties.getAdditionalProperties(HibernateProperties.java:87) + at org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties.determineHibernateProperties(HibernateProperties.java:80) + at org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration.getVendorProperties(HibernateJpaConfiguration.java:130) + at org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.entityManagerFactory(JpaBaseConfiguration.java:131) + at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) + at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) + at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) + at java.base/java.lang.reflect.Method.invoke(Method.java:568) + at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:139) + at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) + at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:645) + at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1325) + at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1162) + at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:562) + at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:522) + at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) + at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) + at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) + at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) + at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1132) + at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:907) + at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:584) + at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) + at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732) + at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434) + at org.springframework.boot.SpringApplication.run(SpringApplication.java:310) + at org.springframework.boot.SpringApplication.run(SpringApplication.java:1304) + at org.springframework.boot.SpringApplication.run(SpringApplication.java:1293) + at cz.muni.pa165.race.App.main(App.java:28) +Caused by: org.h2.jdbc.JdbcSQLNonTransientException: General error: "org.h2.mvstore.MVStoreException: The file is locked: C:/Users/diana/pa165/pa165-formula-one-team/common_library/src/main/resources.mv.db [2.1.214/7]" [50000-214] + at org.h2.message.DbException.getJdbcSQLException(DbException.java:554) + at org.h2.message.DbException.getJdbcSQLException(DbException.java:477) + ... 60 more +Caused by: org.h2.mvstore.MVStoreException: The file is locked: C:/Users/diana/pa165/pa165-formula-one-team/common_library/src/main/resources.mv.db [2.1.214/7] + at org.h2.mvstore.DataUtils.newMVStoreException(DataUtils.java:1004) + at org.h2.mvstore.FileStore.open(FileStore.java:178) + at org.h2.mvstore.FileStore.open(FileStore.java:128) + at org.h2.mvstore.MVStore.<init>(MVStore.java:452) + ... 54 more +2023-04-16 00:27:10 database: flush +org.h2.message.DbException: General error: "org.h2.mvstore.MVStoreException: The file is locked: C:/Users/diana/pa165/pa165-formula-one-team/common_library/src/main/resources.mv.db [2.1.214/7]" [50000-214] + at org.h2.message.DbException.get(DbException.java:212) + at org.h2.message.DbException.convert(DbException.java:395) + at org.h2.mvstore.db.Store.lambda$new$0(Store.java:125) + at org.h2.mvstore.MVStore.handleException(MVStore.java:3318) + at org.h2.mvstore.MVStore.panic(MVStore.java:593) + at org.h2.mvstore.MVStore.<init>(MVStore.java:469) + at org.h2.mvstore.MVStore$Builder.open(MVStore.java:4082) + at org.h2.mvstore.db.Store.<init>(Store.java:136) + at org.h2.engine.Database.<init>(Database.java:324) + at org.h2.engine.Engine.openSession(Engine.java:92) + at org.h2.engine.Engine.openSession(Engine.java:222) + at org.h2.engine.Engine.createSession(Engine.java:201) + at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:338) + at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:122) + at org.h2.Driver.connect(Driver.java:59) + at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) + at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:359) + at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:201) + at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:470) + at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:561) + at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:100) + at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) + at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) + at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:284) + at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:177) + at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:36) + at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:119) + at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:255) + at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:230) + at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:207) + at org.hibernate.boot.model.relational.Database.<init>(Database.java:44) + at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.getDatabase(InFlightMetadataCollectorImpl.java:218) + at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.<init>(InFlightMetadataCollectorImpl.java:191) + at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:138) + at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1348) + at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1419) + at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:66) + at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:376) + at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) + at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396) + at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:352) + at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1798) + at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1748) + at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:600) + at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:522) + at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) + at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) + at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) + at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) + at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1132) + at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:907) + at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:584) + at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) + at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732) + at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434) + at org.springframework.boot.SpringApplication.run(SpringApplication.java:310) + at org.springframework.boot.SpringApplication.run(SpringApplication.java:1304) + at org.springframework.boot.SpringApplication.run(SpringApplication.java:1293) + at cz.muni.pa165.race.App.main(App.java:28) +Caused by: org.h2.jdbc.JdbcSQLNonTransientException: General error: "org.h2.mvstore.MVStoreException: The file is locked: C:/Users/diana/pa165/pa165-formula-one-team/common_library/src/main/resources.mv.db [2.1.214/7]" [50000-214] + at org.h2.message.DbException.getJdbcSQLException(DbException.java:554) + at org.h2.message.DbException.getJdbcSQLException(DbException.java:477) + ... 59 more +Caused by: org.h2.mvstore.MVStoreException: The file is locked: C:/Users/diana/pa165/pa165-formula-one-team/common_library/src/main/resources.mv.db [2.1.214/7] + at org.h2.mvstore.DataUtils.newMVStoreException(DataUtils.java:1004) + at org.h2.mvstore.FileStore.open(FileStore.java:178) + at org.h2.mvstore.FileStore.open(FileStore.java:128) + at org.h2.mvstore.MVStore.<init>(MVStore.java:452) + ... 53 more diff --git a/driver/src/main/resources/application.yml b/driver/src/main/resources/application.yml index 421c1494b0c391b664d3c8ef1495b200c904a8ea..5506c8f217a43b34f91fbd7999c8d5bb0c2fbae6 100644 --- a/driver/src/main/resources/application.yml +++ b/driver/src/main/resources/application.yml @@ -16,7 +16,7 @@ spring: hibernate: ddl-auto: update datasource: - url: jdbc:h2:.\common_library\src\main\resources;MODE=PostgreSQL + url: jdbc:h2:mem:driver;MODE=PostgreSQL driverClassName: org.h2.Driver username: admin password: admin diff --git a/race/src/main/java/cz/muni/pa165/race/data/model/Race.java b/race/src/main/java/cz/muni/pa165/race/data/model/Race.java index 16541e30267d56edc756d70f91a0800ca53e0abb..e158c0f1934cd7d407ef4c0ad48f9f9a291e5530 100644 --- a/race/src/main/java/cz/muni/pa165/race/data/model/Race.java +++ b/race/src/main/java/cz/muni/pa165/race/data/model/Race.java @@ -14,6 +14,7 @@ import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToOne; +import jakarta.persistence.OneToMany; import jakarta.persistence.OneToOne; import jakarta.persistence.Table; import jakarta.validation.constraints.Max; @@ -45,11 +46,11 @@ public class Race implements Serializable { private RaceInfo raceInfo; @OneToOne(cascade = CascadeType.ALL) - @JoinColumn(name = "driver_info_one_id") + @JoinColumn(name = "driver_one_id") private RaceDriverinfo driver1; @OneToOne(cascade = CascadeType.ALL) - @JoinColumn(name = "driver_info_two_id") + @JoinColumn(name = "driver_two_id") private RaceDriverinfo driver2; /** @@ -67,17 +68,13 @@ public class Race implements Serializable { @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; - @ManyToOne(fetch = FetchType.EAGER) - @JoinColumn(name = "driver_one_id") - private Driver driver; + private Long driverId; - @ManyToOne(fetch = FetchType.EAGER) - @JoinColumn(name = "car_one_id") - private Car car; + private Long carId; @Min(1) @Max(20) - private int finalPosition; + private Integer finalPosition; } /** diff --git a/race/src/main/java/cz/muni/pa165/race/rest/RaceController.java b/race/src/main/java/cz/muni/pa165/race/rest/RaceController.java index 24ce9dd85f436d57519406d49c03ef366d6e422a..94b7693e2e13a22f2576440950ce28251ac2d7d6 100644 --- a/race/src/main/java/cz/muni/pa165/race/rest/RaceController.java +++ b/race/src/main/java/cz/muni/pa165/race/rest/RaceController.java @@ -42,14 +42,14 @@ public class RaceController { @Operation(summary = "Delete a race") @DeleteMapping(path = "/", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity<String> deleteRace(@Valid @RequestParam long raceId) { + public ResponseEntity<String> deleteRace(@Valid @RequestParam Long raceId) { return ResponseEntity.ok(raceService.deleteRace(raceId)); } @Operation(summary = "Get a race") @GetMapping(path = "/id", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity<RaceDto> getRace(@Valid @RequestParam long raceId) { + public ResponseEntity<RaceDto> getRace(@Valid @RequestParam Long raceId) { return ResponseEntity.ok(raceService.findRaceById(raceId)); } @@ -63,16 +63,18 @@ public class RaceController { @Operation(summary = "Assign driver as driver number one for a race") @PatchMapping(path = "/assignDriverOne", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity<String> assignDriverOne(@RequestParam long driverOneId, - @RequestParam long raceId) { - return ResponseEntity.ok(raceService.assignDriverOne(driverOneId, raceId)); + public ResponseEntity<RaceDto> assignDriverOne(@RequestParam Long driverOneId, + @RequestParam Long raceId, + @RequestParam Long carId) { + return ResponseEntity.ok(raceService.assignDriverOne(driverOneId, raceId, carId)); } @Operation(summary = "Assign driver as a driver number two for a race") @PatchMapping(path = "/assignDriverTwo", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity<String> assignDriverTwo(@RequestParam long driverTwoId, - @RequestParam long raceId) { - return ResponseEntity.ok(raceService.assignDriverTwo(driverTwoId, raceId)); + public ResponseEntity<RaceDto> assignDriverTwo(@RequestParam Long driverTwoId, + @RequestParam Long raceId, + @RequestParam Long carId) { + return ResponseEntity.ok(raceService.assignDriverTwo(driverTwoId, raceId, carId)); } } diff --git a/race/src/main/java/cz/muni/pa165/race/service/RaceService.java b/race/src/main/java/cz/muni/pa165/race/service/RaceService.java index fc1cae9f879e8433f8d848c5e4d9e63a61ae013f..1845ece478fe09afb4025858c2217de5d208f38b 100644 --- a/race/src/main/java/cz/muni/pa165/race/service/RaceService.java +++ b/race/src/main/java/cz/muni/pa165/race/service/RaceService.java @@ -3,6 +3,7 @@ package cz.muni.pa165.race.service; import cz.muni.pa165.car.data.model.Car; import cz.muni.pa165.common_library.dtos.RaceDriverCarDto; import cz.muni.pa165.common_library.dtos.RaceDto; +import cz.muni.pa165.common_library.exception.BadRequestException; import cz.muni.pa165.common_library.exception.DatabaseException; import cz.muni.pa165.driver.data.model.Driver; import cz.muni.pa165.race.data.model.Race; @@ -10,6 +11,7 @@ import cz.muni.pa165.race.data.repository.CarRepository; import cz.muni.pa165.race.data.repository.DriverRepository; import cz.muni.pa165.race.data.repository.RaceRepository; import java.util.List; +import java.util.Objects; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; @@ -59,29 +61,43 @@ public class RaceService { /** * Assigns driver one. */ - public String assignDriverOne(Long driverId, Long raceId) { - var driver = driverRepository.findById(driverId) - .orElseThrow(() -> new DatabaseException("Driver not found")); + public RaceDto assignDriverOne(Long driverId, Long raceId, Long carId) { + var driver = getDriver(driverId); + var car = getCar(carId); var race = raceRepository.findById(raceId) .orElseThrow(() -> new DatabaseException("Race not found")); - race.getDriver1().setDriver(driver); - raceRepository.save(race); - return "Driver with id: " + driverId + "was succesfully assigned to race with id: " - + raceId + " as driver one"; + + if (race.getDriver2() != null && Objects.equals(race.getDriver2().getDriverId(), driverId)) { + throw new BadRequestException("Driver already assigned to the race as driver two"); + } + + if (race.getDriver2() != null && Objects.equals(race.getDriver2().getCarId(), carId)) { + throw new BadRequestException("Car is already assigned to the race for driver two"); + } + race.getDriver1().setDriverId(driver.getId()); + race.getDriver1().setCarId(car.getId()); + return convertRace(raceRepository.save(race)); } /** * Assigns driver two. */ - public String assignDriverTwo(Long driverId, Long raceId) { - var driver = driverRepository.findById(driverId) - .orElseThrow(() -> new DatabaseException("Driver not found")); + public RaceDto assignDriverTwo(Long driverId, Long raceId, Long carId) { + var driver = getDriver(driverId); + var car = getCar(carId); var race = raceRepository.findById(raceId) .orElseThrow(() -> new DatabaseException("Race not found")); - race.getDriver2().setDriver(driver); - raceRepository.save(race); - return "Driver with id: " + driverId + "was succesfully assigned to race with id: " - + raceId + " as driver two"; + + if (race.getDriver1() != null && Objects.equals(race.getDriver1().getDriverId(), driverId)) { + throw new BadRequestException("Driver already assigned to the race as driver one"); + } + if (race.getDriver1() != null && Objects.equals(race.getDriver1().getCarId(), carId)) { + throw new BadRequestException("Car is already assigned to the race for driver two"); + } + + race.getDriver2().setDriverId(driver.getId()); + race.getDriver2().setCarId(car.getId()); + return convertRace(raceRepository.save(race)); } /** @@ -122,17 +138,21 @@ public class RaceService { if (raceDto.getDriverOne() != null) { race.setDriver1(Race.RaceDriverinfo .builder() - .driver(getDriver(raceDto.getDriverOne())) - .car(getCar(raceDto.getDriverOne())) + .driverId(raceDto.getDriverOne().getDriverId()) + .carId(raceDto.getDriverOne().getCarId()) .build()); + } else { + race.setDriver1(Race.RaceDriverinfo.builder().build()); } if (raceDto.getDriverTwo() != null) { race.setDriver2(Race.RaceDriverinfo .builder() - .driver(getDriver(raceDto.getDriverTwo())) - .car(getCar(raceDto.getDriverTwo())) + .driverId(raceDto.getDriverTwo().getDriverId()) + .carId(raceDto.getDriverTwo().getCarId()) .build()); + } else { + race.setDriver2(Race.RaceDriverinfo.builder().build()); } return race; @@ -147,35 +167,29 @@ public class RaceService { .prizePool(race.getRaceInfo().getPrizePool()) .build()) .build(); - if (race.getDriver1() != null && race.getDriver1().getCar() != null) { + if (race.getDriver1() != null) { raceDto.setDriverOne(RaceDriverCarDto.builder() - .carId(race.getDriver1().getCar().getId()) - .driverId(race.getDriver1().getId()) - .driverName(race.getDriver1().getDriver().getName() - + " " + race.getDriver1().getDriver().getSurname()) + .carId(race.getDriver1().getCarId()) + .driverId(race.getDriver1().getDriverId()) .build()); } - if (race.getDriver2() != null && race.getDriver2().getCar() != null) { + if (race.getDriver2() != null) { raceDto.setDriverTwo(RaceDriverCarDto.builder() - .carId(race.getDriver2().getCar().getId()) - .driverId(race.getDriver2().getId()) - .driverName(race.getDriver2().getDriver().getName() - + " " + race.getDriver2().getDriver().getSurname()) + .carId(race.getDriver2().getCarId()) + .driverId(race.getDriver2().getCarId()) .build()); } return raceDto; } - - private Driver getDriver(RaceDriverCarDto driverCarDto) { - return driverRepository.findById(driverCarDto.getDriverId()).orElseThrow( - () -> new DatabaseException("Something went wrong when" - + "finding the driver with id: " + driverCarDto.getDriverId() + " in the database.")); + private Driver getDriver(Long driverId) { + var response = + client.getForEntity("http://localhost:8083/driver/get/id=" + driverId, Driver.class); + return response.getBody(); } - private Car getCar(RaceDriverCarDto driverCarDto) { - return carRepository.findById(driverCarDto.getCarId()).orElseThrow( - () -> new DatabaseException("Something went wrong when" - + "finding the car with id: " + driverCarDto.getCarId() + " in the database.")); + private Car getCar(Long carId) { + var response = client.getForEntity("http://localhost:8082/car/id=" + carId, Car.class); + return response.getBody(); } }