use of com.ncedu.fooddelivery.api.v1.entities.User in project 2021-msk-food-delivery by netcracker-edu.
the class OrderServiceImpl1 method changeDeliveryRating.
@Override
public void changeDeliveryRating(Order order, ChangeRatingDTO dto, User user) {
if (order.getCourier() == null)
throw new CourierNotSetException();
if (!user.getId().equals(order.getClient().getId()))
throw new CustomAccessDeniedException();
order.setDeliveryRating(dto.getRating());
orderRepo.save(order);
}
use of com.ncedu.fooddelivery.api.v1.entities.User in project 2021-msk-food-delivery by netcracker-edu.
the class OrderServiceImpl1 method buildOrder.
private Order buildOrder(User user, Geometry coords, CreateOrderDTO dto, List<ProductPosition> productPositions) {
Order order = new Order();
final int weightLimit = 15000;
int currentWeight = 0;
Map<Long, Integer> currOrderPositions = new HashMap<>();
// adding records in DB table 'orders_product_positions'
for (int j = 0; j < productPositions.size() && weightLimit != currentWeight; j++) {
ProductPosition currPos = productPositions.get(j);
int currPosWeight = currPos.getProduct().getWeight();
if ((weightLimit - currentWeight) >= currPosWeight) {
currentWeight += currPosWeight;
currOrderPositions.merge(currPos.getId(), 1, Integer::sum);
productPositions.remove(j);
j--;
}
}
Double orderCost = 0d;
Double orderDiscount = 0d;
for (Long psId : currOrderPositions.keySet()) {
int amount = currOrderPositions.get(psId);
ProductPosition ps = productPositionRepo.findById(psId).get();
Double price = (ps.getProduct().getPrice() - ps.getProduct().getDiscount()) * amount;
Double discount = amount * ps.getProduct().getDiscount();
orderCost += price;
orderDiscount += discount;
}
order.setDiscount(orderDiscount);
order.setHighDemandCoeff(dto.getHighDemandCoeff());
order.setOverallCost(orderCost);
order.setClient(user.getClient());
order.setWarehouse(warehouseService.findById(dto.getWarehouseId()));
order.setAddress(dto.getAddress());
order.setCoordinates(coords);
order.setStatus(OrderStatus.CREATED);
order.setDateStart(LocalDateTime.now());
order = orderRepo.save(order);
for (Long psId : currOrderPositions.keySet()) {
int amount = currOrderPositions.get(psId);
ProductPosition ps = productPositionRepo.findById(psId).get();
orderProductPositionRepo.save(new OrderProductPosition(order, ps, amount, (ps.getProduct().getPrice() - ps.getProduct().getDiscount()) * amount));
}
return order;
}
use of com.ncedu.fooddelivery.api.v1.entities.User in project 2021-msk-food-delivery by netcracker-edu.
the class OrderServiceImpl1 method findFiltered.
@Override
public List<OrderInfoDTO> findFiltered(User user, OrderFilterDTO dto, Pageable pageable) {
Specification<Order> spec;
if (user.getRole() == Role.MODERATOR) {
Long moderatorWarehouseId = user.getModerator().getWarehouseId();
if (dto.getWarehouseId() != null) {
if (!dto.getWarehouseId().equals(moderatorWarehouseId))
throw new CustomAccessDeniedException();
}
}
spec = OrderSpecifications.getFilterSpecification(dto);
return orderRepo.findAll(spec, pageable).stream().map(order -> convertToOrderInfoDTO(order)).collect(Collectors.toList());
}
use of com.ncedu.fooddelivery.api.v1.entities.User in project 2021-msk-food-delivery by netcracker-edu.
the class OrderServiceImpl1 method changeClientRating.
@Override
public void changeClientRating(Order order, ChangeRatingDTO dto, User user) {
if (order.getCourier() == null || !user.getId().equals(order.getCourier().getId()))
throw new CustomAccessDeniedException();
order.setClientRating(dto.getRating());
orderRepo.save(order);
}
use of com.ncedu.fooddelivery.api.v1.entities.User in project 2021-msk-food-delivery by netcracker-edu.
the class UserDetailsServiceImpl method loadUserByUsername.
// TODO: add courier repo for finding user by phoneNumber
@Override
public UserDetails loadUserByUsername(String login) throws UsernameNotFoundException {
User user = userRepo.findByEmail(login);
// if user not found by Email try find client/courier by phoneNumber
if (user == null) {
Client client = clientRepo.findByPhoneNumber(login);
if (client == null) {
throw new NotFoundEx(login);
}
user = client.getUser();
}
return user;
}
Aggregations