コード例 #1
0
        public async Task <IActionResult> Product(string guid)
        {
            if (string.IsNullOrEmpty(guid))
            {
                return(BadRequest("Guid is not provided."));
            }

            var product = await _repo.Product(guid);

            if (product != null)
            {
                return(Ok(product));
            }
            return(NotFound());
        }
コード例 #2
0
        public async Task <(TransactionReturnDto transaction, List <ProductReturnDto> unableProducts)> Create(string uGuid, List <ProductBuyDto> wantedProducts)
        {
            // Create transaction:
            var transaction = new Transaction()
            {
                Guid             = Guid.NewGuid().ToString(),
                IssueDate        = DateTime.Now,
                IssuerGuid       = uGuid,
                Status           = TransactionStatus.Pending,
                ApprovalDate     = null,
                CancellationDate = null
            };

            // Create SoldProducts(Content field of Transaction) & calc total amount:
            decimal amount  = 0;
            var     content = new List <SoldProduct>();
            // variable to store products which user couln't buy:
            var unableToBuyProducts = new List <ProductReturnDto>();

            for (int i = 0; i < wantedProducts.Count; i++)
            {
                // get product with wantedProducts' Guid field:
                var product = await _context.Products
                              .Select(p => new
                {
                    StockCount  = p.Count,
                    SoldProduct = new SoldProduct()
                    {
                        Guid            = Guid.NewGuid().ToString(),
                        ProductGuid     = p.Guid,
                        SoldCount       = wantedProducts[i].Count,
                        TransactionGuid = transaction.Guid
                    }
                })
                              .FirstOrDefaultAsync(
                    p => p.SoldProduct.ProductGuid == wantedProducts[i].Guid &&
                    p.StockCount >= wantedProducts[i].Count
                    );

                if (product != null)
                {
                    // If product is found add it to transaction content
                    // and add price to amount:
                    var p = await _context.Products
                            .FirstOrDefaultAsync(p => p.Guid == wantedProducts[i].Guid);

                    amount += p.Price * product.SoldProduct.SoldCount;
                    content.Add(product.SoldProduct);
                }
                else
                {
                    // To return products which user couldn't buy add them to 'unableToBuyProducts' list:
                    var unableProduct = await _productsRepo
                                        .Product(wantedProducts[i].Guid);

                    unableToBuyProducts.Add(unableProduct);
                }
            }
            transaction.Content = content;
            transaction.Amount  = amount;

            await _context.Transactions.AddAsync(transaction);

            var result = await _context.SaveChangesAsync();

            // Return transaction:
            if (result > 0)
            {
                var transactionReturn = await GetTransaction(transaction.Guid);

                return(transactionReturn, unableToBuyProducts);
            }
            return(null, null);
        }