public PromoCode SavePromo(PromoCode promo)
        {
            using (PromoRepository db = new PromoRepository())
            {
                if (promo.Id == Guid.Empty)
                {
                    db.PromoCodes.Add(promo);
                }
                else
                {
                    db.PromoCodes.Attach(promo);
                    db.Entry(promo).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                }
                db.SaveChanges();
            }

            return(promo);
        }
示例#2
0
        public User SaveUser(User user)
        {
            using (PromoRepository db = new PromoRepository())
            {
                if (user.Id == Guid.Empty)
                {
                    db.Users.Add(user);
                }
                else
                {
                    db.Users.Attach(user);
                    db.Entry(user).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                }
                db.SaveChanges();
            }

            return(user);
        }
示例#3
0
        public Cart RedeemPromo(string code, Cart cart)
        {
            using (PromoRepository db = new PromoRepository())
            {
                var dbPromo = db.PromoCodes.Include(_promo => _promo.Product).FirstOrDefault(_dbPromo => _dbPromo.Code == code);

                if (dbPromo != null)
                {
                    DateTime now = DateTime.UtcNow;
                    if (now < dbPromo.StartDate)
                    {
                        cart.PromoErrors.Add(new PromoError()
                        {
                            ErrorCode = PromoErrorType.NotStarted,
                            Message   = $"Promo starts on {dbPromo.StartDate}"
                        });
                    }
                    if (dbPromo.EndDate < now)
                    {
                        cart.PromoErrors.Add(new PromoError()
                        {
                            ErrorCode = PromoErrorType.Expired,
                            Message   = $"Promo expired"
                        });
                    }
                    if (dbPromo.RedemptionCount >= dbPromo.MaxRedemptionCount)
                    {
                        cart.PromoErrors.Add(new PromoError()
                        {
                            ErrorCode = PromoErrorType.MaxRedemptions,
                            Message   = $"Promo expired"
                        });
                    }

                    if (cart.Products.Select(_product => _product.Id).Contains(dbPromo.Id) == false)
                    {
                        cart.PromoErrors.Add(new PromoError()
                        {
                            ErrorCode = PromoErrorType.NoRelatedProduct,
                            Message   = "Promo does not apply to any items in the cart"
                        });
                    }

                    dbPromo.RedemptionCount += 1;
                    db.SaveChanges();

                    // apply promo
                    cart.PromoCodes.Add(dbPromo);
                }
                else
                {
                    cart.PromoErrors.Add(new PromoError()
                    {
                        ErrorCode = PromoErrorType.InvalidPromo,
                        Message   = $"Promo {code} doesn't exist"
                    });
                }

                return(cart);
            }
        }
示例#4
0
        public Cart SaveCart(Cart cart)
        {
            using (PromoRepository db = new PromoRepository())
            {
                var productCache = cart.Products.ToList();
                var promoCache   = cart.PromoCodes.ToList();
                cart.Products   = new List <Product>();
                cart.PromoCodes = new List <PromoCode>();

                if (cart.User.Id == Guid.Empty)
                {
                    db.Users.Add(cart.User);
                }
                else
                {
                    db.Users.Attach(cart.User);
                    db.Entry(cart.User).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                }


                if (cart.Id == Guid.Empty)
                {
                    db.Carts.Add(cart);
                }
                else
                {
                    db.Carts.Attach(cart);
                    db.Entry(cart).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                }

                db.SaveChanges();

                var existingProductRelations = db.CartProducts.Where(cp => cp.CartId == cart.Id);
                var existingProductIds       = existingProductRelations.Select(cp => cp.ProductId);
                var requestedProductIds      = productCache.Select(product => product.Id);

                var toAddProductIds    = requestedProductIds.Except(existingProductIds);
                var toRemoveProductIds = existingProductIds.Except(requestedProductIds);

                var toAddCartProductModels = toAddProductIds.Select(productId => new CartProduct()
                {
                    CartId = cart.Id, ProductId = productId
                });
                db.CartProducts.AddRange(toAddCartProductModels);

                foreach (var toRemoveId in toRemoveProductIds)
                {
                    db.CartProducts.Remove(db.CartProducts.First(cp => cp.CartId == cart.Id && cp.ProductId == toRemoveId));
                }

                var existingPromoRelations = db.CartPromos.Where(cp => cp.CartId == cart.Id);
                var existingPromoIds       = existingPromoRelations.Select(cp => cp.PromoCodeId);
                var requestedPromoIds      = promoCache.Select(product => product.Id);

                var toAddPromoIds    = requestedPromoIds.Except(existingPromoIds);
                var toRemovePromoIds = existingPromoIds.Except(requestedPromoIds);

                var toAddCartPromoModels = toAddPromoIds.Select(promoId => new CartPromo()
                {
                    CartId = cart.Id, PromoCodeId = promoId
                });
                db.CartPromos.AddRange(toAddCartPromoModels);

                foreach (var toRemoveId in toRemoveProductIds)
                {
                    db.CartPromos.Remove(db.CartPromos.First(cp => cp.CartId == cart.Id && cp.PromoCodeId == toRemoveId));
                }

                db.SaveChanges();
                cart.Products   = productCache;
                cart.PromoCodes = promoCache;
            }

            return(cart);
        }