Search in sources :

Example 76 with User

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);
}
Also used : CustomAccessDeniedException(com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException)

Example 77 with User

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;
}
Also used : Order(com.ncedu.fooddelivery.api.v1.entities.order.Order) OrderProductPosition(com.ncedu.fooddelivery.api.v1.entities.orderProductPosition.OrderProductPosition) OrderProductPosition(com.ncedu.fooddelivery.api.v1.entities.orderProductPosition.OrderProductPosition) ProductPosition(com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPosition)

Example 78 with User

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());
}
Also used : Order(com.ncedu.fooddelivery.api.v1.entities.order.Order) OrderProductPositionRepo(com.ncedu.fooddelivery.api.v1.repos.orderProductPosition.OrderProductPositionRepo) java.util(java.util) CoordsDTO(com.ncedu.fooddelivery.api.v1.dto.CoordsDTO) com.ncedu.fooddelivery.api.v1.entities(com.ncedu.fooddelivery.api.v1.entities) LocalDateTime(java.time.LocalDateTime) Autowired(org.springframework.beans.factory.annotation.Autowired) PrecisionModel(com.vividsolutions.jts.geom.PrecisionModel) com.ncedu.fooddelivery.api.v1.dto.order(com.ncedu.fooddelivery.api.v1.dto.order) OrderCostChangedEx(com.ncedu.fooddelivery.api.v1.errors.orderRegistration.OrderCostChangedEx) OrderSpecifications(com.ncedu.fooddelivery.api.v1.specifications.OrderSpecifications) NotFoundEx(com.ncedu.fooddelivery.api.v1.errors.notfound.NotFoundEx) Service(org.springframework.stereotype.Service) com.ncedu.fooddelivery.api.v1.dto.areCreatedDTO(com.ncedu.fooddelivery.api.v1.dto.areCreatedDTO) Geometry(com.vividsolutions.jts.geom.Geometry) Pageable(org.springframework.data.domain.Pageable) OrderProductPosition(com.ncedu.fooddelivery.api.v1.entities.orderProductPosition.OrderProductPosition) ProductRepo(com.ncedu.fooddelivery.api.v1.repos.ProductRepo) Coordinate(com.vividsolutions.jts.geom.Coordinate) ProductMapper(com.ncedu.fooddelivery.api.v1.mappers.ProductMapper) Predicate(java.util.function.Predicate) OrderRepo(com.ncedu.fooddelivery.api.v1.repos.order.OrderRepo) WarehouseService(com.ncedu.fooddelivery.api.v1.services.WarehouseService) WarehouseInfoDTO(com.ncedu.fooddelivery.api.v1.dto.warehouseDTOs.WarehouseInfoDTO) CourierRepo(com.ncedu.fooddelivery.api.v1.repos.CourierRepo) ProductPositionRepo(com.ncedu.fooddelivery.api.v1.repos.productPosition.ProductPositionRepo) CourierAvailabilityEx(com.ncedu.fooddelivery.api.v1.errors.orderRegistration.CourierAvailabilityEx) OrderService(com.ncedu.fooddelivery.api.v1.services.OrderService) Collectors(java.util.stream.Collectors) Order(com.ncedu.fooddelivery.api.v1.entities.order.Order) ProductAvailabilityEx(com.ncedu.fooddelivery.api.v1.errors.orderRegistration.ProductAvailabilityEx) ProductDTO(com.ncedu.fooddelivery.api.v1.dto.product.ProductDTO) Specification(org.springframework.data.jpa.domain.Specification) WarehouseCoordsBindingEx(com.ncedu.fooddelivery.api.v1.errors.orderRegistration.WarehouseCoordsBindingEx) ProductPosition(com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPosition) CustomAccessDeniedException(com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) com.ncedu.fooddelivery.api.v1.errors.badrequest(com.ncedu.fooddelivery.api.v1.errors.badrequest) CourierService(com.ncedu.fooddelivery.api.v1.services.CourierService) Transactional(org.springframework.transaction.annotation.Transactional) CustomAccessDeniedException(com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException)

Example 79 with User

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);
}
Also used : CustomAccessDeniedException(com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException)

Example 80 with User

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;
}
Also used : User(com.ncedu.fooddelivery.api.v1.entities.User) NotFoundEx(com.ncedu.fooddelivery.api.v1.errors.notfound.NotFoundEx) Client(com.ncedu.fooddelivery.api.v1.entities.Client)

Aggregations

User (com.ncedu.fooddelivery.api.v1.entities.User)58 Test (org.junit.jupiter.api.Test)55 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)55 File (com.ncedu.fooddelivery.api.v1.entities.File)21 Path (java.nio.file.Path)19 MultipartFile (org.springframework.web.multipart.MultipartFile)19 CustomAccessDeniedException (com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException)18 MockMultipartFile (org.springframework.mock.web.MockMultipartFile)18 FileLinkDTO (com.ncedu.fooddelivery.api.v1.dto.file.FileLinkDTO)15 Order (com.ncedu.fooddelivery.api.v1.entities.order.Order)14 BufferedImage (java.awt.image.BufferedImage)10 UserInfoDTO (com.ncedu.fooddelivery.api.v1.dto.user.UserInfoDTO)9 Role (com.ncedu.fooddelivery.api.v1.entities.Role)7 NotFoundEx (com.ncedu.fooddelivery.api.v1.errors.notfound.NotFoundEx)7 IOException (java.io.IOException)7 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)7 ProductPosition (com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPosition)6 AlreadyExistsException (com.ncedu.fooddelivery.api.v1.errors.badrequest.AlreadyExistsException)6 PasswordsMismatchException (com.ncedu.fooddelivery.api.v1.errors.badrequest.PasswordsMismatchException)6 CommitAfter (org.apache.tapestry5.jpa.annotations.CommitAfter)6