PIbd-21_MasenkinMS_Aircraft.../AircraftPlant/AircraftPlantDatabaseImplement/Implements/ImplementerStorage.cs

137 lines
4.8 KiB
C#
Raw Normal View History

2024-04-20 22:35:38 +04:00
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
{
/// <summary>
/// Реализация интерфейса хранилища для исполнителей
/// </summary>
public class ImplementerStorage : IImplementerStorage
{
/// <summary>
/// Получение полного списка
/// </summary>
/// <returns></returns>
public List<ImplementerViewModel> GetFullList()
{
using var context = new AircraftPlantDatabase();
return context.Implementers
.Include(x => x.Orders)
.Select(x => x.GetViewModel)
.ToList();
}
/// <summary>
/// Получение фильтрованного списка
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public List<ImplementerViewModel> 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();
}
/// <summary>
/// Получение элемента
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
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;
}
/// <summary>
/// Добавление элемента
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
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;
}
/// <summary>
/// Редактирование элемента
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
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;
}
/// <summary>
/// Удаление элемента
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
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;
}
}
}