//Get discounted price of books public static decimal GetDiscountedPrice(int bookId) { using (Bookshop context = new Bookshop()) { CategoryDiscount discount = new CategoryDiscount(); Book book = context.Book.Single(x => x.BookID == bookId); List <CategoryDiscount> listDiscount = context.CategoryDiscount.Where(x => x.CategoryID == book.CategoryID).ToList(); foreach (CategoryDiscount dis in listDiscount) { if (dis.ValidStart.AddDays(dis.DiscountDuration) >= DateTime.Today) { discount = dis; break; } } if (discount != null) { return(book.Price * ((100 - discount.DiscountAmt) / 100m)); } else { return(book.Price); } } }
//Create discount based on category public static void CreateCategoryDiscount(string discountId, short discountAmt, DateTime validStart, int discountDuration, int categoryId) { using (Bookshop context = new Bookshop()) { CategoryDiscount promoPeriod = new CategoryDiscount { DiscountID = discountId, DiscountAmt = discountAmt, ValidStart = validStart, DiscountDuration = discountDuration, //CategoryID = categoryId, Category = context.Category.Single(x => x.CategoryID == categoryId) }; context.CategoryDiscount.Add(promoPeriod); context.SaveChanges(); } }
//Update category discount public static bool UpdateCategoryDiscount(string discountId, short discountAmt, DateTime validStart, int discountDuration, int categoryId) { using (Bookshop context = new Bookshop()) { CategoryDiscount promoPeriod = context.CategoryDiscount.Single(x => x.DiscountID == discountId); if (promoPeriod != null) { promoPeriod.DiscountAmt = discountAmt; promoPeriod.ValidStart = validStart; promoPeriod.DiscountDuration = discountDuration; promoPeriod.Category = context.Category.Single(x => x.CategoryID == categoryId); context.SaveChanges(); return(true); } else { return(false); } } }