Skip to content

Commit

Permalink
#121: add whole integration with external api
Browse files Browse the repository at this point in the history
  • Loading branch information
eggwhat committed Jan 24, 2024
1 parent d3bccc2 commit 95007f7
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,23 @@ public class CreateOrderHandler : ICommandHandler<CreateOrder>
{
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)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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; }
Expand All @@ -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)
{
Expand All @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Text.Json.Serialization;

namespace SwiftParcel.ExternalAPI.Baronomat.Application.DTO
{
public class OrderResponseDto
Expand All @@ -15,12 +17,40 @@ 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
{
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()
{
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 95007f7

Please sign in to comment.