done back (need more tests)

This commit is contained in:
root 2023-04-20 23:59:45 +04:00
parent 425b8ffc97
commit 0e38b9586b
27 changed files with 547 additions and 166 deletions

View File

@ -1,51 +1,36 @@
package com.subd.subd.Controllers;
import com.subd.subd.Exceptions.IdNotFoundException;
import com.subd.subd.Dtos.CarDto;
import com.subd.subd.Models.Car;
import com.subd.subd.Repositories.CarRepository;
import com.subd.subd.Services.CarService;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
public class CarController {
private final CarRepository repository;
CarController(CarRepository repository) {
this.repository = repository;
private final CarService CarService;
public CarController(CarService CarService) {
this.CarService = CarService;
}
// Aggregate root
// tag::get-aggregate-root[]
@GetMapping("/car")
List<Car> all() {
return repository.findAll();
}
// end::get-aggregate-root[]
@PostMapping("/car")
Car newCar(@RequestBody Car newCar) {
return repository.save(newCar);
}
// Single item
@GetMapping("/car/{id}")
Car one(@PathVariable Long id) {
return repository.findById(id)
.orElseThrow(() -> new IdNotFoundException(id));
public Car getCar(@PathVariable Long id) {
return CarService.findCar(id);
}
@GetMapping("/car")
public List<Car> getCars() {
return CarService.findAllCars();
}
@PostMapping("/car")
public Car createCar(@RequestBody CarDto carDto) {
return CarService.addCar(carDto.getGosNumber(), carDto.getVin(), carDto.getDriverId());
}
@PutMapping("/car/{id}")
Car replaceCar(@RequestBody Car newCar, @PathVariable Long id) {
return repository.findById(id)
.map(car -> {
car.setGosNumber(newCar.getGosNumber());
car.setVin(newCar.getVin());
car.setDriver(newCar.getDriver());
return repository.save(car);
})
.orElseGet(() -> {
newCar.setId(id);
return repository.save(newCar);
});
public Car updateCar(@PathVariable Long id, @RequestBody CarDto carDto) {
return CarService.updateCar(id, carDto.getGosNumber(), carDto.getVin(), carDto.getDriverId());
}
@DeleteMapping("/car/{id}")
void deleteCar(@PathVariable Long id) {
repository.deleteById(id);
public Car deleteCar(@PathVariable Long id) {
return CarService.deleteCar(id);
}
}

View File

@ -1,51 +1,35 @@
package com.subd.subd.Controllers;
import com.subd.subd.Exceptions.IdNotFoundException;
import com.subd.subd.Models.Client;
import com.subd.subd.Repositories.ClientRepository;
import com.subd.subd.Services.ClientService;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
public class ClientController {
private final ClientRepository repository;
ClientController(ClientRepository repository) {
this.repository = repository;
private final ClientService ClientService;
public ClientController(ClientService ClientService) {
this.ClientService = ClientService;
}
// Aggregate root
// tag::get-aggregate-root[]
@GetMapping("/client")
List<Client> all() {
return repository.findAll();
}
// end::get-aggregate-root[]
@PostMapping("/client")
Client newClient(@RequestBody Client newClient) {
return repository.save(newClient);
}
// Single item
@GetMapping("/client/{id}")
Client one(@PathVariable Long id) {
return repository.findById(id)
.orElseThrow(() -> new IdNotFoundException(id));
public Client getClient(@PathVariable Long id) {
return ClientService.findClient(id);
}
@GetMapping("/client")
public List<Client> getClients() {
return ClientService.findAllClients();
}
@PostMapping("/client")
public Client createClient(@RequestBody Client client) {
return ClientService.addClient(client.getName(), client.getPhone(), client.getEmail());
}
@PutMapping("/client/{id}")
Client replaceClient(@RequestBody Client newClient, @PathVariable Long id) {
return repository.findById(id)
.map(client -> {
client.setName(newClient.getName());
client.setPhone(newClient.getPhone());
client.setEmail(newClient.getEmail());
return repository.save(client);
})
.orElseGet(() -> {
newClient.setId(id);
return repository.save(newClient);
});
public Client updateClient(@PathVariable Long id, @RequestBody Client client) {
return ClientService.updateClient(id, client.getName(), client.getPhone(), client.getEmail());
}
@DeleteMapping("/client/{id}")
void deleteClient(@PathVariable Long id) {
repository.deleteById(id);
public Client deleteClient(@PathVariable Long id) {
return ClientService.deleteClient(id);
}
}

View File

@ -1,52 +1,35 @@
package com.subd.subd.Controllers;
import com.subd.subd.Exceptions.IdNotFoundException;
import com.subd.subd.Models.Driver;
import com.subd.subd.Repositories.DriverRepository;
import com.subd.subd.Services.DriverService;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
public class DriverController {
private final DriverRepository repository;
DriverController(DriverRepository repository) {
this.repository = repository;
private final DriverService DriverService;
public DriverController(DriverService DriverService) {
this.DriverService = DriverService;
}
// Aggregate root
// tag::get-aggregate-root[]
@GetMapping("/driver")
List<Driver> all() {
return repository.findAll();
}
// end::get-aggregate-root[]
@PostMapping("/driver")
Driver newDriver(@RequestBody Driver newDriver) {
return repository.save(newDriver);
}
// Single item
@GetMapping("/driver/{id}")
Driver one(@PathVariable Long id) {
return repository.findById(id)
.orElseThrow(() -> new IdNotFoundException(id));
public Driver getDriver(@PathVariable Long id) {
return DriverService.findDriver(id);
}
@GetMapping("/driver")
public List<Driver> getDrivers() {
return DriverService.findAllDrivers();
}
@PostMapping("/driver")
public Driver createDriver(@RequestBody Driver driver) {
return DriverService.addDriver(driver.getName(), driver.getAge(), driver.getPhone(), driver.getEmail());
}
@PutMapping("/driver/{id}")
Driver replaceDriver(@RequestBody Driver newDriver, @PathVariable Long id) {
return repository.findById(id)
.map(driver -> {
driver.setName(newDriver.getName());
driver.setAge(newDriver.getAge());
driver.setPhone(newDriver.getPhone());
driver.setEmail(newDriver.getEmail());
return repository.save(driver);
})
.orElseGet(() -> {
newDriver.setId(id);
return repository.save(newDriver);
});
public Driver updateDriver(@PathVariable Long id, @RequestBody Driver driver) {
return DriverService.updateDriver(id, driver.getName(), driver.getAge(), driver.getPhone(), driver.getEmail());
}
@DeleteMapping("/driver/{id}")
void deleteClient(@PathVariable Long id) {
repository.deleteById(id);
public Driver deleteDriver(@PathVariable Long id) {
return DriverService.deleteDriver(id);
}
}

View File

@ -1,53 +1,36 @@
package com.subd.subd.Controllers;
import com.subd.subd.Exceptions.IdNotFoundException;
import com.subd.subd.Dtos.OrderDto;
import com.subd.subd.Models.Order;
import com.subd.subd.Repositories.OrderRepository;
import com.subd.subd.Services.OrderService;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
public class OrderController {
private final OrderRepository repository;
OrderController(OrderRepository repository) {
this.repository = repository;
private final OrderService OrderService;
public OrderController(OrderService OrderService) {
this.OrderService = OrderService;
}
// Aggregate root
// tag::get-aggregate-root[]
@GetMapping("/order")
List<Order> all() {
return repository.findAll();
}
// end::get-aggregate-root[]
@PostMapping("/order")
Order newOrder(@RequestBody Order newOrder) {
return repository.save(newOrder);
}
// Single item
@GetMapping("/order/{id}")
Order one(@PathVariable Long id) {
return repository.findById(id)
.orElseThrow(() -> new IdNotFoundException(id));
public Order getOrder(@PathVariable Long id) {
return OrderService.findOrder(id);
}
@GetMapping("/order")
public List<Order> getOrders() {
return OrderService.findAllOrders();
}
@PostMapping("/order")
public Order createOrder(@RequestBody OrderDto orderDto) {
return OrderService.addOrder(orderDto.getValue(), orderDto.getClientId(), orderDto.getSourcePickUpPointId(), orderDto.getDestPickUpPointId(), orderDto.getCarId());
}
@PutMapping("/order/{id}")
Order replaceOrder(@RequestBody Order newOrder, @PathVariable Long id) {
return repository.findById(id)
.map(order -> {
order.setValue(newOrder.getValue());
order.setClient(newOrder.getClient());
order.setSourcePickUpPoint(newOrder.getSourcePickUpPoint());
order.setDestPickUpPoint(newOrder.getDestPickUpPoint());
order.setCar(newOrder.getCar());
return repository.save(order);
})
.orElseGet(() -> {
newOrder.setId(id);
return repository.save(newOrder);
});
public Order updateOrder(@PathVariable Long id, @RequestBody OrderDto orderDto) {
return OrderService.updateOrder(id, orderDto.getValue(), orderDto.getClientId(), orderDto.getSourcePickUpPointId(), orderDto.getDestPickUpPointId(), orderDto.getCarId());
}
@DeleteMapping("/order/{id}")
void deleteOrder(@PathVariable Long id) {
repository.deleteById(id);
public Order deleteOrder(@PathVariable Long id) {
return OrderService.deleteOrder(id);
}
}

View File

@ -12,23 +12,23 @@ public class PickUpPointController {
public PickUpPointController(PickUpPointService PickUpPointService) {
this.PickUpPointService = PickUpPointService;
}
@GetMapping("/{id}")
@GetMapping("/pickUpPoint/{id}")
public PickUpPoint getPickUpPoint(@PathVariable Long id) {
return PickUpPointService.findPickUpPoint(id);
}
@GetMapping
@GetMapping("/pickUpPoint")
public List<PickUpPoint> getPickUpPoints() {
return PickUpPointService.findAllPickUpPoints();
}
@PostMapping
public PickUpPoint createPickUpPoint(@RequestBody PickUpPoint PickUpPoint) {
return PickUpPointService.addPickUpPoint(PickUpPoint.getAddress());
@PostMapping("/pickUpPoint")
public PickUpPoint createPickUpPoint(@RequestBody PickUpPoint pickUpPoint) {
return PickUpPointService.addPickUpPoint(pickUpPoint.getAddress());
}
@PutMapping("/{id}")
public PickUpPoint updatePickUpPoint(@PathVariable Long id, @RequestBody PickUpPoint PickUpPoint) {
return PickUpPointService.updatePickUpPoint(id, PickUpPoint.getAddress());
@PutMapping("/pickUpPoint/{id}")
public PickUpPoint updatePickUpPoint(@PathVariable Long id, @RequestBody PickUpPoint pickUpPoint) {
return PickUpPointService.updatePickUpPoint(id, pickUpPoint.getAddress());
}
@DeleteMapping("/{id}")
@DeleteMapping("/pickUpPoint/{id}")
public PickUpPoint deletePickUpPoint(@PathVariable Long id) {
return PickUpPointService.deletePickUpPoint(id);
}

View File

@ -0,0 +1,22 @@
package com.subd.subd.Dtos;
public class CarDto {
private String gosNumber;
private String vin;
private Long driverId;
public CarDto() {}
public CarDto(String gosNumber, String vin, Long driverId) {
this.gosNumber = gosNumber;
this.vin = vin;
this.driverId = driverId;
}
public String getGosNumber() {
return this.gosNumber;
}
public String getVin() {
return this.vin;
}
public Long getDriverId() {
return this.driverId;
}
}

View File

@ -0,0 +1,32 @@
package com.subd.subd.Dtos;
public class OrderDto {
private Double value;
private Long clientId;
private Long sourcePickUpPointId;
private Long destPickUpPointId;
private Long carId;
public OrderDto() {}
public OrderDto(Double value, Long clientId, Long sourcePickUpPointId, Long destPickUpPointId, Long carId) {
this.value = value;
this.clientId = clientId;
this.sourcePickUpPointId = sourcePickUpPointId;
this.destPickUpPointId = destPickUpPointId;
this.carId = carId;
}
public Double getValue() {
return this.value;
}
public Long getClientId() {
return this.clientId;
}
public Long getSourcePickUpPointId() {
return this.sourcePickUpPointId;
}
public Long getDestPickUpPointId() {
return this.destPickUpPointId;
}
public Long getCarId() {
return this.carId;
}
}

View File

@ -7,11 +7,11 @@ import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
@ControllerAdvice
class IdNotFoundAdvice {
class CarNotFoundAdvice {
@ResponseBody
@ExceptionHandler(IdNotFoundException.class)
@ExceptionHandler(CarNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
String IdNotFoundHandler(IdNotFoundException ex) {
String CarNotFoundHandler(CarNotFoundException ex) {
return ex.getMessage();
}
}
}

View File

@ -0,0 +1,7 @@
package com.subd.subd.Exceptions;
public class CarNotFoundException extends RuntimeException{
public CarNotFoundException(Long id) {
super(String.format("Not found car with id: %s", id));
}
}

View File

@ -0,0 +1,17 @@
package com.subd.subd.Exceptions;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
@ControllerAdvice
class ClientNotFoundAdvice {
@ResponseBody
@ExceptionHandler(ClientNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
String ClientNotFoundHandler(ClientNotFoundException ex) {
return ex.getMessage();
}
}

View File

@ -0,0 +1,7 @@
package com.subd.subd.Exceptions;
public class ClientNotFoundException extends RuntimeException{
public ClientNotFoundException(Long id) {
super(String.format("Not found client with id: %s", id));
}
}

View File

@ -0,0 +1,17 @@
package com.subd.subd.Exceptions;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
@ControllerAdvice
class DriverNotFoundAdvice {
@ResponseBody
@ExceptionHandler(DriverNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
String DriverNotFoundHandler(DriverNotFoundException ex) {
return ex.getMessage();
}
}

View File

@ -0,0 +1,7 @@
package com.subd.subd.Exceptions;
public class DriverNotFoundException extends RuntimeException{
public DriverNotFoundException(Long id) {
super(String.format("Not found driver with id: %s", id));
}
}

View File

@ -1,7 +0,0 @@
package com.subd.subd.Exceptions;
public class IdNotFoundException extends RuntimeException {
public IdNotFoundException(Long id) {
super("Could not find: " + id);
}
}

View File

@ -0,0 +1,17 @@
package com.subd.subd.Exceptions;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
@ControllerAdvice
class OrderNotFoundAdvice {
@ResponseBody
@ExceptionHandler(OrderNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
String OrderNotFoundHandler(OrderNotFoundException ex) {
return ex.getMessage();
}
}

View File

@ -0,0 +1,7 @@
package com.subd.subd.Exceptions;
public class OrderNotFoundException extends RuntimeException{
public OrderNotFoundException(Long id) {
super(String.format("Not found order with id: %s", id));
}
}

View File

@ -0,0 +1,17 @@
package com.subd.subd.Exceptions;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
@ControllerAdvice
class PickUpPointNotFoundAdvice {
@ResponseBody
@ExceptionHandler(PickUpPointNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
String PickUpPointNotFoundHandler(PickUpPointNotFoundException ex) {
return ex.getMessage();
}
}

View File

@ -0,0 +1,7 @@
package com.subd.subd.Exceptions;
public class PickUpPointNotFoundException extends RuntimeException{
public PickUpPointNotFoundException(Long id) {
super(String.format("Not found pickUpPoint with id: %s", id));
}
}

View File

@ -1,5 +1,6 @@
package com.subd.subd.Models;
import com.subd.subd.Dtos.CarDto;
import jakarta.persistence.*;
import java.util.Objects;
@ -16,11 +17,15 @@ public class Car {
@JoinColumn(name = "Driver_id", referencedColumnName = "id")
private Driver driver;
Car() {}
Car(String gosNumber, String vin, Driver driver) {
public Car(String gosNumber, String vin, Driver driver) {
this.gosNumber = gosNumber;
this.vin = vin;
this.driver = driver;
}
public Car(CarDto carDto) {
this.gosNumber = carDto.getGosNumber();
this.vin = carDto.getVin();
}
public Long getId() {
return this.id;
}

View File

@ -20,7 +20,7 @@ public class Client {
@OneToMany(cascade = {CascadeType.MERGE})
private List<Order> orders;
Client() {}
Client(String name, String phone, @Nullable String email) {
public Client(String name, String phone, @Nullable String email) {
this.name = name;
this.phone = phone;
this.email = email;

View File

@ -18,7 +18,7 @@ public class Driver {
private String phone;
private String email;
Driver() {}
Driver(String name, Integer age, String phone, String email) {
public Driver(String name, Integer age, String phone, String email) {
this.name = name;
this.age = age;
this.phone = phone;

View File

@ -24,7 +24,7 @@ public class Order {
@JoinColumn(name = "Car_id", referencedColumnName = "id")
private Car car;
Order() {}
Order(Double value, Client client, PickUpPoint sourcePickUpPoint, PickUpPoint destPickUpPoint, Car car) {
public Order(Double value, Client client, PickUpPoint sourcePickUpPoint, PickUpPoint destPickUpPoint, Car car) {
this.value = value;
this.client = client;
this.sourcePickUpPoint = sourcePickUpPoint;

View File

@ -0,0 +1,79 @@
package com.subd.subd.Services;
import com.subd.subd.Exceptions.CarNotFoundException;
import com.subd.subd.Exceptions.DriverNotFoundException;
import com.subd.subd.Models.Car;
import com.subd.subd.Models.Driver;
import com.subd.subd.Repositories.CarRepository;
import com.subd.subd.Repositories.DriverRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Optional;
@Service
public class CarService {
private final CarRepository carRepository;
private final DriverRepository driverRepository;
public CarService(CarRepository carRepository, DriverRepository driverRepository) {
this.carRepository = carRepository;
this.driverRepository = driverRepository;
}
@Transactional
public Car addCar(String gosNumber, String vin, Long driverId) {
final Driver driver = driverRepository.findById(driverId)
.orElseThrow(() -> new DriverNotFoundException(driverId));
final Car car = new Car(gosNumber, vin, driver);
return carRepository.save(car);
}
@Transactional(readOnly = true)
public Car findCar(Long id) {
final Optional<Car> car = carRepository.findById(id);
return car.orElseThrow(() -> new CarNotFoundException(id));
}
@Transactional(readOnly = true)
public List<Car> findAllCars() {
return carRepository.findAll();
}
@Transactional
public Car updateCar(Long id, String gosNumber, String vin, Long driverId) {
final Car currentCar = findCar(id);
if (gosNumber != null) {
currentCar.setGosNumber(gosNumber);
}
if (vin != null) {
currentCar.setVin(vin);
}
if (driverId != null) {
final Driver driver = driverRepository.findById(driverId)
.orElseThrow(() -> new DriverNotFoundException(driverId));
currentCar.setDriver(driver);
}
return carRepository.save(currentCar);
}
@Transactional
public Driver getDriver(Long carId) {
Car currentCar = carRepository.findById(carId)
.orElseThrow(() -> new CarNotFoundException(carId));
return currentCar.getDriver();
}
@Transactional
public void setDriver(Long carId, Long driverId) {
Car currentCar = carRepository.findById(carId)
.orElseThrow(() -> new CarNotFoundException(carId));
Driver driver = driverRepository.findById(driverId)
.orElseThrow(() -> new DriverNotFoundException(driverId));
currentCar.setDriver(driver);
}
@Transactional
public Car deleteCar(Long id) {
final Car currentCar = findCar(id);
carRepository.delete(currentCar);
return currentCar;
}
@Transactional
public void deleteAllCars() {
carRepository.deleteAll();
}
}

View File

@ -0,0 +1,56 @@
package com.subd.subd.Services;
import com.subd.subd.Exceptions.ClientNotFoundException;
import com.subd.subd.Models.Client;
import com.subd.subd.Repositories.ClientRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Optional;
@Service
public class ClientService {
private final ClientRepository clientRepository;
public ClientService(ClientRepository clientRepository) {
this.clientRepository = clientRepository;
}
@Transactional
public Client addClient(String name, String phone, String email) {
final Client client = new Client(name, phone, email);
return clientRepository.save(client);
}
@Transactional(readOnly = true)
public Client findClient(Long id) {
final Optional<Client> client = clientRepository.findById(id);
return client.orElseThrow(() -> new ClientNotFoundException(id));
}
@Transactional(readOnly = true)
public List<Client> findAllClients() {
return clientRepository.findAll();
}
@Transactional
public Client updateClient(Long id, String name, String phone, String email) {
final Client currentClient = findClient(id);
if (name != null) {
currentClient.setName(name);
}
if (phone != null) {
currentClient.setPhone(phone);
}
if (email != null) {
currentClient.setEmail(email);
}
return clientRepository.save(currentClient);
}
@Transactional
public Client deleteClient(Long id) {
final Client currentClient = findClient(id);
clientRepository.delete(currentClient);
return currentClient;
}
@Transactional
public void deleteAllClients() {
clientRepository.deleteAll();
}
}

View File

@ -0,0 +1,59 @@
package com.subd.subd.Services;
import com.subd.subd.Exceptions.DriverNotFoundException;
import com.subd.subd.Models.Driver;
import com.subd.subd.Repositories.DriverRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Optional;
@Service
public class DriverService {
private final DriverRepository driverRepository;
public DriverService(DriverRepository driverRepository) {
this.driverRepository = driverRepository;
}
@Transactional
public Driver addDriver(String name, Integer age, String phone, String email) {
final Driver driver = new Driver(name, age, phone, email);
return driverRepository.save(driver);
}
@Transactional(readOnly = true)
public Driver findDriver(Long id) {
final Optional<Driver> driver = driverRepository.findById(id);
return driver.orElseThrow(() -> new DriverNotFoundException(id));
}
@Transactional(readOnly = true)
public List<Driver> findAllDrivers() {
return driverRepository.findAll();
}
@Transactional
public Driver updateDriver(Long id, String name, Integer age, String phone, String email) {
final Driver currentDriver = findDriver(id);
if (name != null) {
currentDriver.setName(name);
}
if (age != null) {
currentDriver.setAge(age);
}
if (phone != null) {
currentDriver.setPhone(phone);
}
if (email != null) {
currentDriver.setEmail(email);
}
return driverRepository.save(currentDriver);
}
@Transactional
public Driver deleteDriver(Long id) {
final Driver currentDriver = findDriver(id);
driverRepository.delete(currentDriver);
return currentDriver;
}
@Transactional
public void deleteAllDrivers() {
driverRepository.deleteAll();
}
}

View File

@ -0,0 +1,93 @@
package com.subd.subd.Services;
import com.subd.subd.Exceptions.CarNotFoundException;
import com.subd.subd.Exceptions.ClientNotFoundException;
import com.subd.subd.Exceptions.OrderNotFoundException;
import com.subd.subd.Exceptions.PickUpPointNotFoundException;
import com.subd.subd.Models.Car;
import com.subd.subd.Models.Client;
import com.subd.subd.Models.Order;
import com.subd.subd.Models.PickUpPoint;
import com.subd.subd.Repositories.CarRepository;
import com.subd.subd.Repositories.ClientRepository;
import com.subd.subd.Repositories.OrderRepository;
import com.subd.subd.Repositories.PickUpPointRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Optional;
@Service
public class OrderService {
private final OrderRepository orderRepository;
private final ClientRepository clientRepository;
private final PickUpPointRepository pickUpPointRepository;
private final CarRepository carRepository;
public OrderService(OrderRepository orderRepository, ClientRepository clientRepository, PickUpPointRepository pickUpPointRepository, CarRepository carRepository) {
this.orderRepository = orderRepository;
this.clientRepository = clientRepository;
this.pickUpPointRepository = pickUpPointRepository;
this.carRepository = carRepository;
}
@Transactional
public Order addOrder(Double value, Long clientId, Long sourcePickUpPointId, Long destPickUpPointId, Long carId) {
final Client client = clientRepository.findById(clientId)
.orElseThrow(() -> new ClientNotFoundException(clientId));
final PickUpPoint sourcePickUpPoint = pickUpPointRepository.findById(sourcePickUpPointId)
.orElseThrow(() -> new PickUpPointNotFoundException(sourcePickUpPointId));
final PickUpPoint destPickUpPoint = pickUpPointRepository.findById(destPickUpPointId)
.orElseThrow(() -> new PickUpPointNotFoundException(destPickUpPointId));
final Car car = carRepository.findById(carId)
.orElseThrow(() -> new CarNotFoundException(carId));
final Order order = new Order(value, client, sourcePickUpPoint, destPickUpPoint, car);
return orderRepository.save(order);
}
@Transactional(readOnly = true)
public Order findOrder(Long id) {
final Optional<Order> order = orderRepository.findById(id);
return order.orElseThrow(() -> new OrderNotFoundException(id));
}
@Transactional(readOnly = true)
public List<Order> findAllOrders() {
return orderRepository.findAll();
}
@Transactional
public Order updateOrder(Long id, Double value, Long clientId, Long sourcePickUpPointId, Long destPickUpPointId, Long carId) {
final Order currentOrder = findOrder(id);
if (value != null) {
currentOrder.setValue(value);
}
if (clientId != null) {
final Client client = clientRepository.findById(clientId)
.orElseThrow(() -> new ClientNotFoundException(clientId));
currentOrder.setClient(client);
}
if (sourcePickUpPointId != null) {
final PickUpPoint sourcePickUpPoint = pickUpPointRepository.findById(sourcePickUpPointId)
.orElseThrow(() -> new PickUpPointNotFoundException(sourcePickUpPointId));
currentOrder.setSourcePickUpPoint(sourcePickUpPoint);
}
if (destPickUpPointId != null) {
final PickUpPoint destPickUpPoint = pickUpPointRepository.findById(destPickUpPointId)
.orElseThrow(() -> new PickUpPointNotFoundException(destPickUpPointId));
currentOrder.setDestPickUpPoint(destPickUpPoint);
}
if (carId != null) {
final Car car = carRepository.findById(carId)
.orElseThrow(() -> new CarNotFoundException(carId));
currentOrder.setCar(car);
}
return orderRepository.save(currentOrder);
}
@Transactional
public Order deleteOrder(Long id) {
final Order currentOrder = findOrder(id);
orderRepository.delete(currentOrder);
return currentOrder;
}
@Transactional
public void deleteAllOrder() {
orderRepository.deleteAll();
}
}

View File

@ -1,13 +1,15 @@
package com.subd.subd.Services;
import com.subd.subd.Exceptions.IdNotFoundException;
import com.subd.subd.Exceptions.PickUpPointNotFoundException;
import com.subd.subd.Models.PickUpPoint;
import com.subd.subd.Repositories.PickUpPointRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Optional;
@Service
public class PickUpPointService {
private final PickUpPointRepository pickUpPointRepository;
public PickUpPointService(PickUpPointRepository pickUpPointRepository) {
@ -21,7 +23,7 @@ public class PickUpPointService {
@Transactional(readOnly = true)
public PickUpPoint findPickUpPoint(Long id) {
final Optional<PickUpPoint> pickUpPoint = pickUpPointRepository.findById(id);
return pickUpPoint.orElseThrow(() -> new IdNotFoundException(id));
return pickUpPoint.orElseThrow(() -> new PickUpPointNotFoundException(id));
}
@Transactional(readOnly = true)
public List<PickUpPoint> findAllPickUpPoints() {
@ -30,7 +32,9 @@ public class PickUpPointService {
@Transactional
public PickUpPoint updatePickUpPoint(Long id, String address) {
final PickUpPoint currentPickUpPoint = findPickUpPoint(id);
currentPickUpPoint.setAddress(address);
if (address != null) {
currentPickUpPoint.setAddress(address);
}
return pickUpPointRepository.save(currentPickUpPoint);
}
@Transactional