Exemple #1
0
        public void AddToBasket(int productId)
        {
            var basket        = GetBasket();
            var basketEntries = basket.Find(k => k.Product.ProductId == productId);

            if (basketEntries != null)
            {
                basketEntries.Amount++;
                return;
            }
            var productToAdd = Task.Run(() => _productRepository.GetProductAsync(productId)).Result;

            if (productToAdd != null)
            {
                var basketEntry = new BasketEntry()
                {
                    Product = productToAdd,
                    Amount  = 1,
                    Price   = productToAdd.Price
                };
                basket.Add(basketEntry);
            }

            _sessionManager.Set(SessionKey, basket);
        }
        // GET: Basket
        public ActionResult RemoveOne(string id)
        {
            string userID = HelperMethods.GetUserID(User, Session);

            BasketEntry entry = db.BasketEntries.Where(x => x.UserID == userID && x.ProductID == id.ToString()).SingleOrDefault();

            BasketEntry newEntry = new BasketEntry()
            {
                ProductID = entry.ProductID, UserID = userID, Quantity = entry.Quantity
            };

            if (newEntry.Quantity > 1)
            {
                newEntry.Quantity--;
            }
            else
            {
                return(RedirectToAction("Delete", new { id = newEntry.ProductID }));
            }

            db.BasketEntries.Remove(entry);
            db.BasketEntries.Add(newEntry);

            db.SaveChanges();

            return(RedirectToAction("Index", new { id = userID }));
        }
