コード例 #1
0
        public async Task <IActionResult> Put([FromBody] Delivery delivery, string id)
        {
            logger.LogInformation("In Put action with delivery {Id}: {@DeliveryInfo}", id, delivery.ToLogInfo());

            try
            {
                var internalDelivery = delivery.ToInternal();

                // Adds new inflight delivery
                await deliveryRepository.CreateAsync(internalDelivery);

                // Adds the delivery created status event
                var deliveryTrackingEvent = new DeliveryTrackingEvent {
                    DeliveryId = delivery.Id, Stage = DeliveryStage.Created
                };
                await deliveryTrackingRepository.AddAsync(deliveryTrackingEvent);

                return(CreatedAtRoute("GetDelivery", new { id = delivery.Id }, delivery));
            }
            catch (DuplicateResourceException)
            {
                //This method is mainly used to create deliveries. If the delivery already exists then update
                logger.LogInformation("Updating resource with delivery id: {DeliveryId}", id);

                var internalDelivery = delivery.ToInternal();

                // Updates inflight delivery
                await deliveryRepository.UpdateAsync(id, internalDelivery);

                return(NoContent());
            }
        }
コード例 #2
0
        public async Task <IActionResult> Create([FromBody] Delivery delivery)
        {
            if (delivery == null)
            {
                return(BadRequest());
            }

            if (delivery.Address == null ||
                delivery.Car == null ||
                delivery.Courier == null)
            {
                return(BadRequest());
            }

            Delivery added = await deliveryRepository.CreateAsync(delivery);

            return(CreatedAtRoute("GetDelivery", new { id = added.Id }, delivery));
        }
        public async Task <IActionResult> Put([FromBody] Delivery delivery, string id)
        {
            logger.LogInformation("In Put action with delivery {Id}: {Delivery}", id, delivery);

            // Adds new inflight delivery
            var success = await deliveryRepository.CreateAsync(delivery);

            if (success)
            {
                return(CreatedAtRoute("GetDelivery", new { id = delivery.Id }, delivery));
            }

            //This method is mainly used to create deliveries. If the delivery already exists then update
            logger.LogInformation("Updating resource with delivery id: {DeliveryId}", id);

            // Updates inflight delivery
            await deliveryRepository.UpdateAsync(id, delivery);

            return(NoContent());
        }
コード例 #4
0
        public async Task <OperationResult <SaleOrderDto> > CreateSaleAsync(CreateSaleInput input)
        {
            var validationResult = await _saleValidator.CreateSaleAsync(input);

            if (validationResult.IsSuccess)
            {
                IEnumerable <Product> products = _productRepository.GetAvailableProducts(input.Products.Select(x => x.ProductId));

                Customer customer = await _customerRepository.GetCustomer(input.CustomerEmail);

                AddressCustomer addressCustomer = await _addressRepository.GetByCustomerAsync(input.CustomerEmail);

                Address address = addressCustomer.Addresses.Single(x => x.Id == input.AddressId);

                CouponCode couponCode = null;

                if (!string.IsNullOrWhiteSpace(input.CouponCode))
                {
                    couponCode = await _couponCodeRepository.GetCouponAsync(input.CouponCode);
                }

                Order order = new()
                {
                    Status        = OrderStatus.Created,
                    CreationDate  = DateTime.Now,
                    CustomerEmail = input.CustomerEmail,
                    Type          = OrderType.ProductSale,
                    TotalAmount   = CalculateTotalAmount(couponCode, products, customer)
                };

                order = await _orderRepository.CreateAsync(order);

                Sale sale = new()
                {
                    CouponCode = input.CouponCode,
                    OrderId    = order.Id,
                    Products   = products.Select(x => x.Id),
                    Status     = SaleStatus.PendingPayment
                };

                sale = await _saleRepository.CreateAsync(sale);

                SaleDetail saleDetail = new()
                {
                    SaleId   = sale.Id,
                    Products = input.Products
                };

                saleDetail = await _saleDetailRepository.CreateAsync(saleDetail);

                IPaymentProvider paymentProvider = _paymentProviderFactory.CreatePaymentProvider(input.PaymentProvider);

                Invoice invoice = await paymentProvider.CreateInvoice(order);

                invoice = await _invoiceRepository.CreateAsync(invoice);

                Delivery delivery = new()
                {
                    AddressId = input.AddressId,
                    OrderId   = order.Id,
                    Status    = DeliveryStatus.Created
                };

                delivery = await _deliveryRepository.CreateAsync(delivery);

                return(OperationResult <SaleOrderDto> .Success(new(sale.ConvertToDto(), order.ConvertToDto(), invoice.ConvertToDto())));
            }

            return(OperationResult <SaleOrderDto> .Fail(validationResult));
        }

        private static decimal CalculateTotalAmount(CouponCode couponCode, IEnumerable <Product> products, Customer customer)
        {
            decimal totalAmount = products.Sum(x => x.Price);

            if (couponCode is null)
            {
                return(totalAmount);
            }

            if (totalAmount < couponCode.MinAmount || totalAmount > couponCode.MaxAmount)
            {
                return(totalAmount);
            }

            if (DateTime.Today < couponCode.DateStart || DateTime.Today > couponCode.DateExpire)
            {
                return(totalAmount);
            }

            List <Product> productsToOff = new();


            if (couponCode.Customers?.Contains(customer.Email) ?? false)
            {
                productsToOff.AddRange(products);
            }
            else
            {
                if (couponCode.Categories?.Any() ?? false)
                {
                    productsToOff.AddRange(products.Where(x => couponCode.Categories.Contains(x.Category)));
                }

                if (couponCode.SubCategories?.Any() ?? false)
                {
                    productsToOff.AddRange(products.Where(x => couponCode.SubCategories.Any(y => y.Category == x.Category && y.SubCategory == x.SubCategory)));
                }

                if (couponCode.Products?.Any() ?? false)
                {
                    productsToOff.AddRange(products.Where(x => couponCode.Products.Contains(x.Id)));
                }
            }

            decimal valueOff = couponCode.Type switch
            {
                CouponCodeOffType.Percentage => (couponCode.Value / productsToOff.Sum(x => x.Price)) * 100,
                CouponCodeOffType.SpecificAmount => productsToOff.Sum(x => x.Price) - couponCode.Value,
                _ => throw new ArgumentException("CouponCodeOffType invalido.", nameof(couponCode))
            };


            return(totalAmount - valueOff);
        }
    }
}