diff --git a/SwiftParcel.ExternalAPI.Baronomat/src/SwiftParcel.ExternalAPI.Baronomat.Application/SwiftParcel.ExternalAPI.Baronomat.Application/Commands/CreateOrder.cs b/SwiftParcel.ExternalAPI.Baronomat/src/SwiftParcel.ExternalAPI.Baronomat.Application/SwiftParcel.ExternalAPI.Baronomat.Application/Commands/CreateOrder.cs index 83a4450..4544a98 100644 --- a/SwiftParcel.ExternalAPI.Baronomat/src/SwiftParcel.ExternalAPI.Baronomat.Application/SwiftParcel.ExternalAPI.Baronomat.Application/Commands/CreateOrder.cs +++ b/SwiftParcel.ExternalAPI.Baronomat/src/SwiftParcel.ExternalAPI.Baronomat.Application/SwiftParcel.ExternalAPI.Baronomat.Application/Commands/CreateOrder.cs @@ -8,6 +8,7 @@ public class CreateOrder: ICommand { public Guid OrderId { get; } public Guid CustomerId { get; } + public Guid ParcelId { get; } public string SourceStreet { get; } public string SourceBuildingNumber { get; } public string SourceApartmentNumber { get; } @@ -31,10 +32,11 @@ public class CreateOrder: ICommand public string Name { get; } public string Email { get; } - public CreateOrder(Guid orderId, Guid customerId, string sourceStreet, string sourceBuildingNumber, string sourceApartmentNumber, string sourceCity, string sourceZipCode, string sourceCountry, string destinationStreet, string destinationBuildingNumber, string destinationApartmentNumber, string destinationCity, string destinationZipCode, string destinationCountry, double price, double width, double height, double depth, double weight, DateTime deliveryDate, string priority, bool atWeekend, string name, string email) + public CreateOrder(Guid orderId, Guid customerId, Guid parcelId, string sourceStreet, string sourceBuildingNumber, string sourceApartmentNumber, string sourceCity, string sourceZipCode, string sourceCountry, string destinationStreet, string destinationBuildingNumber, string destinationApartmentNumber, string destinationCity, string destinationZipCode, string destinationCountry, double price, double width, double height, double depth, double weight, DateTime deliveryDate, string priority, bool atWeekend, string name, string email) { OrderId = orderId; CustomerId = customerId; + ParcelId = parcelId; SourceStreet = sourceStreet; SourceBuildingNumber = sourceBuildingNumber; SourceApartmentNumber = sourceApartmentNumber; diff --git a/SwiftParcel.ExternalAPI.Baronomat/src/SwiftParcel.ExternalAPI.Baronomat.Application/SwiftParcel.ExternalAPI.Baronomat.Application/Commands/Handlers/CreateOrderHandler.cs b/SwiftParcel.ExternalAPI.Baronomat/src/SwiftParcel.ExternalAPI.Baronomat.Application/SwiftParcel.ExternalAPI.Baronomat.Application/Commands/Handlers/CreateOrderHandler.cs index d48b788..f3c2bf7 100644 --- a/SwiftParcel.ExternalAPI.Baronomat/src/SwiftParcel.ExternalAPI.Baronomat.Application/SwiftParcel.ExternalAPI.Baronomat.Application/Commands/Handlers/CreateOrderHandler.cs +++ b/SwiftParcel.ExternalAPI.Baronomat/src/SwiftParcel.ExternalAPI.Baronomat.Application/SwiftParcel.ExternalAPI.Baronomat.Application/Commands/Handlers/CreateOrderHandler.cs @@ -11,15 +11,23 @@ public class CreateOrderHandler : ICommandHandler { private readonly IOrdersServiceClient _ordersServiceClient; private readonly IOfferSnippetRepository _offerSnippetRepository; + private readonly IInquiryOfferRepository _inquiryOfferRepository; - public CreateOrderHandler(IOrdersServiceClient ordersServiceClient, IOfferSnippetRepository offerSnippetRepository) + public CreateOrderHandler(IOrdersServiceClient ordersServiceClient, IOfferSnippetRepository offerSnippetRepository, + IInquiryOfferRepository inquiryOfferRepository) { _ordersServiceClient = ordersServiceClient; _offerSnippetRepository = offerSnippetRepository; + _inquiryOfferRepository = inquiryOfferRepository; } public async Task HandleAsync(CreateOrder command, CancellationToken cancellationToken) { - var orderRequest = new OrderRequestDto(command); + var inquiryOffer = await _inquiryOfferRepository.GetAsync(command.ParcelId); + if (inquiryOffer is null) + { + throw new InquiryOfferNotFoundException(command.ParcelId); + } + var orderRequest = new OrderRequestDto(command, inquiryOffer.TotalPrice); var response = await _ordersServiceClient.PostAsync(orderRequest); if(response == null) { diff --git a/SwiftParcel.ExternalAPI.Baronomat/src/SwiftParcel.ExternalAPI.Baronomat.Application/SwiftParcel.ExternalAPI.Baronomat.Application/DTO/OrderAddressDto.cs b/SwiftParcel.ExternalAPI.Baronomat/src/SwiftParcel.ExternalAPI.Baronomat.Application/SwiftParcel.ExternalAPI.Baronomat.Application/DTO/OrderAddressDto.cs index 5fff336..881ef65 100644 --- a/SwiftParcel.ExternalAPI.Baronomat/src/SwiftParcel.ExternalAPI.Baronomat.Application/SwiftParcel.ExternalAPI.Baronomat.Application/DTO/OrderAddressDto.cs +++ b/SwiftParcel.ExternalAPI.Baronomat/src/SwiftParcel.ExternalAPI.Baronomat.Application/SwiftParcel.ExternalAPI.Baronomat.Application/DTO/OrderAddressDto.cs @@ -1,6 +1,8 @@ namespace SwiftParcel.ExternalAPI.Baronomat.Application.DTO; using SwiftParcel.ExternalAPI.Baronomat.Core.Entities; +using System.Text.Json.Serialization; + public class OrderAddressDto { public int id { get; set; } @@ -15,6 +17,22 @@ public class OrderAddressDto public string postalCode { get; set; } public string email { get; set; } public string phoneNumber { get; set; } + [JsonConstructor] + public OrderAddressDto(int id, string firstName, string surname, string country, string city, string street, string homeNumber, string apartmentNumber, string note, string postalCode, string email, string phoneNumber) + { + this.id = id; + this.firstName = firstName; + this.surname = surname; + this.country = country; + this.city = city; + this.street = street; + this.homeNumber = homeNumber; + this.apartmentNumber = apartmentNumber; + this.note = note; + this.postalCode = postalCode; + this.email = email; + this.phoneNumber = phoneNumber; + } public OrderAddressDto(AddressDto address, string name, string email) { @@ -29,7 +47,7 @@ public OrderAddressDto(AddressDto address, string name, string email) street = address.Street; homeNumber = address.BuildingNumber; apartmentNumber = address.ApartmentNumber; - note = "Don't throw"; + note = "Dont throw"; postalCode = address.ZipCode; this.email = email; phoneNumber = "123456789"; diff --git a/SwiftParcel.ExternalAPI.Baronomat/src/SwiftParcel.ExternalAPI.Baronomat.Application/SwiftParcel.ExternalAPI.Baronomat.Application/DTO/OrderRequestDto.cs b/SwiftParcel.ExternalAPI.Baronomat/src/SwiftParcel.ExternalAPI.Baronomat.Application/SwiftParcel.ExternalAPI.Baronomat.Application/DTO/OrderRequestDto.cs index 7c021c6..22bb195 100644 --- a/SwiftParcel.ExternalAPI.Baronomat/src/SwiftParcel.ExternalAPI.Baronomat.Application/SwiftParcel.ExternalAPI.Baronomat.Application/DTO/OrderRequestDto.cs +++ b/SwiftParcel.ExternalAPI.Baronomat/src/SwiftParcel.ExternalAPI.Baronomat.Application/SwiftParcel.ExternalAPI.Baronomat.Application/DTO/OrderRequestDto.cs @@ -4,31 +4,31 @@ namespace SwiftParcel.ExternalAPI.Baronomat.Application.DTO { public class OrderRequestDto { - public OrderAddressDto SenderAddress { get; set; } - public OrderAddressDto RecipientAddress { get; set; } - public int PriceCents { get; set; } - public int ShipmentWidthMm { get; set; } - public int ShipmentHeightMm { get; set; } - public int ShipmentLengthMm { get; set; } - public int ShipmentWeightMg { get; set; } - public string DeliveryDate { get; set; } - public bool HighPriority { get; set; } - public bool WeekendDelivery { get; set; } + public OrderAddressDto senderAddress { get; set; } + public OrderAddressDto recipientAddress { get; set; } + public int priceCents { get; set; } + public int shipmentWidthMm { get; set; } + public int shipmentHeightMm { get; set; } + public int shipmentLengthMm { get; set; } + public int shipmentWeightMg { get; set; } + public string deliveryDate { get; set; } + public bool highPriority { get; set; } + public bool weekendDelivery { get; set; } - public OrderRequestDto(CreateOrder command) + public OrderRequestDto(CreateOrder command, double price) { var source = new AddressDto(command.SourceStreet, command.SourceBuildingNumber, command.SourceApartmentNumber, command.SourceCity, command.SourceZipCode, command.SourceCountry); var destination = new AddressDto(command.DestinationStreet, command.DestinationBuildingNumber, command.DestinationApartmentNumber, command.DestinationCity, command.DestinationZipCode, command.DestinationCountry); - SenderAddress = new OrderAddressDto(source, command.Name, command.Email); - RecipientAddress = new OrderAddressDto(destination, command.Name, command.Email); - PriceCents = (int)(command.Price * 100); - ShipmentWidthMm = (int)(command.Width * 1000); - ShipmentHeightMm = (int)(command.Height * 1000); - ShipmentLengthMm = (int)(command.Depth * 1000); - ShipmentWeightMg = (int)(command.Weight * 1000); - DeliveryDate = command.DeliveryDate.ToString("yyyy-MM-dd"); - HighPriority = command.Priority == "High"; - WeekendDelivery = command.AtWeekend; + senderAddress = new OrderAddressDto(source, command.Name, command.Email); + recipientAddress = new OrderAddressDto(destination, command.Name, command.Email); + priceCents = (int)(price * 100); + shipmentWidthMm = (int)(command.Width * 1000); + shipmentHeightMm = (int)(command.Height * 1000); + shipmentLengthMm = (int)(command.Depth * 1000); + shipmentWeightMg = (int)(command.Weight * 1000); + deliveryDate = command.DeliveryDate.ToString("yyyy-MM-dd"); + highPriority = command.Priority == "High"; + weekendDelivery = command.AtWeekend; } } } \ No newline at end of file diff --git a/SwiftParcel.ExternalAPI.Baronomat/src/SwiftParcel.ExternalAPI.Baronomat.Application/SwiftParcel.ExternalAPI.Baronomat.Application/DTO/OrderResponseDto.cs b/SwiftParcel.ExternalAPI.Baronomat/src/SwiftParcel.ExternalAPI.Baronomat.Application/SwiftParcel.ExternalAPI.Baronomat.Application/DTO/OrderResponseDto.cs index c4b9887..e7a0414 100644 --- a/SwiftParcel.ExternalAPI.Baronomat/src/SwiftParcel.ExternalAPI.Baronomat.Application/SwiftParcel.ExternalAPI.Baronomat.Application/DTO/OrderResponseDto.cs +++ b/SwiftParcel.ExternalAPI.Baronomat/src/SwiftParcel.ExternalAPI.Baronomat.Application/SwiftParcel.ExternalAPI.Baronomat.Application/DTO/OrderResponseDto.cs @@ -1,3 +1,5 @@ +using System.Text.Json.Serialization; + namespace SwiftParcel.ExternalAPI.Baronomat.Application.DTO { public class OrderResponseDto @@ -15,6 +17,24 @@ public class OrderResponseDto public DateTime DeliveryDate { get; set; } public string Priority { get; set; } public bool WeekendDelivery { get; set; } + + [JsonConstructor] + public OrderResponseDto(int id, OrderAddressDto senderAddress, OrderAddressDto recipientAddress, ApiUserDto apiUser, string orderStatus, int priceCents, int shipmentWidthMm, int shipmentHeightMm, int shipmentLengthMm, int shipmentWeightMg, DateTime deliveryDate, string priority, bool weekendDelivery) + { + Id = id; + SenderAddress = senderAddress; + RecipientAddress = recipientAddress; + ApiUser = apiUser != null ? new ApiUserDto(apiUser.Id, apiUser.User, apiUser.HostName) : new ApiUserDto(); + OrderStatus = orderStatus; + PriceCents = priceCents; + ShipmentWidthMm = shipmentWidthMm; + ShipmentHeightMm = shipmentHeightMm; + ShipmentLengthMm = shipmentLengthMm; + ShipmentWeightMg = shipmentWeightMg; + DeliveryDate = deliveryDate; + Priority = priority; + WeekendDelivery = weekendDelivery; + } } public class ApiUserDto @@ -22,5 +42,15 @@ public class ApiUserDto public int Id { get; set; } public object User { get; set; } public string HostName { get; set; } + [JsonConstructor] + public ApiUserDto(int id, object user, string hostName) + { + Id = id; + User = user; + HostName = hostName; + } + public ApiUserDto() + { + } } } \ No newline at end of file diff --git a/SwiftParcel.ExternalAPI.Baronomat/src/SwiftParcel.ExternalAPI.Baronomat.Application/SwiftParcel.ExternalAPI.Baronomat.Application/Exceptions/InquiryOfferNotFoundException.cs b/SwiftParcel.ExternalAPI.Baronomat/src/SwiftParcel.ExternalAPI.Baronomat.Application/SwiftParcel.ExternalAPI.Baronomat.Application/Exceptions/InquiryOfferNotFoundException.cs new file mode 100644 index 0000000..396b11b --- /dev/null +++ b/SwiftParcel.ExternalAPI.Baronomat/src/SwiftParcel.ExternalAPI.Baronomat.Application/SwiftParcel.ExternalAPI.Baronomat.Application/Exceptions/InquiryOfferNotFoundException.cs @@ -0,0 +1,12 @@ +namespace SwiftParcel.ExternalAPI.Baronomat.Application.Exceptions +{ + public class InquiryOfferNotFoundException : AppException + { + public override string Code { get; } = "inqury_offer_not_found"; + public Guid InquiryOfferId { get; } + public InquiryOfferNotFoundException(Guid inquiryOfferId) : base("inqury_offer_not_found") + { + InquiryOfferId = inquiryOfferId; + } + } +} \ No newline at end of file diff --git a/SwiftParcel.Services.Orders/src/SwiftParcel.Services.Orders.Application/SwiftParcel.Services.Orders.Application/Commands/CreateOrderBaronomat.cs b/SwiftParcel.Services.Orders/src/SwiftParcel.Services.Orders.Application/SwiftParcel.Services.Orders.Application/Commands/CreateOrderBaronomat.cs index 2d4656d..2d2e5a5 100644 --- a/SwiftParcel.Services.Orders/src/SwiftParcel.Services.Orders.Application/SwiftParcel.Services.Orders.Application/Commands/CreateOrderBaronomat.cs +++ b/SwiftParcel.Services.Orders/src/SwiftParcel.Services.Orders.Application/SwiftParcel.Services.Orders.Application/Commands/CreateOrderBaronomat.cs @@ -8,6 +8,7 @@ public class CreateOrderBaronomat: ICommand { public Guid OrderId { get; } public Guid CustomerId { get; } + public Guid ParcelId { get; } public string SourceStreet { get; } public string SourceBuildingNumber { get; } public string SourceApartmentNumber { get; } @@ -35,6 +36,7 @@ public CreateOrderBaronomat(CreateOrder command, ParcelDto parcel) { OrderId = command.OrderId; CustomerId = command.CustomerId; + ParcelId = parcel.Id; SourceStreet = parcel.Source.Street; SourceBuildingNumber = parcel.Source.BuildingNumber; SourceApartmentNumber = parcel.Source.ApartmentNumber; @@ -47,6 +49,7 @@ public CreateOrderBaronomat(CreateOrder command, ParcelDto parcel) DestinationCity = parcel.Destination.City; DestinationZipCode = parcel.Destination.ZipCode; DestinationCountry = parcel.Destination.Country; + Price = (double)parcel.CalculatedPrice; Width = parcel.Width; Height = parcel.Height; Depth = parcel.Depth;