149 lines
4.8 KiB
C#
149 lines
4.8 KiB
C#
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 PlaneStorage : IPlaneStorage
|
|
{
|
|
/// <summary>
|
|
/// Получение полного списка
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public List<PlaneViewModel> GetFullList()
|
|
{
|
|
using var context = new AircraftPlantDatabase();
|
|
return context.Planes
|
|
.Include(x => x.Components)
|
|
.ThenInclude(x => x.Component)
|
|
.ToList()
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Получение фильтрованного списка
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public List<PlaneViewModel> GetFilteredList(PlaneSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.PlaneName))
|
|
{
|
|
return new();
|
|
}
|
|
|
|
using var context = new AircraftPlantDatabase();
|
|
return context.Planes
|
|
.Include(x => x.Components)
|
|
.ThenInclude(x => x.Component)
|
|
.Where(x => x.PlaneName.Contains(model.PlaneName))
|
|
.ToList()
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Получение элемента
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public PlaneViewModel? GetElement(PlaneSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.PlaneName) && !model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
using var context = new AircraftPlantDatabase();
|
|
return context.Planes
|
|
.Include(x => x.Components)
|
|
.ThenInclude(x => x.Component)
|
|
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.PlaneName) &&
|
|
x.PlaneName == model.PlaneName) ||
|
|
(model.Id.HasValue && x.Id == model.Id))
|
|
?.GetViewModel;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Добавление элемента
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public PlaneViewModel? Insert(PlaneBindingModel model)
|
|
{
|
|
using var context = new AircraftPlantDatabase();
|
|
var newPlane = Plane.Create(context, model);
|
|
if (newPlane == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
context.Planes.Add(newPlane);
|
|
context.SaveChanges();
|
|
return newPlane.GetViewModel;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Редактирование элемента
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public PlaneViewModel? Update(PlaneBindingModel model)
|
|
{
|
|
using var context = new AircraftPlantDatabase();
|
|
using var transaction = context.Database.BeginTransaction();
|
|
try
|
|
{
|
|
var plane = context.Planes.FirstOrDefault(rec => rec.Id == model.Id);
|
|
if (plane == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
plane.Update(model);
|
|
context.SaveChanges();
|
|
plane.UpdateComponents(context, model);
|
|
transaction.Commit();
|
|
return plane.GetViewModel;
|
|
}
|
|
catch
|
|
{
|
|
transaction.Rollback();
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Удаление элемента
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public PlaneViewModel? Delete(PlaneBindingModel model)
|
|
{
|
|
using var context = new AircraftPlantDatabase();
|
|
var element = context.Planes
|
|
.Include(x => x.Components)
|
|
.FirstOrDefault(rec => rec.Id == model.Id);
|
|
if (element != null)
|
|
{
|
|
context.Planes.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
}
|