using SushiBarContracts.BindingModels; using SushiBarContracts.SearchModels; using SushiBarContracts.ViewModels; using SushiBarDatabaseImplement.Models; namespace SushiBarDatabaseImplement.Storages { public class PromotionStorage { public List GetFullList() { using var Context = new SushiBarDatabase(); return Context.Promotions.Select(x => x.ViewModel).ToList(); } public List GetFilteredList(PromotionSearchModel Model) { if (!Model.TriggeringSum.HasValue) return new(); using var Context = new SushiBarDatabase(); return Context.Promotions .Where(x => x.TriggeringSum >= Model.TriggeringSum) .Select(x => x.ViewModel) .ToList(); } public PromotionViewModel? GetElement(PromotionSearchModel Model) { if (!Model.Id.HasValue) return null; using var Context = new SushiBarDatabase(); return Context.Promotions .FirstOrDefault(x => x.Id == Model.Id)?.ViewModel; } public PromotionViewModel? Insert(PromotionBindingModel Model) { var NewPromotion = Promotion.Create(Model); if (NewPromotion == null) return null; using var Context = new SushiBarDatabase(); Context.Promotions.Add(NewPromotion); Context.SaveChanges(); return NewPromotion.ViewModel; } public PromotionViewModel? Update(PromotionBindingModel Model) { using var Context = new SushiBarDatabase(); var Promotion = Context.Promotions.FirstOrDefault(x => x.Id == Model.Id); if (Promotion == null) return null; Promotion.Update(Model); Context.SaveChanges(); return Promotion.ViewModel; } public PromotionViewModel? Delete(PromotionBindingModel Model) { using var Context = new SushiBarDatabase(); var Promotion = Context.Promotions.FirstOrDefault(rec => rec.Id == Model.Id); if (Promotion == null) return null; Context.Promotions.Remove(Promotion); Context.SaveChanges(); return Promotion.ViewModel; } } }