PIbd-22_Shabunov_O.A._SushiBar/SushiBarDatabaseImplement/Storages/PromotionStorage.cs
2024-05-16 17:13:55 +04:00

81 lines
2.4 KiB
C#

using SushiBarContracts.BindingModels;
using SushiBarContracts.SearchModels;
using SushiBarContracts.ViewModels;
using SushiBarDatabaseImplement.Models;
namespace SushiBarDatabaseImplement.Storages
{
public class PromotionStorage
{
public List<PromotionViewModel> GetFullList()
{
using var Context = new SushiBarDatabase();
return Context.Promotions.Select(x => x.ViewModel).ToList();
}
public List<PromotionViewModel> 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;
}
}
}