Exemple #3
0
        public ActionResult DeleteConfirmed(string id)
        {
            BasketEntry basketEntry = db.BasketEntries.Find(id);

            db.BasketEntries.Remove(basketEntry);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
Exemple #4
0
        public ActionResult DeleteFromBasket(int id)
        {
            string      currentUser  = UserAuthentication.WhoAmI(User, Session);
            BasketEntry itemToRemove = db.BasketEntries.Find(currentUser, id);

            db.BasketEntries.Remove(itemToRemove);
            db.SaveChanges();
            return(RedirectToAction("ShowBasket"));
        }
Exemple #5
0
 public ActionResult Edit([Bind(Include = "UserID,ProductID,Quantity")] BasketEntry basketEntry)
 {
     if (ModelState.IsValid)
     {
         db.Entry(basketEntry).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(basketEntry));
 }
Exemple #6
0
        public ActionResult Create([Bind(Include = "UserID,ProductID,Quantity")] BasketEntry basketEntry)
        {
            if (ModelState.IsValid)
            {
                db.BasketEntries.Add(basketEntry);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(basketEntry));
        }
        // GET: Basket
        public ActionResult Delete(string id)
        {
            string userID = HelperMethods.GetUserID(User, Session);

            BasketEntry entry = db.BasketEntries.Where(x => x.UserID == userID && x.ProductID == id.ToString()).SingleOrDefault();

            db.BasketEntries.Remove(entry);

            db.SaveChanges();

            return(RedirectToAction("Index", new { id = userID }));
        }
Exemple #8
0
    public void removeEntry(BasketEntry entry)
    {
        int entryNo = basketEntries.IndexOf(entry);

        basketEntries.Remove(entry);
        if (entryNo != basketEntries.Count - 1)
        {
            foreach (BasketEntry b in basketEntries.GetRange(entryNo, basketEntries.Count - entryNo))
            {
                b.transform.localPosition = new Vector3(b.transform.localPosition.x, b.transform.localPosition.y + 30, b.transform.localPosition.z);
            }
        }
        Destroy(entry.gameObject);
    }
Exemple #9
0
        // GET: BasketEntry/Delete/5
        public ActionResult Delete(string id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            BasketEntry basketEntry = db.BasketEntries.Find(id);

            if (basketEntry == null)
            {
                return(HttpNotFound());
            }
            return(View(basketEntry));
        }
        private void AddToBasket(BasketEntry basketEntry, ICollection <string> errors, Basket basket)
        {
            var item = this._itemService.Get(basketEntry.ItemId);

            if (!IsStock(basketEntry.ItemId, basketEntry.Quantity))
            {
                errors.Add(item.Name);
            }
            else
            {
                var mapped = this._mapperService.Map(item);
                mapped.Quantity = basketEntry.Quantity;
                basket.Add(mapped);
            }
        }
        public static void CopyBasketEntries(string userID, HttpSessionStateBase session)
        {
            using (StoreContext db = new StoreContext())
            {
                List <BasketEntry> basketEntries = db.BasketEntries.Where(x => x.UserID == session.SessionID).ToList();

                foreach (BasketEntry entry in basketEntries)
                {
                    if (db.BasketEntries.Where(x => x.UserID == userID && x.ProductID == entry.ProductID.ToString()).Count() == 0)
                    {
                        BasketEntry newEntry = new BasketEntry()
                        {
                            UserID    = userID,
                            ProductID = entry.ProductID.ToString(),
                            Quantity  = 1
                        };
                        db.BasketEntries.Add(newEntry);
                    }
                    else
                    {
                        BasketEntry old      = db.BasketEntries.Where(x => x.UserID == userID && x.ProductID == entry.ProductID.ToString()).FirstOrDefault();
                        BasketEntry newEntry = new BasketEntry {
                            UserID = old.UserID, ProductID = old.ProductID, Quantity = old.Quantity
                        };
                        newEntry.Quantity++;

                        db.BasketEntries.Remove(old);
                        db.BasketEntries.Add(newEntry);
                    }



                    //db.BasketEntries.Add(new BasketEntry()
                    //{
                    //    UserID = user.Identity.GetUserId(),
                    //    ProductID = entry.ProductID,
                    //    Quantity=entry.Quantity
                    //});
                }

                foreach (BasketEntry entry in basketEntries)
                {
                    db.BasketEntries.Remove(entry);
                }

                db.SaveChanges();
            }
        }
        public IHttpActionResult AddBasketEntry(int userId, [FromBody] BasketEntry basketEntry)
        {
            var errors = new List <string>();
            var basket = this._basketService.GetByUserId(userId);

            this.AddToBasket(basketEntry, errors, basket);

            if (errors.Any())
            {
                var builder = GetErrorMessage(errors);
                return(this.BadRequest(builder));
            }

            this._basketService.Update(basket);
            return(Ok(basket));
        }
Exemple #13
0
        public ActionResult AddToBasket(int?id)
        {
            Session["dummy"] = "Dummy";
            string currentUser = UserAuthentication.WhoAmI(User, Session);

            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Product     product            = db.Products.Find(id);
            BasketEntry entryToAddToBasket = new BasketEntry(currentUser, product.ID);

            if (product == null)
            {
                return(HttpNotFound());
            }
            //TODo: if benne van már a termék(SessionID && ProductID && OrderTime==null alapján) akkor Quantity++
            var query = (from a in db.BasketEntries
                         where a.UserID == currentUser && a.ProductID == product.ID
                         select a).FirstOrDefault();

            if (query != null)
            {
                var result = db.BasketEntries.SingleOrDefault(b => b.UserID == currentUser && b.ProductID == product.ID);
                result.Quantity++;
            }


            else
            {
                entryToAddToBasket.Quantity = 1;
                db.BasketEntries.Add(entryToAddToBasket);
            }
            db.SaveChanges();
            if (Request.UrlReferrer.ToString().EndsWith("/ShowBasket"))
            {
                return(RedirectToAction("ShowBasket", "Basket"));
            }
            return(RedirectToAction(null));
        }
        /// <summary>
        /// Select or deselect an image - adding or removing it from the basket.
        /// </summary>
        /// <param name="image"></param>
        /// <param name="newState"></param>
        public void SetBasketState(Image image, bool newState)
        {
            using var db = new ImageContext();
            bool changed = false;
            var  watch   = new Stopwatch("SetSelection");
            var  entry   = db.BasketEntries.FirstOrDefault(x => x.ImageId.Equals(image.ImageId));

            if (newState && entry == null)
            {
                entry = new BasketEntry {
                    ImageId = image.ImageId, BasketId = CurrentBasket.BasketId, DateAdded = DateTime.UtcNow
                };
                db.Add(entry);
                db.SaveChanges("AddToBasket");

                image.BasketEntry = entry;
                SelectedImages.Add(image);
                changed = true;

                StatusService.Instance.StatusText = $"{image.FileName} added to the basket.";
            }
            else if (!newState && entry != null)
            {
                db.Remove(entry);
                db.SaveChanges("RemoveFromBasket");

                image.BasketEntry = null;
                SelectedImages.Remove(image);
                changed = true;

                StatusService.Instance.StatusText = $"{image.FileName} removed from the basket.";
            }

            watch.Stop();

            if (changed)
            {
                NotifyStateChanged();
            }
        }
Exemple #15
0
        public ActionResult AddToBasket(int?id)
        {
            Session["dummy"] = "Dummy";
            string currentUser = UserAuthentication.WhoAmI(User, Session);

            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Product     product            = db.Products.Find(id);
            BasketEntry entryToAddToBasket = new BasketEntry(currentUser, product.ID);

            if (product == null)
            {
                return(HttpNotFound());
            }

            //TODo: if benne van már a termék(SessionID && ProductID && OrderTime==null alapján) akkor Quantity++
            BasketEntry ProductAlreadyInBasket = (from a in db.BasketEntries
                                                  where a.UserID == currentUser && a.ProductID == product.ID
                                                  select a).FirstOrDefault();

            if (ProductAlreadyInBasket != null)
            {
                ProductAlreadyInBasket.Quantity++;
            }
            else
            {
                entryToAddToBasket.Quantity = 1;
                db.BasketEntries.Add(entryToAddToBasket);
            }

            db.SaveChanges();
            List <BasketEntry> CurrentUserBasketEntries = db.BasketEntries.Where(x => x.UserID == currentUser).ToList();

            TempData["ProductNameAndCount"] = CurrentUserBasketEntries;

            return(RedirectToAction("Index"));
        }
Exemple #16
0
        public ActionResult RemoveFromBasket(int?id)
        {
            Session["dummy"] = "Dummy";
            string currentUser = UserAuthentication.WhoAmI(User, Session);

            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Product     product = db.Products.Find(id);
            BasketEntry entryToRemoveFromBasket = new BasketEntry(currentUser, product.ID);

            if (product == null)
            {
                return(HttpNotFound());
            }
            //TODo: if benne van már a termék(SessionID && ProductID && OrderTime==null alapján) akkor Quantity++
            BasketEntry BasketEntryToLowerQuantity = (from a in db.BasketEntries
                                                      where a.UserID == currentUser && a.ProductID == product.ID
                                                      select a).FirstOrDefault();

            if (BasketEntryToLowerQuantity != null)
            {
                BasketEntryToLowerQuantity.Quantity--;
            }
            if (BasketEntryToLowerQuantity.Quantity == 0)
            {
                db.BasketEntries.Remove(BasketEntryToLowerQuantity);
            }
            db.SaveChanges();

            if (Request.UrlReferrer.ToString().EndsWith("/ShowBasket"))
            {
                return(RedirectToAction("ShowBasket", "Basket"));
            }
            return(RedirectToAction("Index"));
        }
Exemple #17
0
        //public ActionResult AddToBasket([Bind(Include = "ID,CategoryID,Name,Description,Price,UrlFriendlyName,Available")] Product product)
        // /Product/AddToBasket/fbd5af80-a929-4e92-a2da-908e2bd8eb2e
        public ActionResult AddToBasket(Guid id)
        {
            string userID = HelperMethods.GetUserID(User, Session);


            if (ModelState.IsValid)
            {
                if (db.BasketEntries.Where(x => x.UserID == userID && x.ProductID == id.ToString()).Count() == 0)
                {
                    BasketEntry entry = new BasketEntry()
                    {
                        UserID    = userID,
                        ProductID = id.ToString(),
                        Quantity  = 1
                    };
                    db.BasketEntries.Add(entry);
                }
                else
                {
                    BasketEntry old      = db.BasketEntries.Where(x => x.UserID == userID && x.ProductID == id.ToString()).FirstOrDefault();
                    BasketEntry newEntry = new BasketEntry {
                        UserID = old.UserID, ProductID = old.ProductID, Quantity = old.Quantity
                    };
                    newEntry.Quantity++;

                    db.BasketEntries.Remove(old);
                    db.BasketEntries.Add(newEntry);
                }

                db.SaveChanges();
                return(RedirectToAction("Index", "Basket", new { id = userID }));
            }

            //ViewBag.CategoryID = new SelectList(db.ProductCategories, "CategoryId", "CategoryName", product.CategoryID);
            return(View("Index"));
        }
        public ActionResult AddProductToBasket(BasketEntry entry)
        {
            try
            {
                entry.Price            = new UmbracoHelper(UmbracoContext.Current).TypedContent(entry.Id).GetPropertyValue <double>("price");
                entry.DiscountPercents = new UmbracoHelper(UmbracoContext.Current).TypedContent(entry.Id).GetPropertyValue <int>("discount");
                var product = new UmbracoHelper(UmbracoContext.Current).TypedContent(entry.Id);

                int itemsRemained = product.GetPropertyValue <int>("inStock");

                if (entry.Items > itemsRemained)
                {
                    throw new Exception("Not enough items in stock");
                }

                List <BasketEntry> entries = Session["basket"] as List <BasketEntry>;
                if (entries == null)
                {
                    Session["basket"] = new List <BasketEntry>()
                    {
                        entry
                    };
                }
                else
                {
                    bool found = false;

                    var comp = entries.Where(x => x.Name == entry.Name);

                    foreach (var pr in comp)
                    {
                        bool suitable = true;

                        if (pr.Properties.Count() != entry.Properties.Count())
                        {
                            suitable = false;
                        }

                        if (suitable)
                        {
                            var entryProp = entry.Properties.OrderBy(x => x.Name).ToList();
                            var currProp  = pr.Properties.OrderBy(x => x.Name).ToList();

                            for (int i = 0; i < entryProp.Count(); ++i)
                            {
                                if (entryProp[i].Name != currProp[i].Name || entryProp[i].Values[0] != currProp[i].Values[0])
                                {
                                    suitable = false;
                                    break;
                                }
                            }
                        }

                        if (suitable)
                        {
                            found     = true;
                            pr.Items += entry.Items;
                            break;
                        }
                    }


                    if (!found)
                    {
                        entries.Add(entry);
                    }

                    Session["basket"] = entries;
                }

                var      contentService = Services.ContentService;
                IContent content        = contentService.GetById(entry.Id);
                content.SetValue("inStock", itemsRemained - entry.Items);

                System.Threading.Tasks.Task.Run(() => Services.ContentService.SaveAndPublishWithStatus(content));

                return(new HttpStatusCodeResult(HttpStatusCode.OK));
            }
            catch (Exception e)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError, e.Message));
            }
        }
        public ActionResult AddProductToBasket(BasketEntry entry)
        {
            try
            {
                entry.Price            = new UmbracoHelper(UmbracoContext.Current).TypedContent(entry.Id).GetPropertyValue <double>("price");
                entry.DiscountPercents = new UmbracoHelper(UmbracoContext.Current).TypedContent(entry.Id).GetPropertyValue <int>("discount");

                List <BasketEntry> entries = Session["basket"] as List <BasketEntry>;
                if (entries == null)
                {
                    Session["basket"] = new List <BasketEntry>()
                    {
                        entry
                    };
                }
                else
                {
                    bool found = false;

                    var comp = entries.Where(x => x.Name == entry.Name);

                    foreach (var pr in comp)
                    {
                        bool suitable = true;

                        if (pr.Properties.Count() != entry.Properties.Count())
                        {
                            suitable = false;
                        }

                        if (suitable)
                        {
                            var entryProp = entry.Properties.OrderBy(x => x.Name).ToList();
                            var currProp  = pr.Properties.OrderBy(x => x.Name).ToList();

                            for (int i = 0; i < entryProp.Count(); ++i)
                            {
                                if (entryProp[i].Name != currProp[i].Name || entryProp[i].Values[0] != currProp[i].Values[0])
                                {
                                    suitable = false;
                                    break;
                                }
                            }
                        }

                        if (suitable)
                        {
                            found     = true;
                            pr.Items += entry.Items;
                            break;
                        }
                    }


                    if (!found)
                    {
                        entries.Add(entry);
                    }

                    Session["basket"] = entries;
                }

                return(new HttpStatusCodeResult(HttpStatusCode.OK));
            }
            catch
            {
                return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError));
            }
        }
        public ActionResult ChangeItemsNumber(BasketEntry entry)
        {
            try
            {
                entry.Properties = entry.Properties.OrderBy(x => x.Name).ToList();
                List <BasketEntry> entries = Session["basket"] as List <BasketEntry>;
                if (entries == null)
                {
                    throw new Exception("Session has expired");
                }

                var ent = entries.Where(x => x.Id == entry.Id).ToList();

                BasketEntry needed    = null;
                int         neededPos = -1;

                for (int j = 0; j < ent.Count; ++j)
                {
                    bool ok         = true;
                    var  properties = ent[j].Properties.OrderBy(x => x.Name).ToList();

                    if (properties.Count() != entry.Properties.Count())
                    {
                        ok = false;
                    }

                    if (ok)
                    {
                        for (int i = 0; i < properties.Count(); ++i)
                        {
                            if (properties[i].Name != entry.Properties[i].Name || properties[i].Values[0] != entry.Properties[i].Values[0])
                            {
                                ok = false;
                                break;
                            }
                        }
                    }

                    if (ok)
                    {
                        needed    = ent[j];
                        neededPos = j;
                        break;
                    }
                }

                if (needed == null)
                {
                    throw new Exception("Session has expired");
                }

                var      contentService = Services.ContentService;
                IContent content        = contentService.GetById(entry.Id);

                if (entry.Items < needed.Items)
                {
                    content.SetValue("inStock", content.GetValue <int>("inStock") + needed.Items - entry.Items);
                    System.Threading.Tasks.Task.Run(() => Services.ContentService.SaveAndPublishWithStatus(content));
                }
                else
                {
                    int remain = content.GetValue <int>("inStock");

                    if (remain < entry.Items - needed.Items)
                    {
                        throw new Exception("Not enought items in stock");
                    }
                    else
                    {
                        content.SetValue("inStock", content.GetValue <int>("inStock") - (entry.Items - needed.Items));
                        System.Threading.Tasks.Task.Run(() => Services.ContentService.SaveAndPublishWithStatus(content));
                    }
                }

                entries[neededPos].Items = entry.Items;
                Session["basket"]        = entries;

                return(new HttpStatusCodeResult(HttpStatusCode.OK));
            }
            catch (Exception e)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError, e.Message));
            }
        }
 public void BasketEntry_DoesntAllow0Quantity(int quantity)
 {
     _ = new BasketEntry(new Item(name: "Test", price: 1), quantity);
 }