add repository logic. add service logic. it works now
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
package ua.com.dxrkness.model;
|
package ua.com.dxrkness.model;
|
||||||
|
|
||||||
public record Freight() {
|
public record Freight(long id) implements Identifiable {
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package ua.com.dxrkness.model;
|
||||||
|
|
||||||
|
public interface Identifiable {
|
||||||
|
long id();
|
||||||
|
}
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
package ua.com.dxrkness.model;
|
package ua.com.dxrkness.model;
|
||||||
|
|
||||||
public record Route() {
|
import java.util.List;
|
||||||
|
|
||||||
|
public record Route(long id, Vehicle vehicle, List<Freight> freights) implements Identifiable {
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package ua.com.dxrkness.model;
|
package ua.com.dxrkness.model;
|
||||||
|
|
||||||
public record Vehicle() {
|
public record Vehicle(long id) implements Identifiable {
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,45 @@
|
|||||||
|
package ua.com.dxrkness.repository;
|
||||||
|
|
||||||
|
import ua.com.dxrkness.model.Identifiable;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
abstract class CrudRepository<T extends Identifiable> {
|
||||||
|
protected final List<T> STORAGE = new ArrayList<>();
|
||||||
|
|
||||||
|
public List<T> getAll() {
|
||||||
|
return new ArrayList<>(STORAGE); // defensive copy
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<T> getById(long id) {
|
||||||
|
return STORAGE.stream().filter(el -> el.id() == id).findFirst();
|
||||||
|
}
|
||||||
|
|
||||||
|
public T add(T toAdd) {
|
||||||
|
STORAGE.add(toAdd);
|
||||||
|
return toAdd;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T update(long id, T newT) {
|
||||||
|
for (int i = 0; i < STORAGE.size(); i++) {
|
||||||
|
if (STORAGE.get(i).id() == id) {
|
||||||
|
STORAGE.set(i, newT);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return newT;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<T> delete(long id) {
|
||||||
|
T deleted = null;
|
||||||
|
for (int i = 0; i < STORAGE.size(); i++) {
|
||||||
|
if (STORAGE.get(i).id() == id) {
|
||||||
|
deleted = STORAGE.remove(i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Optional.ofNullable(deleted);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package ua.com.dxrkness.repository;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
import ua.com.dxrkness.model.Freight;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public final class FreightRepository extends CrudRepository<Freight> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package ua.com.dxrkness.repository;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
import ua.com.dxrkness.model.Route;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public final class RouteRepository extends CrudRepository<Route> {
|
||||||
|
public Optional<Route> getByFreightId(long freightId) {
|
||||||
|
return STORAGE.stream()
|
||||||
|
.filter(route -> route.freights().stream().anyMatch(freight -> freight.id() == freightId))
|
||||||
|
.findAny();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<Route> getByVehicleId(long vehicleId) {
|
||||||
|
return STORAGE.stream()
|
||||||
|
.filter(route -> route.vehicle().id() == vehicleId)
|
||||||
|
.findAny();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package ua.com.dxrkness.repository;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
import ua.com.dxrkness.model.Vehicle;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public final class VehicleRepository extends CrudRepository<Vehicle> {
|
||||||
|
}
|
||||||
@@ -2,15 +2,36 @@ package ua.com.dxrkness.service;
|
|||||||
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import ua.com.dxrkness.model.Freight;
|
import ua.com.dxrkness.model.Freight;
|
||||||
|
import ua.com.dxrkness.repository.FreightRepository;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class FreightService {
|
public final class FreightService {
|
||||||
public List<Freight> getAll() { }
|
private final FreightRepository repo;
|
||||||
public Freight add(Freight freight) { }
|
|
||||||
public Optional<Freight> getById(long id) { }
|
public FreightService(FreightRepository repo) {
|
||||||
public Freight update(long id, Freight newFreight) { }
|
this.repo = repo;
|
||||||
public Freight delete(long id) { }
|
}
|
||||||
|
|
||||||
|
public List<Freight> getAll() {
|
||||||
|
return repo.getAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Freight add(Freight freight) {
|
||||||
|
return repo.add(freight);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<Freight> getById(long id) {
|
||||||
|
return repo.getById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Freight update(long id, Freight newFreight) {
|
||||||
|
return repo.update(id, newFreight);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<Freight> delete(long id) {
|
||||||
|
return repo.delete(id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,17 +2,44 @@ package ua.com.dxrkness.service;
|
|||||||
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import ua.com.dxrkness.model.Route;
|
import ua.com.dxrkness.model.Route;
|
||||||
|
import ua.com.dxrkness.repository.RouteRepository;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class RouteService {
|
public final class RouteService {
|
||||||
public List<Route> getAll() { }
|
private final RouteRepository repo;
|
||||||
public Optional<Route> getById(long id) { }
|
|
||||||
public Route add(Route route) { }
|
public RouteService(RouteRepository repo) {
|
||||||
public Route getByFreightId(long freightId) { }
|
this.repo = repo;
|
||||||
public Route getByVehicleId(long vehicleId) { }
|
}
|
||||||
public Route update(long id, Route newRoute) { }
|
|
||||||
public Route delete(long id) { }
|
public List<Route> getAll() {
|
||||||
|
return repo.getAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<Route> getById(long id) {
|
||||||
|
return repo.getById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Route add(Route route) {
|
||||||
|
return repo.add(route);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<Route> getByFreightId(long freightId) {
|
||||||
|
return repo.getByFreightId(freightId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<Route> getByVehicleId(long vehicleId) {
|
||||||
|
return repo.getByVehicleId(vehicleId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Route update(long id, Route newRoute) {
|
||||||
|
return repo.update(id, newRoute);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<Route> delete(long id) {
|
||||||
|
return repo.delete(id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,19 +2,36 @@ package ua.com.dxrkness.service;
|
|||||||
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import ua.com.dxrkness.model.Vehicle;
|
import ua.com.dxrkness.model.Vehicle;
|
||||||
|
import ua.com.dxrkness.repository.VehicleRepository;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class VehicleService {
|
public final class VehicleService {
|
||||||
public List<Vehicle> getAll() { }
|
private final VehicleRepository repo;
|
||||||
|
|
||||||
public Vehicle add(Vehicle veh) { }
|
public VehicleService(VehicleRepository repo) {
|
||||||
|
this.repo = repo;
|
||||||
public Optional<Vehicle> getById(long id) { }
|
}
|
||||||
|
|
||||||
public Vehicle update(long id, Vehicle newVehicle) { }
|
public List<Vehicle> getAll() {
|
||||||
|
return repo.getAll();
|
||||||
public Vehicle delete(long id) { }
|
}
|
||||||
|
|
||||||
|
public Vehicle add(Vehicle veh) {
|
||||||
|
return repo.add(veh);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<Vehicle> getById(long id) {
|
||||||
|
return repo.getById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Vehicle update(long id, Vehicle newVehicle) {
|
||||||
|
return repo.update(id, newVehicle);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<Vehicle> delete(long id) {
|
||||||
|
return repo.delete(id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user