using AircraftPlantContracts.BindingModels; using AircraftPlantContracts.SearchModels; using AircraftPlantContracts.StoragesContracts; using AircraftPlantContracts.ViewModels; using AircraftPlantDatabaseImplement.Models; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AircraftPlantDatabaseImplement.Implements { /// /// Реализация интерфейса хранилища для исполнителей /// public class ImplementerStorage : IImplementerStorage { /// /// Получение полного списка /// /// public List GetFullList() { using var context = new AircraftPlantDatabase(); return context.Implementers .Include(x => x.Orders) .Select(x => x.GetViewModel) .ToList(); } /// /// Получение фильтрованного списка /// /// /// public List GetFilteredList(ImplementerSearchModel model) { if (string.IsNullOrEmpty(model.ImplementerFIO)) { return new(); } using var context = new AircraftPlantDatabase(); return context.Implementers .Include(x => x.Orders) .Where(x => x.ImplementerFIO.Contains(model.ImplementerFIO)) .Select(x => x.GetViewModel) .ToList(); } /// /// Получение элемента /// /// /// public ImplementerViewModel? GetElement(ImplementerSearchModel model) { using var context = new AircraftPlantDatabase(); if (model.Id.HasValue) return context.Implementers .Include(x => x.Orders) .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)? .GetViewModel; else if (!string.IsNullOrEmpty(model.ImplementerFIO) && !string.IsNullOrEmpty(model.Password)) return context.Implementers.Include(x => x.Orders) .FirstOrDefault(x => x.ImplementerFIO == model.ImplementerFIO && x.Password == model.Password)? .GetViewModel; else if (!string.IsNullOrEmpty(model.ImplementerFIO)) return context.Implementers.Include(x => x.Orders) .FirstOrDefault(x => x.ImplementerFIO == model.ImplementerFIO)? .GetViewModel; return null; } /// /// Добавление элемента /// /// /// public ImplementerViewModel? Insert(ImplementerBindingModel model) { var newImplementer = Implementer.Create(model); if (newImplementer == null) { return null; } using var context = new AircraftPlantDatabase(); context.Implementers.Add(newImplementer); context.SaveChanges(); return newImplementer.GetViewModel; } /// /// Редактирование элемента /// /// /// public ImplementerViewModel? Update(ImplementerBindingModel model) { using var context = new AircraftPlantDatabase(); var implementer = context.Implementers.Include(x => x.Orders).FirstOrDefault(x => x.Id == model.Id); if (implementer == null) { return null; } implementer.Update(model); context.SaveChanges(); return implementer.GetViewModel; } /// /// Удаление элемента /// /// /// public ImplementerViewModel? Delete(ImplementerBindingModel model) { using var context = new AircraftPlantDatabase(); var element = context.Implementers.Include(x => x.Orders).FirstOrDefault(x => x.Id == model.Id); if (element != null) { context.Implementers.Remove(element); context.SaveChanges(); return element.GetViewModel; } return null; } } }