Exemple #1
0
        /// <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 ActionResult Checkout(Cart model)
        {
            var cart = CurrentCart;

            foreach (var item in cart.Items)
            {
                var sponsorSpot = Context.SponsorSpots.GetSpot(item.SpotID);
                sponsorSpot.Taken = true;
                sponsorSpot.SponsorID = model.SponsorID;

                Context.SponsorSpots.SaveSpot(sponsorSpot);

                // process the sub spots below
                Coordinate northEast = new Coordinate()
                { 
                    Latitude = sponsorSpot.SpotShape.Max(s => s.Latitude), 
                    Longitude = sponsorSpot.SpotShape.Max(s => s.Longitude)
                };
                Coordinate southWest = new Coordinate()
                {
                    Latitude = sponsorSpot.SpotShape.Min(s => s.Latitude),
                    Longitude = sponsorSpot.SpotShape.Min(s => s.Longitude)
                };

                // get the spots and update them
                var spots = Context.Spots.GetSpotsByRegion(northEast, southWest);
                bool success = Context.Spots.UpdateSponsor(spots, sponsorSpot.SponsorID);
            }

            ClearCart();

            return RedirectToAction("Edit", new { id = model.SponsorID });
        }