diff --git a/src/main/java/com/pomika/carwashbackend/dto/CarWashCreationDto.java b/src/main/java/com/pomika/carwashbackend/dto/CarWashCreationDto.java index 93ea8dc..0d72fe0 100644 --- a/src/main/java/com/pomika/carwashbackend/dto/CarWashCreationDto.java +++ b/src/main/java/com/pomika/carwashbackend/dto/CarWashCreationDto.java @@ -1,6 +1,7 @@ package com.pomika.carwashbackend.dto; import com.fasterxml.jackson.annotation.JsonProperty; +import com.pomika.carwashbackend.model.MapPosition; import lombok.Getter; @Getter @@ -11,16 +12,17 @@ public class CarWashCreationDto { @JsonProperty("name") String name, @JsonProperty("picture") String picture, @JsonProperty("address") String address, - @JsonProperty("latitude") double latitude, - @JsonProperty("longitude") double longitude - ){ + @JsonProperty("map_position")MapPositionDto mapPositionDto + ){ this.phoneNumber = phoneNumber; this.password = password; this.name = name; this.picture = picture; this.address = address; - this.latitude = latitude; - this.longitude = longitude; + this.mapPosition = new MapPosition( + mapPositionDto.getLatitude(), + mapPositionDto.getLongitude() + ); } private final String phoneNumber; @@ -28,6 +30,5 @@ public class CarWashCreationDto { private final String name; private final String picture; private final String address; - private final double latitude; - private final double longitude; + private final MapPosition mapPosition; } diff --git a/src/main/java/com/pomika/carwashbackend/dto/CarWashDto.java b/src/main/java/com/pomika/carwashbackend/dto/CarWashDto.java index b696a3c..8b08d66 100644 --- a/src/main/java/com/pomika/carwashbackend/dto/CarWashDto.java +++ b/src/main/java/com/pomika/carwashbackend/dto/CarWashDto.java @@ -26,9 +26,7 @@ public class CarWashDto { @JsonProperty("address") private final String address; - @JsonProperty("latitude") - private final double latitude; + @JsonProperty("map_position") + private final MapPositionDto mapPositionDto; - @JsonProperty("longitude") - private final double longitude; } diff --git a/src/main/java/com/pomika/carwashbackend/dto/CarWashUpdateDto.java b/src/main/java/com/pomika/carwashbackend/dto/CarWashUpdateDto.java index 229b0f4..33134d9 100644 --- a/src/main/java/com/pomika/carwashbackend/dto/CarWashUpdateDto.java +++ b/src/main/java/com/pomika/carwashbackend/dto/CarWashUpdateDto.java @@ -1,6 +1,7 @@ package com.pomika.carwashbackend.dto; import com.fasterxml.jackson.annotation.JsonProperty; +import com.pomika.carwashbackend.model.MapPosition; import lombok.Getter; @Getter @@ -9,19 +10,19 @@ public CarWashUpdateDto( @JsonProperty("name") String name, @JsonProperty("picture") String picture, @JsonProperty("address") String address, - @JsonProperty("latitude") double latitude, - @JsonProperty("longitude") double longitude + @JsonProperty("map_position")MapPositionDto mapPositionDto ){ this.name= name; this.picture = picture; this.address = address; - this.latitude = latitude; - this.longitude = longitude; + this.mapPosition = new MapPosition( + mapPositionDto.getLatitude(), + mapPositionDto.getLongitude() + ); } private final String name; private final String picture; private final String address; - private final double latitude; - private final double longitude; + private final MapPosition mapPosition; } diff --git a/src/main/java/com/pomika/carwashbackend/dto/MapPositionDto.java b/src/main/java/com/pomika/carwashbackend/dto/MapPositionDto.java new file mode 100644 index 0000000..fc9be8e --- /dev/null +++ b/src/main/java/com/pomika/carwashbackend/dto/MapPositionDto.java @@ -0,0 +1,16 @@ +package com.pomika.carwashbackend.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class MapPositionDto { + + @JsonProperty("latitude") + private double latitude; + + @JsonProperty("longitude") + private double longitude; +} diff --git a/src/main/java/com/pomika/carwashbackend/dto/OfferDto.java b/src/main/java/com/pomika/carwashbackend/dto/OfferDto.java index 39f55d2..6e373e5 100644 --- a/src/main/java/com/pomika/carwashbackend/dto/OfferDto.java +++ b/src/main/java/com/pomika/carwashbackend/dto/OfferDto.java @@ -27,11 +27,8 @@ public class OfferDto { @JsonProperty("car_wash_address") private final String carWashAddress; - @JsonProperty("latitude") - private final double latitude; - - @JsonProperty("longitude") - private final double longitude; + @JsonProperty("map_position") + private final MapPositionDto mapPositionDto; @JsonProperty("car_wash_name") private final String carWashName; diff --git a/src/main/java/com/pomika/carwashbackend/dto/OrderInProcessForCarWashDto.java b/src/main/java/com/pomika/carwashbackend/dto/OrderInProcessForCarWashDto.java new file mode 100644 index 0000000..522cead --- /dev/null +++ b/src/main/java/com/pomika/carwashbackend/dto/OrderInProcessForCarWashDto.java @@ -0,0 +1,34 @@ +package com.pomika.carwashbackend.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.pomika.carwashbackend.model.WashServiceType; +import lombok.AllArgsConstructor; +import lombok.Getter; + +import java.util.Date; +import java.util.List; + +@Getter +@AllArgsConstructor +public class OrderInProcessForCarWashDto { + @JsonProperty("id") + private int id; + + @JsonProperty("start_time") + private Date startTime; + + @JsonProperty("wash_time") + private int washTime; + + @JsonProperty("price") + private int price; + + @JsonProperty("car") + private CarDto car; + + @JsonProperty("wash_service_types") + private List washServiceTypes; + + @JsonProperty("user_phone_number") + private String userPhoneNumber; +} diff --git a/src/main/java/com/pomika/carwashbackend/dto/OrderInProcessForUserDto.java b/src/main/java/com/pomika/carwashbackend/dto/OrderInProcessForUserDto.java new file mode 100644 index 0000000..2e0d79d --- /dev/null +++ b/src/main/java/com/pomika/carwashbackend/dto/OrderInProcessForUserDto.java @@ -0,0 +1,46 @@ +package com.pomika.carwashbackend.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.pomika.carwashbackend.model.WashServiceType; +import lombok.AllArgsConstructor; +import lombok.Getter; + +import java.util.Date; +import java.util.List; + +@Getter +@AllArgsConstructor +public class OrderInProcessForUserDto { + @JsonProperty("id") + private int id; + + @JsonProperty("start_time") + private Date startTime; + + @JsonProperty("wash_time") + private int washTime; + + @JsonProperty("price") + private int price; + + @JsonProperty("car") + private CarDto car; + + @JsonProperty("wash_service_types") + private List washServiceTypes; + + @JsonProperty("car_wash_name") + private String carWashName; + + @JsonProperty("car_wash_address") + private String carWashAddress; + + @JsonProperty("map_position") + private MapPositionDto mapPositionDto; + + @JsonProperty("car_wash_picture") + private String carWashPicture; + + @JsonProperty("car_wash_phone_number") + private String carWashPhoneNumber; +} diff --git a/src/main/java/com/pomika/carwashbackend/dto/OrderSessionCreationDto.java b/src/main/java/com/pomika/carwashbackend/dto/OrderSessionCreationDto.java index 5fe499a..959b4ec 100644 --- a/src/main/java/com/pomika/carwashbackend/dto/OrderSessionCreationDto.java +++ b/src/main/java/com/pomika/carwashbackend/dto/OrderSessionCreationDto.java @@ -15,15 +15,16 @@ public OrderSessionCreationDto( @JsonProperty("endTime") Date endTime, @JsonProperty("car") CarDto car, @JsonProperty("wash_services") List washServiceTypes, - @JsonProperty("latitude") double latitude, - @JsonProperty("longitude") double longitude, - @JsonProperty("radius") double radius + @JsonProperty("search_area") SearchAreaDto searchAreaDto ){ this.startTime = startTime; this.endTime = endTime; this.car = car; this.washServiceTypes = washServiceTypes; - this.searchArea = new SearchArea(latitude,longitude,radius); + this.searchArea = new SearchArea( + searchAreaDto.getMapPositionDto().getLatitude(), + searchAreaDto.getMapPositionDto().getLongitude(), + searchAreaDto.getRadius()); } private final Date startTime; diff --git a/src/main/java/com/pomika/carwashbackend/dto/SearchAreaDto.java b/src/main/java/com/pomika/carwashbackend/dto/SearchAreaDto.java new file mode 100644 index 0000000..f892a91 --- /dev/null +++ b/src/main/java/com/pomika/carwashbackend/dto/SearchAreaDto.java @@ -0,0 +1,15 @@ +package com.pomika.carwashbackend.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class SearchAreaDto { + @JsonProperty("map_position") + private MapPositionDto mapPositionDto; + + @JsonProperty("radius") + private double radius; +} diff --git a/src/main/java/com/pomika/carwashbackend/exception/OrderInProcessServiceException.java b/src/main/java/com/pomika/carwashbackend/exception/OrderInProcessServiceException.java new file mode 100644 index 0000000..6070620 --- /dev/null +++ b/src/main/java/com/pomika/carwashbackend/exception/OrderInProcessServiceException.java @@ -0,0 +1,7 @@ +package com.pomika.carwashbackend.exception; + +public class OrderInProcessServiceException extends RuntimeException{ + public OrderInProcessServiceException(String message){ + super(message); + } +} diff --git a/src/main/java/com/pomika/carwashbackend/model/Car.java b/src/main/java/com/pomika/carwashbackend/model/Car.java index 5290773..a2ce83f 100644 --- a/src/main/java/com/pomika/carwashbackend/model/Car.java +++ b/src/main/java/com/pomika/carwashbackend/model/Car.java @@ -29,7 +29,7 @@ public Car(int id, @Column(name = "id") private int id; - @ManyToOne + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "owner_id") private UserAccount userAccount; diff --git a/src/main/java/com/pomika/carwashbackend/model/CarWash.java b/src/main/java/com/pomika/carwashbackend/model/CarWash.java index 5fa577d..f50f508 100644 --- a/src/main/java/com/pomika/carwashbackend/model/CarWash.java +++ b/src/main/java/com/pomika/carwashbackend/model/CarWash.java @@ -17,16 +17,14 @@ public CarWash(Account account, double rating, String picture, String address, - double latitude, - double longitude + MapPosition mapPosition ){ this.account = account; this.name = name; this.rating = rating; this.picture = picture; this.address = address; - this.latitude = latitude; - this.longitude = longitude; + this.mapPosition = mapPosition; } @Id @@ -50,11 +48,10 @@ public CarWash(Account account, @Column(name = "address") private String address; - @Column(name = "latitude") - private double latitude; - - @Column(name = "longitude") - private double longitude; - - + @Embedded + @AttributeOverrides({ + @AttributeOverride( name = "latitude", column = @Column(name = "latitude")), + @AttributeOverride( name = "longitude", column = @Column(name = "longitude")) + }) + private MapPosition mapPosition; } diff --git a/src/main/java/com/pomika/carwashbackend/model/MapPosition.java b/src/main/java/com/pomika/carwashbackend/model/MapPosition.java index 6bbb57e..bff00a1 100644 --- a/src/main/java/com/pomika/carwashbackend/model/MapPosition.java +++ b/src/main/java/com/pomika/carwashbackend/model/MapPosition.java @@ -1,5 +1,6 @@ package com.pomika.carwashbackend.model; +import jakarta.persistence.Embeddable; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.Setter; @@ -7,7 +8,10 @@ @Getter @Setter @AllArgsConstructor +@Embeddable public class MapPosition { + public MapPosition(){} + private double latitude; private double longitude; } diff --git a/src/main/java/com/pomika/carwashbackend/model/Order.java b/src/main/java/com/pomika/carwashbackend/model/Order.java index a7f6a30..cdce788 100644 --- a/src/main/java/com/pomika/carwashbackend/model/Order.java +++ b/src/main/java/com/pomika/carwashbackend/model/Order.java @@ -4,6 +4,7 @@ import lombok.Setter; import java.util.Date; +import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -19,13 +20,15 @@ public class Order { private Map carWashAndServices; private double bestRating; private int bestPrice; + private List washServiceTypes; public Order( int userId, Date startTime, Date endTime, Car car, - Map carWashAndServices + Map carWashAndServices, + List washServiceTypes ){ this.userId = userId; this.startTime = startTime; @@ -35,6 +38,7 @@ public Order( this.bestPrice = 0; this.bestRating = 0; this.carWashAndServices = carWashAndServices; + this.washServiceTypes = washServiceTypes; } @Override diff --git a/src/main/java/com/pomika/carwashbackend/model/OrderInProcess.java b/src/main/java/com/pomika/carwashbackend/model/OrderInProcess.java index 62e7b4d..8309bf5 100644 --- a/src/main/java/com/pomika/carwashbackend/model/OrderInProcess.java +++ b/src/main/java/com/pomika/carwashbackend/model/OrderInProcess.java @@ -17,11 +17,16 @@ public OrderInProcess(){} public OrderInProcess(Car car, Date startTime, CarWash carWash, - List washServices){ + List washServiceTypes, + int price, + int washTime){ this.car = car; this.startTime = startTime; - this.washServices = washServices; + this.washServiceTypes = washServiceTypes; this.carWash = carWash; + this.price = price; + this.washTime = washTime; + this.isFinished = false; } @Id @@ -39,7 +44,15 @@ public OrderInProcess(Car car, @JoinColumn(name = "car_wash_id") private CarWash carWash; - @ManyToMany - private List washServices; + @Column(name = "wash_service_types") + private List washServiceTypes; + @Column(name = "price") + private int price; + + @Column(name = "wash_time") + private int washTime; + + @Column(name = "is_finished") + private boolean isFinished; } diff --git a/src/main/java/com/pomika/carwashbackend/repository/CarWashRepository.java b/src/main/java/com/pomika/carwashbackend/repository/CarWashRepository.java index 32d5517..5da30a7 100644 --- a/src/main/java/com/pomika/carwashbackend/repository/CarWashRepository.java +++ b/src/main/java/com/pomika/carwashbackend/repository/CarWashRepository.java @@ -11,7 +11,7 @@ public interface CarWashRepository extends JpaRepository { CarWash findByAccount(Account account); - List findByLatitudeIsBetweenAndLongitudeIsBetween(double latitudeBottom, + List findByMapPosition_LatitudeIsBetweenAndMapPosition_LongitudeIsBetween(double latitudeBottom, double latitudeTop, double longitudeBottom, double longitudeTop); @@ -19,7 +19,7 @@ List findByLatitudeIsBetweenAndLongitudeIsBetween(double latitudeBottom default List findCarWashesInSquare(double centerLatitude, double centerLongitude, double radius){ double latitudeShift = (180 * radius)/(Math.PI * 6378100); double longitudeShift = (180 * radius)/(Math.PI * 6356800); - return findByLatitudeIsBetweenAndLongitudeIsBetween( + return findByMapPosition_LatitudeIsBetweenAndMapPosition_LongitudeIsBetween( centerLatitude - latitudeShift, centerLatitude + latitudeShift, centerLongitude - longitudeShift, diff --git a/src/main/java/com/pomika/carwashbackend/repository/OrderInProcessRepository.java b/src/main/java/com/pomika/carwashbackend/repository/OrderInProcessRepository.java index 8e6392c..f0b05f1 100644 --- a/src/main/java/com/pomika/carwashbackend/repository/OrderInProcessRepository.java +++ b/src/main/java/com/pomika/carwashbackend/repository/OrderInProcessRepository.java @@ -1,10 +1,18 @@ package com.pomika.carwashbackend.repository; +import com.pomika.carwashbackend.model.CarWash; import com.pomika.carwashbackend.model.OrderInProcess; +import com.pomika.carwashbackend.model.UserAccount; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import java.util.List; + @Repository public interface OrderInProcessRepository extends JpaRepository { + OrderInProcess findByCar_UserAccount(UserAccount userAccount); + + List findByCarWash(CarWash carWash); + OrderInProcess findById(int orderId); } diff --git a/src/main/java/com/pomika/carwashbackend/service/OrderInProcessService.java b/src/main/java/com/pomika/carwashbackend/service/OrderInProcessService.java new file mode 100644 index 0000000..7477cfc --- /dev/null +++ b/src/main/java/com/pomika/carwashbackend/service/OrderInProcessService.java @@ -0,0 +1,15 @@ +package com.pomika.carwashbackend.service; + +import com.pomika.carwashbackend.dto.OrderInProcessForCarWashDto; +import com.pomika.carwashbackend.dto.OrderInProcessForUserDto; + +import java.util.List; + +public interface OrderInProcessService { + OrderInProcessForUserDto getOrder(String userPhoneNumber); + List getOrders(String carWashPhoneNumber); + void cancelOrderByUser(String userPhoneNumber); + void cancelOrderByCarWash(String carWashPhoneNumber, int orderInProcessId); + void finishOrder(String carWashPhoneNumber, int orderInProcessId, double orderRating); + void rateFinishedOrder(String userPhoneNumber, double orderRating); +} diff --git a/src/main/java/com/pomika/carwashbackend/service/impl/CarWashServiceImpl.java b/src/main/java/com/pomika/carwashbackend/service/impl/CarWashServiceImpl.java index d8434b3..7825e2e 100644 --- a/src/main/java/com/pomika/carwashbackend/service/impl/CarWashServiceImpl.java +++ b/src/main/java/com/pomika/carwashbackend/service/impl/CarWashServiceImpl.java @@ -53,8 +53,7 @@ public void createCarWash(CarWashCreationDto carWashCreationDto) 5.0, //TODO rating carWashCreationDto.getPicture(), carWashCreationDto.getAddress(), - carWashCreationDto.getLatitude(), - carWashCreationDto.getLongitude())); + carWashCreationDto.getMapPosition())); } @Override @@ -64,8 +63,7 @@ public void updateProfile(String ownerPhoneNumber, CarWashUpdateDto carWashUpdat carWash.setName(carWashUpdateDto.getName()); carWash.setAddress(carWashUpdateDto.getAddress()); carWash.setPicture(carWashUpdateDto.getPicture()); - carWash.setLatitude(carWashUpdateDto.getLatitude()); - carWash.setLongitude(carWashUpdateDto.getLongitude()); + carWash.setMapPosition(carWashUpdateDto.getMapPosition()); carWashRepository.save(carWash); } @@ -164,8 +162,10 @@ private CarWashDto carWashEntityToDto(CarWash carWash){ carWash.getPicture(), carWash.getRating(), carWash.getAddress(), - carWash.getLatitude(), - carWash.getLongitude()); + new MapPositionDto( + carWash.getMapPosition().getLatitude(), + carWash.getMapPosition().getLongitude() + )); } private WashServiceDto serviceEntityToDto(WashService washService){ diff --git a/src/main/java/com/pomika/carwashbackend/service/impl/OrderInProcessServiceImpl.java b/src/main/java/com/pomika/carwashbackend/service/impl/OrderInProcessServiceImpl.java new file mode 100644 index 0000000..3258c0a --- /dev/null +++ b/src/main/java/com/pomika/carwashbackend/service/impl/OrderInProcessServiceImpl.java @@ -0,0 +1,223 @@ +package com.pomika.carwashbackend.service.impl; + +import com.pomika.carwashbackend.dto.CarDto; +import com.pomika.carwashbackend.dto.MapPositionDto; +import com.pomika.carwashbackend.dto.OrderInProcessForCarWashDto; +import com.pomika.carwashbackend.dto.OrderInProcessForUserDto; +import com.pomika.carwashbackend.exception.CarWashServiceException; +import com.pomika.carwashbackend.exception.OrderInProcessServiceException; +import com.pomika.carwashbackend.model.Account; +import com.pomika.carwashbackend.model.CarWash; +import com.pomika.carwashbackend.model.OrderInProcess; +import com.pomika.carwashbackend.model.UserAccount; +import com.pomika.carwashbackend.repository.AccountRepository; +import com.pomika.carwashbackend.repository.CarWashRepository; +import com.pomika.carwashbackend.repository.OrderInProcessRepository; +import com.pomika.carwashbackend.repository.UserAccountRepository; +import com.pomika.carwashbackend.service.OrderInProcessService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.stream.Collectors; + +@Service +public class OrderInProcessServiceImpl implements OrderInProcessService { + + private final AccountRepository accountRepository; + private final UserAccountRepository userAccountRepository; + private final OrderInProcessRepository orderInProcessRepository; + private final CarWashRepository carWashRepository; + + + @Autowired + public OrderInProcessServiceImpl ( + AccountRepository accountRepository, + UserAccountRepository userAccountRepository, + OrderInProcessRepository orderInProcessRepository, + CarWashRepository carWashRepository + ){ + this.accountRepository = accountRepository; + this.userAccountRepository = userAccountRepository; + this.orderInProcessRepository = orderInProcessRepository; + this.carWashRepository = carWashRepository; + } + + + @Override + public OrderInProcessForUserDto getOrder(String userPhoneNumber) + throws OrderInProcessServiceException{ + OrderInProcess orderInProcess = findOrderInProcessByUserPhoneNumber(userPhoneNumber); + return orderInProcessEntityToUserDto(orderInProcess); + } + + @Override + public List getOrders(String carWashPhoneNumber) + throws OrderInProcessServiceException{ + List ordersInProcess = findOrdersInProcessByCarWashPhoneNumber(carWashPhoneNumber); + return ordersInProcess.stream() + .map(this::orderInProcessEntityToCarWashDto) + .collect(Collectors.toList()); + } + + @Override + public void cancelOrderByUser(String userPhoneNumber) + throws OrderInProcessServiceException{ + OrderInProcess orderInProcess = findOrderInProcessByUserPhoneNumber(userPhoneNumber); + if (orderInProcess.isFinished()){ + throw new OrderInProcessServiceException("Can not cancel order that already finished and not rated"); + } + orderInProcessRepository.delete(orderInProcess); + //TODO notify car wash about canceled order + } + + @Override + public void cancelOrderByCarWash(String carWashPhoneNumber, int orderInProcessId) + throws OrderInProcessServiceException{ + CarWash carWash = findCarWashByPhoneNumber(carWashPhoneNumber); + OrderInProcess orderInProcess = findOrderInProcessByOrderId(orderInProcessId); + if (!orderInProcess.getCarWash().equals(carWash)){ + throw new OrderInProcessServiceException("Car wash with phone number " + + carWashPhoneNumber + " does not participate in order with id " + + orderInProcessId); + } + + if (orderInProcess.isFinished()){ + throw new OrderInProcessServiceException("Can not cancel order that already finished until user rate"); + } + + orderInProcessRepository.delete(orderInProcess); + //TODO notify user about canceled order + } + + @Override + public void finishOrder(String carWashPhoneNumber, int orderInProcessId, double orderRating) + throws OrderInProcessServiceException{ + CarWash carWash = findCarWashByPhoneNumber(carWashPhoneNumber); + OrderInProcess orderInProcess = findOrderInProcessByOrderId(orderInProcessId); + if (!orderInProcess.getCarWash().equals(carWash)){ + throw new OrderInProcessServiceException("Car wash with phone number " + + carWashPhoneNumber + " does not participate in order with id " + + orderInProcessId); + } + + if (orderInProcess.isFinished()){ + throw new OrderInProcessServiceException("Can not finish that already finished"); + } + + orderInProcess.setFinished(true); + orderInProcessRepository.save(orderInProcess); + //TODO somehow rate user + } + + @Override + public void rateFinishedOrder(String userPhoneNumber, double orderRating) + throws OrderInProcessServiceException{ + OrderInProcess orderInProcess = findOrderInProcessByUserPhoneNumber(userPhoneNumber); + if (!orderInProcess.isFinished()){ + throw new OrderInProcessServiceException("Can not rate car wash because order not yet finished"); + } + + //TODO somehow rate car wash + orderInProcessRepository.delete(orderInProcess); + } + + private Account findAccountByPhoneNumber(String phoneNumber){ + Account account = accountRepository.findByPhoneNumber(phoneNumber); + if (account == null){ + throw new OrderInProcessServiceException("Account with phone number " + + phoneNumber + "does not exists"); + } + return account; + } + + private UserAccount findUserByPhoneNumber(String phoneNumber){ + Account account = findAccountByPhoneNumber(phoneNumber); + UserAccount userAccount = userAccountRepository.findByAccount(account); + if (userAccount == null){ + throw new OrderInProcessServiceException("Account with phone number " + + phoneNumber + "does not exists"); + } + return userAccount; + } + + private CarWash findCarWashByPhoneNumber(String phoneNumber){ + Account account = findAccountByPhoneNumber(phoneNumber); + CarWash carWash = carWashRepository.findByAccount(account); + if (carWash == null){ + throw new CarWashServiceException("Car wash with phone number " + + phoneNumber + "does not exists"); + } + return carWash; + } + + private OrderInProcess findOrderInProcessByUserPhoneNumber(String phoneNumber){ + UserAccount userAccount = findUserByPhoneNumber(phoneNumber); + OrderInProcess orderInProcess = orderInProcessRepository.findByCar_UserAccount(userAccount); + if (orderInProcess == null){ + throw new OrderInProcessServiceException("Order in process for user with phone number " + + phoneNumber + " does not exists"); + } + return orderInProcess; + } + + private List findOrdersInProcessByCarWashPhoneNumber(String phoneNumber){ + CarWash carWash = findCarWashByPhoneNumber(phoneNumber); + List ordersInProcess = orderInProcessRepository.findByCarWash(carWash); + if (ordersInProcess.isEmpty()){ + throw new OrderInProcessServiceException("Orders in process for car wash wth number " + + phoneNumber + " does not exist"); + } + return ordersInProcess; + } + + private OrderInProcess findOrderInProcessByOrderId(int orderInProcessId){ + OrderInProcess orderInProcess = orderInProcessRepository.findById(orderInProcessId); + if (orderInProcess == null){ + throw new OrderInProcessServiceException("Order in process with id " + + orderInProcessId + " does not exists"); + } + return orderInProcess; + } + + private OrderInProcessForUserDto orderInProcessEntityToUserDto(OrderInProcess orderInProcess){ + return new OrderInProcessForUserDto( + orderInProcess.getId(), + orderInProcess.getStartTime(), + orderInProcess.getWashTime(), + orderInProcess.getPrice(), + new CarDto( + orderInProcess.getCar().getId(), + orderInProcess.getCar().getNumber(), + orderInProcess.getCar().getName(), + orderInProcess.getCar().getCarType() + ), + orderInProcess.getWashServiceTypes(), + orderInProcess.getCarWash().getName(), + orderInProcess.getCarWash().getAddress(), + new MapPositionDto( + orderInProcess.getCarWash().getMapPosition().getLatitude(), + orderInProcess.getCarWash().getMapPosition().getLongitude() + ), + orderInProcess.getCarWash().getPicture(), + orderInProcess.getCarWash().getAccount().getPhoneNumber() + ); + } + + private OrderInProcessForCarWashDto orderInProcessEntityToCarWashDto(OrderInProcess orderInProcess){ + return new OrderInProcessForCarWashDto( + orderInProcess.getId(), + orderInProcess.getStartTime(), + orderInProcess.getWashTime(), + orderInProcess.getPrice(), + new CarDto( + orderInProcess.getCar().getId(), + orderInProcess.getCar().getNumber(), + orderInProcess.getCar().getName(), + orderInProcess.getCar().getCarType() + ), + orderInProcess.getWashServiceTypes(), + orderInProcess.getCar().getUserAccount().getAccount().getPhoneNumber() + ); + } +} diff --git a/src/main/java/com/pomika/carwashbackend/service/impl/OrderSessionServiceImpl.java b/src/main/java/com/pomika/carwashbackend/service/impl/OrderSessionServiceImpl.java index 3668da9..6d418f3 100644 --- a/src/main/java/com/pomika/carwashbackend/service/impl/OrderSessionServiceImpl.java +++ b/src/main/java/com/pomika/carwashbackend/service/impl/OrderSessionServiceImpl.java @@ -45,6 +45,12 @@ public void openSession(String userPhoneNumber, OrderSessionCreationDto orderSes throws OrderSessionServiceException{ UserAccount userAccount = findUserByPhoneNumber(userPhoneNumber); + OrderInProcess orderInProcess = orderInProcessRepository.findByCar_UserAccount(userAccount); + if (orderInProcess != null){ + throw new OrderSessionServiceException("User with phone number " + + userPhoneNumber + " have not finished previous order yet"); + } + if (sessions.get(userAccount.getId()) != null){ throw new OrderSessionServiceException("Session already opened by user with phone number " + userPhoneNumber); @@ -64,7 +70,8 @@ public void openSession(String userPhoneNumber, OrderSessionCreationDto orderSes orderSessionCreationDto.getCar().getNumber(), orderSessionCreationDto.getCar().getName(), orderSessionCreationDto.getCar().getType()), - services + services, + orderSessionCreationDto.getWashServiceTypes() ); sessions.put(userAccount.getId(), order); @@ -93,7 +100,9 @@ public void acceptOffer(String userPhoneNumber, String carWashPhoneNumber) order.getCar(), acceptedOffer.getStartTime(), carWashServicesInOrder.getCarWash(), - carWashServicesInOrder.getServices() + order.getWashServiceTypes(), + acceptedOffer.getPrice(), + acceptedOffer.getWashTime() ); orderInProcessRepository.save(orderInProcess); @@ -261,12 +270,13 @@ private boolean checkCarWashCoordinates( double centerLongitude, double radius ){ - double theta = centerLongitude - carWash.getLongitude(); + double theta = centerLongitude - carWash.getMapPosition().getLongitude(); double distance = 60 * 1.1515 * (180/Math.PI) * Math.acos( - Math.sin(centerLatitude * (Math.PI/180)) * Math.sin(carWash.getLatitude() * (Math.PI/180)) + - Math.cos(centerLatitude * (Math.PI/180)) * - Math.cos(carWash.getLatitude() * (Math.PI/180)) * - Math.cos(theta * (Math.PI/180)) + Math.sin(centerLatitude * (Math.PI/180)) * + Math.sin(carWash.getMapPosition().getLatitude() * (Math.PI/180)) + + Math.cos(centerLatitude * (Math.PI/180)) * + Math.cos(carWash.getMapPosition().getLatitude() * (Math.PI/180)) * + Math.cos(theta * (Math.PI/180)) ); return radius > distance; } @@ -320,8 +330,10 @@ private OfferDto offerAndCarWashEntityToDto(Offer offer, CarWash carWash){ offer.getWashTime(), carWash.getRating(), carWash.getAddress(), - carWash.getLatitude(), - carWash.getLongitude(), + new MapPositionDto( + carWash.getMapPosition().getLatitude(), + carWash.getMapPosition().getLongitude() + ), carWash.getName() ); }