using GiftShopContracts.BindingModels; using GiftShopContracts.SearchModels; using GiftShopContracts.StoragesContracts; using GiftShopContracts.ViewModels; using GiftShopDatabaseImplement.Models; namespace GiftShopDatabaseImplement.Implements { public class ImplementerStorage : IImplementerStorage { public ImplementerViewModel? Delete(ImplementerBindingModel model) { using var context = new GiftShopDatabase(); var res = context.Implementers .FirstOrDefault(x => x.Id == model.Id); if (res != null) { context.Implementers.Remove(res); context.SaveChanges(); } return res?.GetViewModel; } public ImplementerViewModel? GetElement(ImplementerSearchModel model) { using var context = new GiftShopDatabase(); if (model.Id.HasValue) return context.Implementers .FirstOrDefault(x => x.Id == model.Id) ?.GetViewModel; if (model.ImplementerFIO != null && model.Password != null) return context.Implementers .FirstOrDefault(x => x.ImplementerFIO.Equals(model.ImplementerFIO) && x.Password.Equals(model.Password)) ?.GetViewModel; if (model.ImplementerFIO != null) return context.Implementers .FirstOrDefault(x => x.ImplementerFIO.Equals(model.ImplementerFIO)) ?.GetViewModel; return null; } public List GetFilteredList(ImplementerSearchModel model) { if (model == null) { return new(); } if (model.Id.HasValue) { var res = GetElement(model); return res != null ? new() { res } : new(); } if (model.ImplementerFIO != null) { using var context = new GiftShopDatabase(); return context.Implementers .Where(x => x.ImplementerFIO.Equals(model.ImplementerFIO)) .Select(x => x.GetViewModel) .ToList(); } return new(); } public List GetFullList() { using var context = new GiftShopDatabase(); return context.Implementers .Select(x => x.GetViewModel) .ToList(); } public ImplementerViewModel? Insert(ImplementerBindingModel model) { using var context = new GiftShopDatabase(); var res = Implementer.Create(model); if (res != null) { context.Implementers.Add(res); context.SaveChanges(); } return res?.GetViewModel; } public ImplementerViewModel? Update(ImplementerBindingModel model) { using var context = new GiftShopDatabase(); var res = context.Implementers .FirstOrDefault(x => x.Id == model.Id); if (res != null) { res.Update(model); context.SaveChanges(); } return res?.GetViewModel; } } }