public JsonResult AddItems(ObjectId id, int qty, ObjectId sponsorID) { var cart = Context.Carts.GetCart(ObjectId.Empty, ClientIP) ?? new Cart { CartID = ObjectId.GenerateNewId(), ClientIP = ClientIP, Items = new List<CartItem>() }; bool success = true; string message = ""; int totalQty = 0; double totalPrice = 0.0; try { if (id == ObjectId.Empty) { var active = Context.Phases.GetActivePhase(); id = active.PhaseID; } var phase = Context.Phases.GetPhase(id); var item = new CartItem() { Price = phase.SpotPrice, SponsorPrice = phase.SponsorPrice, Phase = phase.PhaseName, SpotID = ObjectId.Empty, Qty = qty, PhaseID = id }; var sponsor = Context.Sponsors.GetSponsor(sponsorID); if (sponsor != null) { item.SponsorID = sponsorID; item.Sponsor = sponsor.Name; } success = Context.Carts.AddItem(ClientIP, item, ref cart); totalQty = cart.Items.Sum(i => i.Qty); totalPrice = cart.Items.Sum(i => i.Qty * i.Price); } catch (Exception exp) { success = false; message = exp.Message; } return Json(new { success = success, quantity = totalQty, total = totalPrice }); }
/// <summary> /// Inserts a new role into the db. /// </summary> /// <param name="role">The role to add.</param> /// <returns>true if the role is valid and successfully added.</returns> public bool AddItem(string clientIP, CartItem item, ref Cart cart) { // first check to see if the cart doesn't currently exist var db = (from c in Carts.AsQueryable() where c.ClientIP == clientIP select c); // retrieve or create cart if (db.Count() > 0) { cart = db.First(); } else { cart = new Cart() { ClientIP = clientIP, CartID = ObjectId.GenerateNewId(), Items = new List<CartItem>() }; } // set the item bool found = false; foreach (var itm in cart.Items) { if (itm.SpotID == item.SpotID) { found = true; itm.Qty += item.Qty; if (item.Qty == 0) { cart.Items.Remove(itm); } break; } } if (!found) { cart.Items.Add(item); } // now save var result = Carts.Save(cart, WriteConcern.Acknowledged); return result.Ok; }
public JsonResult Add(ObjectId id, ObjectId phaseID) { bool success = true; try { if (phaseID == ObjectId.Empty) { var spot = Context.Spots.GetSpot(id); phaseID = spot.PhaseID; } var phase = Context.Phases.GetPhase(phaseID); var item = new CartItem() { Price = phase.SpotPrice, SponsorPrice = phase.SponsorPrice, Phase = phase.PhaseName, SpotID = id }; var cart = CurrentCart; cart.Items.Add(item); Context.Carts.AddItem(ClientIP, item, ref cart); } catch { success = false; } return Json(new { success = success }); }