Ejemplo n.º 1
0
        public PurchaseProductResponse PurchaseProduct(PurchaseProductRequest productPurchaseRequest)
        {
            PurchaseProductResponse purchaseProductResponse = new PurchaseProductResponse();
            try
            {
                if (_messageRepository.IsUniqueRequest(productPurchaseRequest.CorrelationId))
                {
                    Product product = _productRepository.FindBy(Guid.Parse(productPurchaseRequest.ProductId));
                    if (product != null)
                    {
                        ProductPurchase productPurchase = null;
                        if (product.ReservationIdValid(Guid.Parse(productPurchaseRequest.ReservationId)))
                        {
                            productPurchase = product.ConfirmPurchaseWith(Guid.Parse(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(string.Concat("Invalid or expired reservation id: ", productPurchaseRequest.ReservationId));
                        }
                        _messageRepository.SaveResponse<PurchaseProductResponse>(productPurchaseRequest.CorrelationId, purchaseProductResponse);
                    }
                    else
                    {
                        throw new ResourceNotFoundException(string.Concat("No product with id ", productPurchaseRequest.ProductId, ", was found."));
                    }
                }
                else
                {
                    purchaseProductResponse = _messageRepository.RetrieveResponseFor<PurchaseProductResponse>(productPurchaseRequest.CorrelationId);
                }
            }
            catch (Exception ex)
            {
                purchaseProductResponse.Exception = ex;
            }

            return purchaseProductResponse;
        }
 public HttpResponseMessage Post(PurchaseProductRequest purchaseProductRequest)
 {
     purchaseProductRequest.CorrelationId = purchaseProductRequest.ReservationId;
     ServiceResponseBase response = _productService.PurchaseProduct(purchaseProductRequest);
     return Request.BuildResponse(response);
 }