public static bool CheckPromoCode(string promoCode) { using (Bookshop context = new Bookshop()) { bool foundCode = context.CartPromo.Any(x => x.PromoCode == promoCode); bool promoDateValid = false; if (foundCode) { CartPromo cartPromo = context.CartPromo.First(x => x.PromoCode == promoCode); promoDateValid = cartPromo.ValidStart.AddDays(cartPromo.PromoDuration) >= DateTime.Today; } return(promoDateValid); } }
//Create promo code public static void CreatePromoCode(string promoCode, short discount, DateTime validStart, int promoDuration) { using (Bookshop context = new Bookshop()) { CartPromo cartPromo = new CartPromo { PromoCode = promoCode, Discount = discount, ValidStart = validStart, PromoDuration = promoDuration }; context.CartPromo.Add(cartPromo); context.SaveChanges(); } }
//Update promo code public static bool UpdatePromoCode(string promoCode, short discount, DateTime validStart, int promoDuration) { using (Bookshop context = new Bookshop()) { CartPromo cartPromo = context.CartPromo.Single(x => x.PromoCode == promoCode); if (cartPromo != null) { //cartPromo.PromoCode = promoCode; cartPromo.Discount = discount; cartPromo.ValidStart = validStart; cartPromo.PromoDuration = promoDuration; context.SaveChanges(); return(true); } else { return(false); } } }