Exemple #1
0
        public async Task <IHttpActionResult> Post(CustomerOrderDTO orderDetail)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (orderDetail == null)
            {
                return(BadRequest("OrderDetails should not have been null while placing the customer order"));
            }
            decimal deductWalletAmount = 0;

            //TODO: Verify pay amount.
            try
            {
                await UpdateStockOfProductsAsync(orderDetail.ProductsConsumed);

                deductWalletAmount = await ComputeTransactionAmountAsync(orderDetail);

                var customerOrder  = CreateNewCustomerOrder(orderDetail, deductWalletAmount);
                var transactionDTO = new CustomerTransactionDTO()
                {
                    CustomerId        = orderDetail.CustomerId,
                    TransactionAmount = Math.Abs(deductWalletAmount),
                    IsCredit          = deductWalletAmount > 0 ? true : false,
                    Description       = customerOrder.CustomerOrderNo,
                };
                var transaction = await transactionDTO.CreateNewTransactionAsync(db);

                var customerOrderTransaction = CreateNewCustomerOrderTransaction(customerOrder, transaction);

                await AddIntoCustomerOrderProductAsync(orderDetail.ProductsConsumed, customerOrder.CustomerOrderId);

                //Data Analytics call
                await UpdateCustomerNetWorthAsync(orderDetail);

                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                throw;
            }
            catch (Exception e)
            { return(BadRequest(e.ToString())); }
            return(Ok(deductWalletAmount));
        }
        public async Task <IHttpActionResult> Post(CustomerTransactionDTO transactionDTO)
        {
            if (transactionDTO == null)
            {
                return(BadRequest("TransactionDTO cannot be null, on creating transaction for customer"));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (transactionDTO.IsCredit == true)
            {
                throw new Exception("Currently transaction of type credit is not supported through this API");
            }

            var transaction = await transactionDTO.CreateNewTransactionAsync(db);

            await db.SaveChangesAsync();

            return(Ok(transaction));
        }