Beispiel #1
0
        /// <summary>
        /// Creates new shopping cart and all items related to that cart
        /// </summary>
        private void CreateCart(string userId, List<BitCoupon.API.Models.Item> items, double total)
        {
            Cart cart = new Cart() { ApplicationUserId = userId, Purchased = "Not Payed", PaymentId = "", TimeOfPurchase = DateTime.Now, TotalPrice = total };
            db.Carts.Add(cart);
            db.SaveChanges();


            var Dbitems = db.Items.ToList();
            // generates verification id 

            for (int i = 0; i < items.Count; i++)
            {
                BitCoupon.DAL.Models.Item item = new BitCoupon.DAL.Models.Item() { CartId = cart.Id, CouponId = Int32.Parse(items[i].id), Quantity = items[i].quantity, VerificationId = LogicCoupons.GenerateVerificationId(Dbitems), TimeOfPurchase = DateTime.Now };
                db.Items.Add(item);
                db.SaveChanges();
            }
        }
Beispiel #2
0
        public ActionResult Buy(string couponId, string quantity, string userId)
        {
            var coupon = db.Coupons.Find(Int32.Parse(couponId));

            coupon.TotalNumberOfCoupons -= coupon.RequiredNumberOfCoupons * Int32.Parse(quantity);  //reduce amount of coupons bought
            if (coupon.TotalNumberOfCoupons < 0)
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            coupon.Purchase++;  //increse purchase coupon

            if (coupon.NuberOfCouponsToOfferManaged > 0)
                coupon.NuberOfCouponsToOfferManaged -= coupon.RequiredNumberOfCoupons;

            Cart cart = new Cart() { ApplicationUserId = userId, Purchased = "Payed", PaymentId = "", TimeOfPurchase = DateTime.Now, TotalPrice = coupon.Price };

            db.Entry(coupon).State = EntityState.Modified;
            db.Carts.Add(cart);
            db.SaveChanges();

            var items = db.Items.ToList();

            Item item = new Item() { CartId = cart.Id, CouponId = coupon.CouponId, Quantity = Int32.Parse(quantity), VerificationId = LogicCoupons.GenerateVerificationId(new List<Item>(items)), TimeOfPurchase = DateTime.Now};
            db.Items.Add(item);
            db.SaveChanges();
            if (coupon.TotalNumberOfCoupons > 0)  //if there is still coupons to buy return OK
                return new HttpStatusCodeResult(HttpStatusCode.OK);

            coupon.Acitve = false;  //else deactivate coupon
            db.Entry(coupon).State = EntityState.Modified;
            db.SaveChanges();
            return new HttpStatusCodeResult(HttpStatusCode.OK);
        }