public HttpResponseMessage Post(IPurchaseProductRequest purchaseProductRequest) { purchaseProductRequest.CorrelationId = purchaseProductRequest.ReservationId; ServiceResponseBase response = (ServiceResponseBase)_productService.PurchaseProduct(purchaseProductRequest); return(Request.BuildResponse(response)); }
public IPurchaseProductResponse PurchaseProduct(IPurchaseProductRequest productPurchaseRequest) { PurchaseProductResponse purchaseProductResponse = new PurchaseProductResponse(); try { if (_messageRepository.IsUniqueRequest(productPurchaseRequest.CorrelationId)) { Product product = (Product)_productRepository.FindBy(productPurchaseRequest.ProductId); if (product != null) { IProductPurchase productPurchase = null; if (product.ReservationIsValid(productPurchaseRequest.ReservationId)) { productPurchase = product.ConfirmPurchaseWith(productPurchaseRequest.ReservationId); _productRepository.Save(product); purchaseProductResponse.ProductId = productPurchase.Product.Id.ToString(); purchaseProductResponse.PurchaseId = productPurchase.Id.ToString(); purchaseProductResponse.ProductQuantity = productPurchase.ProductQuantity; purchaseProductResponse.ProductName = productPurchase.Product.Name; } else { throw new ResourceNotFoundException($"invalid or expired reservation id: {productPurchaseRequest.ReservationId}"); } _messageRepository.SaveResponse(productPurchaseRequest.CorrelationId, purchaseProductResponse); } else { throw new ResourceNotFoundException($"no product with id {productPurchaseRequest.ProductId} was found"); } } else { purchaseProductResponse = _messageRepository.RetrieveResponseFor <PurchaseProductResponse>(productPurchaseRequest.CorrelationId); } } catch (Exception ex) { purchaseProductResponse.Exception = ex; } return(purchaseProductResponse); }