예제 #1
0
        public async Task <IActionResult> PutStock(int id, Stock stock)
        {
            if (id != stock.MovementId)
            {
                return(BadRequest());
            }

            _context.Entry(stock).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!StockExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
예제 #2
0
        public async Task <ActionResult <Products> > PostProducts(Products products)
        {
            var  identity    = HttpContext.User.Identity as ClaimsIdentity;
            bool canContinue = await IdentifyAdminLoginAsync(identity);

            if (canContinue)
            {
                _context.Products.Add(products);
                await _context.SaveChangesAsync();

                // I was thinking of leaving the Stock Insert in a separeted API Resp
                // but lets say that the bussines logic demands to set an inictial stock
                // wend a product is created, even if is zero :p
                var stock = new Stock
                {
                    PurchaseId       = 0,
                    ProductID        = products.ProductId,
                    MovementTypeid   = 1,
                    MovementDate     = DateTime.UtcNow,
                    MovementQuantity = products.inStock,
                    ProductQuantity  = products.inStock
                };
                _context.Stock.Add(stock);
                await _context.SaveChangesAsync();

                return(CreatedAtAction("GetProducts", new { id = products.ProductId }, products));
            }
            return(NotFound());
        }
예제 #3
0
        public async Task <ActionResult <ProductLikes> > PostProductLikes(int id)
        {
            var    identity  = HttpContext.User.Identity as ClaimsIdentity;
            string LogedUser = identity.FindFirst(ClaimTypes.NameIdentifier).Value.ToString();
            var    product   = await _context.Products.FindAsync(id);

            ProductLikes productLikes = new ProductLikes
            {
                ProductId = id,
                UserId    = LogedUser,
                DateLiked = DateTime.UtcNow
            };

            try
            {
                product.ProductLikes         += 1;
                _context.Entry(product).State = EntityState.Modified;
                _context.ProductLikes.Add(productLikes);
                await _context.SaveChangesAsync();
            }
            catch
            {
                return(NoContent());
            }

            return(CreatedAtAction("GetProductLikes", new { id = productLikes.ProductId }, productLikes));
        }
예제 #4
0
        public async Task <ActionResult <Purchase> > PostPurchase(Purchase purchase)
        {
            // here we recover the user or customer login , without careing if it comes or not in
            // the purchse object and we update the purchase object with the corresoponding user
            var identity  = HttpContext.User.Identity as ClaimsIdentity;
            var LogedUser = identity.FindFirst(ClaimTypes.NameIdentifier).Value.ToString();

            purchase.UserId = LogedUser;

            // we recover the existing product quanty
            var products = await _context.Products.FindAsync(purchase.ProductId);

            if ((products.inStock - purchase.PurchaseTotal) > 0)
            {
                _context.Purchase.Add(purchase);
                await _context.SaveChangesAsync();

                var createResult = CreatedAtAction("GetPurchase", new { id = purchase.PurchaseId }, purchase);

                products.inStock = (products.inStock - purchase.PurchaseTotal);

                if (createResult.StatusCode == 201)
                {
                    var stock = new Stock
                    {
                        PurchaseId       = purchase.PurchaseId,
                        ProductID        = purchase.ProductId,
                        MovementTypeid   = 2,
                        MovementDate     = DateTime.UtcNow,
                        MovementQuantity = purchase.PurchaseTotal,
                        ProductQuantity  = products.inStock
                    };
                    _context.Products.Update(products);
                    _context.Stock.Add(stock);
                    await _context.SaveChangesAsync();

                    return(createResult);
                }
                else
                {
                    return(NoContent());
                }
            }
            return(NoContent());
        }