Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/main/java/com/podzilla/warehouse/Events/EventFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,15 @@ public static OrderPackagedEvent createOrderPackagedEvent(
public static OrderAssignedToCourierEvent createOrderAssignedToCourierEvent(
UUID orderId,
UUID courierId,
double totalAmount,
BigDecimal totalAmount,
double orderLatitude,
double orderLongitude,
String signature,
ConfirmationType confirmationType) {
BigDecimal totalAmountBigDecimal = BigDecimal.valueOf(totalAmount);
return new OrderAssignedToCourierEvent(
orderId.toString(),
courierId.toString(),
totalAmountBigDecimal,
totalAmount,
orderLatitude,
orderLongitude,
signature,
Expand Down
46 changes: 42 additions & 4 deletions src/main/java/com/podzilla/warehouse/Models/AssignedOrders.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.podzilla.warehouse.Models;

import com.podzilla.mq.events.ConfirmationType;
import com.podzilla.mq.events.DeliveryAddress;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.CreationTimestamp;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Entity
Expand All @@ -28,18 +32,52 @@ public class AssignedOrders {
@CreationTimestamp
private LocalDateTime assignedAt;

public AssignedOrders(UUID orderId, UUID assignerId, UUID courierId) {
@Column(nullable = false)
@OneToMany(mappedBy = "packagedOrder")
private List<Stock> items;

@Column(nullable = false)
private DeliveryAddress deliveryAddress;
@Column(nullable = false)
private BigDecimal totalAmount;
@Column(nullable = false)
private double orderLatitude;
@Column(nullable = false)
private double orderLongitude;
@Column(nullable = false)
private String signature;
@Column(nullable = false)
private ConfirmationType confirmationType;

public AssignedOrders(UUID orderId, UUID assignerId, UUID courierId, List<Stock> items,
DeliveryAddress deliveryAddress, BigDecimal totalAmount, double orderLatitude,
double orderLongitude, String signature, ConfirmationType confirmationType) {
this.orderId = orderId;
this.assignerId = assignerId;
this.courierId = courierId;
this.assignedAt = LocalDateTime.now();
this.items = items;
this.deliveryAddress = deliveryAddress;
this.totalAmount = totalAmount;
this.orderLatitude = orderLatitude;
this.orderLongitude = orderLongitude;
this.signature = signature;
this.confirmationType = confirmationType;
}


public AssignedOrders(UUID orderId, UUID assignerId, UUID courierId, LocalDateTime assignedAt) {

public AssignedOrders(UUID orderId, UUID assignerId, UUID courierId, LocalDateTime assignedAt,
List<Stock> items, DeliveryAddress deliveryAddress, BigDecimal totalAmount,
double orderLatitude, double orderLongitude, String signature, ConfirmationType confirmationType) {
this.orderId = orderId;
this.assignerId = assignerId;
this.courierId = courierId;
this.assignedAt = assignedAt;
this.items = items;
this.deliveryAddress = deliveryAddress;
this.totalAmount = totalAmount;
this.orderLatitude = orderLatitude;
this.orderLongitude = orderLongitude;
this.signature = signature;
this.confirmationType = confirmationType;
}
}
35 changes: 34 additions & 1 deletion src/main/java/com/podzilla/warehouse/Models/PackagedOrders.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.podzilla.warehouse.Models;

import com.podzilla.mq.events.ConfirmationType;
import com.podzilla.mq.events.DeliveryAddress;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.CreationTimestamp;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Setter
Expand All @@ -17,8 +21,23 @@
public class PackagedOrders {
@Id
private UUID orderId;

private UUID packagerId;
@Column(nullable = false)
@OneToMany(mappedBy = "packagedOrder")
private List<Stock> items;

@Column(nullable = false)
private DeliveryAddress deliveryAddress;
@Column(nullable = false)
private BigDecimal totalAmount;
@Column(nullable = false)
private double orderLatitude;
@Column(nullable = false)
private double orderLongitude;
@Column(nullable = false)
private String signature;
@Column(nullable = false)
private ConfirmationType confirmationType;

@CreationTimestamp
private LocalDateTime packagedAt;
Expand All @@ -32,5 +51,19 @@ public PackagedOrders(UUID orderId, UUID packagerId, LocalDateTime packagedAt) {
public PackagedOrders(UUID orderId) {
this.orderId = orderId;
this.packagedAt = LocalDateTime.now();

}
public PackagedOrders(UUID orderId, UUID packagerId, List<Stock> items, DeliveryAddress deliveryAddress,
BigDecimal totalAmount, double orderLatitude, double orderLongitude, String signature,
ConfirmationType confirmationType) {
this.orderId = orderId;
this.packagerId = packagerId;
this.items = items;
this.deliveryAddress = deliveryAddress;
this.totalAmount = totalAmount;
this.orderLatitude = orderLatitude;
this.orderLongitude = orderLongitude;
this.signature = signature;
this.confirmationType = confirmationType;
}
}
5 changes: 4 additions & 1 deletion src/main/java/com/podzilla/warehouse/Models/Stock.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ public class Stock {

@Column(nullable = false)
private String category;

@ManyToOne
@JoinColumn(name = "order_id", nullable = true)
private PackagedOrders packagedOrder;
@CreationTimestamp
@Column(updatable = false)
private LocalDateTime createdAt;
Expand Down Expand Up @@ -62,4 +64,5 @@ public Stock(String name, int quantity) {
this.quantity = quantity;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.Optional;
import java.util.UUID;

@Repository
public interface PackagedOrdersRepository extends JpaRepository<PackagedOrders, UUID> {
Page<PackagedOrders> findByPackagerIdIsNull(Pageable pageable);
Page<PackagedOrders> findByPackagerId(UUID packagerId, Pageable pageable);
Page<PackagedOrders> findByOrderId(UUID orderId, Pageable pageable);
Optional<PackagedOrders> findOneByOrderId(UUID orderId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
import com.podzilla.mq.events.OrderAssignedToCourierEvent;
import com.podzilla.warehouse.Events.EventFactory;
import com.podzilla.warehouse.Models.AssignedOrders;
import com.podzilla.warehouse.Models.PackagedOrders;
import com.podzilla.warehouse.Repositories.AssignedOrdersRepository;
import com.podzilla.warehouse.Repositories.PackagedOrdersRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
Expand All @@ -27,6 +28,7 @@ public class AssignedOrdersService {

@Autowired
private AssignedOrdersRepository assignedOrdersRepository;
private PackagedOrdersRepository packagedOrdersRepository;

@Cacheable(key = "'orderId:' + #orderId")
public Optional<List<AssignedOrders>> findByOrderId(UUID orderId) {
Expand All @@ -45,13 +47,24 @@ public Optional<List<AssignedOrders>> findByAssignerIdIsNull() {

@CachePut(key = "'orderId:' + #orderId")
public AssignedOrders assignOrder(UUID orderId, UUID assignerId, UUID courierId) {
AssignedOrders assignment = new AssignedOrders(orderId, assignerId, courierId);
PackagedOrders packagedOrder = packagedOrdersRepository.findOneByOrderId(orderId).get();
AssignedOrders assignment = new AssignedOrders(orderId, assignerId, courierId, packagedOrder.getItems(),
packagedOrder.getDeliveryAddress(), packagedOrder.getTotalAmount(), packagedOrder.getOrderLatitude(),
packagedOrder.getOrderLongitude(), packagedOrder.getSignature(), packagedOrder.getConfirmationType());
assignedOrdersRepository.save(assignment);

//TODO: after merge
// OrderAssignedToCourierEvent event = EventFactory.createOrderAssignedToCourierEvent(orderId, courierId);
//
// eventPublisher.publishEvent(EventsConstants.ORDER_ASSIGNED_TO_COURIER, event);
OrderAssignedToCourierEvent event =
EventFactory.createOrderAssignedToCourierEvent(
orderId,
courierId,
assignment.getTotalAmount(),
assignment.getOrderLatitude(),
assignment.getOrderLongitude(),
assignment.getSignature(),
assignment.getConfirmationType()
);

eventPublisher.publishEvent(EventsConstants.ORDER_ASSIGNED_TO_COURIER, event);
return assignment;
}

Expand Down