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