Exemple #1
0
        public async Task <IActionResult> Create([FromBody] TransactionVM dataVM)
        {
            var dataT = new TransactionVM
            {
                UserId = dataVM.UserId,
                Total  = dataVM.Total,
            };
            var create = _repo.Create(dataT);

            if (create > 0)
            {
                var getTransactionId = _context.Transactions.Max(x => x.TransactionId);
                foreach (TransactionItemVM ti in dataVM.transactionItems)
                {
                    var getProductId = _context.Products.SingleOrDefault(x => x.ProductName == ti.ProductName);
                    var data         = new TransactionItem
                    {
                        TransactionId = getTransactionId,
                        ProductId     = getProductId.ProductId,
                        Quantity      = ti.Quantity,
                        SubTotal      = ti.SubTotal,
                    };
                    await _context.TransactionItems.AddAsync(data);

                    var getCart = _context.Carts.SingleOrDefault(x => x.ProductId == getProductId.ProductId && x.UserId == dataVM.UserId);
                    _context.Carts.Remove(getCart);
                }
                await _context.SaveChangesAsync();

                return(Ok("Successfully Created"));
            }
            return(BadRequest("Input User Not Successfully"));
        }