public void AddPurchaseItem(string userid, string itemid, int qty)
        {
            var oldcartItem = dbcontext.purchaseCarts
                              .Where(x => x.EmployeeId == userid && x.InventoryId == itemid)
                              .FirstOrDefault();

            if (oldcartItem == null)
            {
                var cartItem = new PurchaseCart()
                {
                    Id          = Guid.NewGuid().ToString(),
                    EmployeeId  = userid,
                    InventoryId = itemid,
                    Qty         = qty,
                    Inventory   = dbcontext.inventories.SingleOrDefault(p => p.Id == itemid)
                };
                dbcontext.purchaseCarts.Add(cartItem);
                dbcontext.SaveChanges();
            }
            else
            {
                oldcartItem.Qty = qty;
                dbcontext.Update(oldcartItem);
                dbcontext.SaveChanges();
            }
        }
        public IHttpActionResult PutPurchases(int id, PurchaseCart purchases)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != purchases.purchaseID)
            {
                return(BadRequest());
            }

            db.Entry(purchases).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PurchasesExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
        public IHttpActionResult GetPurchases(int id)
        {
            PurchaseCart purchases = db.Purchases.Find(id);

            if (purchases == null)
            {
                return(NotFound());
            }

            PurchaseCart.VMPurchaseCart vmPurchase = new PurchaseCart.VMPurchaseCart(purchases);

            return(Ok(vmPurchase));
        }
        public IHttpActionResult DeletePurchases(int id)
        {
            PurchaseCart purchases = db.Purchases.Find(id);

            if (purchases == null)
            {
                return(NotFound());
            }

            db.Purchases.Remove(purchases);
            db.SaveChanges();

            return(Ok(purchases));
        }
        public IHttpActionResult PostPurchases(PurchaseCart purchase)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            purchase.createdAt = DateTime.Now;
            purchase.boughtAt  = DateTime.Now;
            purchase.buyerID   = string.IsNullOrEmpty(purchase.buyerID) ? User.Identity.GetUserId() : purchase.buyerID;

            var orders = purchase.products.ToList();

            orders.ForEach(orderProduct => orderProduct.product = null);
            purchase.products = orders;
            db.Purchases.Add(purchase);
            int count = db.SaveChanges();

            return(CreatedAtRoute("DefaultApi", new { id = purchase.purchaseID, count }, purchase));
        }
Esempio n. 6
0
        public async Task <IActionResult> AddtoCart(string brand, int id)
        {
            var loginUser = await userManager.FindByNameAsync(User.Identity.Name);

            var purchasecart = db.purchaseCarts.FirstOrDefault(x => x.UserId == loginUser.Id && x.IsPaid == false);

            if (purchasecart == null)
            {
                purchasecart = new PurchaseCart()
                {
                    AddDate = DateTime.Now,
                    IsPaid  = false,
                    UserId  = loginUser.Id,
                };
                db.Add(purchasecart);
                db.SaveChanges();
            }

            switch (brand)
            {
            case "PSN":
                var ExistGift = db.PurchaseCartItems.FirstOrDefault(x => x.PSNId == id && x.PurchaseCardId == purchasecart.Id);
                if (ExistGift != null)
                {
                    ExistGift.Count++;
                    db.Update(ExistGift);
                    db.SaveChanges();
                }
                else
                {
                    PurchaseCartItems purchaseCartItems = new PurchaseCartItems()
                    {
                        Count          = 1,
                        PSNId          = id,
                        PurchaseCardId = purchasecart.Id,
                        NewPrrice      = 0,
                    };
                    db.Add(purchaseCartItems);
                    db.SaveChanges();
                }
                break;

            case "XBOX":
                var ExistGiftX = db.PurchaseCartItems.FirstOrDefault(x => x.XBOXId == id && x.PurchaseCardId == purchasecart.Id);
                if (ExistGiftX != null)
                {
                    ExistGiftX.Count++;
                    db.Update(ExistGiftX);
                    db.SaveChanges();
                }
                else
                {
                    PurchaseCartItems purchaseCartItemsX = new PurchaseCartItems()
                    {
                        Count          = 1,
                        XBOXId         = id,
                        PurchaseCardId = purchasecart.Id,
                        NewPrrice      = 0,
                    };
                    db.Add(purchaseCartItemsX);
                    db.SaveChanges();
                }
                break;

            default:
                break;
            }


            return(Json(true));
        }