using LawFirmContracts.BindingModels; using LawFirmContracts.SearchModels; using LawFirmContracts.StorageContracts; using LawFirmContracts.ViewModels; using LawFirmDatabaseImplement.Models; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LawFirmDatabaseImplement.Implements { public class ImplementerStorage : IImplementerStorage { public ImplementerViewModel? GetElement(ImplementerSearchModel model) { using var context = new LawFirmDatabase(); 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.ImplementerFIO != null) { using var context = new LawFirmDatabase(); return context.Implementers .Where(x => x.ImplementerFIO.Contains(model.ImplementerFIO)) .Select(x => x.GetViewModel) .ToList(); } return new(); } public List GetFullList() { using var context = new LawFirmDatabase(); return context.Implementers.Select(x => x.GetViewModel).ToList(); } public ImplementerViewModel? Insert(ImplementerBindingModel model) { using var context = new LawFirmDatabase(); 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 LawFirmDatabase(); var res = context.Implementers.FirstOrDefault(x => x.Id == model.Id); if (res != null) { res.Update(model); context.SaveChanges(); } return res?.GetViewModel; } public ImplementerViewModel? Delete(ImplementerBindingModel model) { using var context = new LawFirmDatabase(); var res = context.Implementers.FirstOrDefault(x => x.Id == model.Id); if (res != null) { context.Implementers.Remove(res); context.SaveChanges(); } return res?.GetViewModel; } } }