PIbd-21_MasenkinMS_Aircraft.../AircraftPlant/AircraftPlantFileImplement/Implements/PlaneStorage.cs
2024-02-25 01:22:22 +04:00

138 lines
4.1 KiB
C#

using AircraftPlantContracts.BindingModels;
using AircraftPlantContracts.SearchModels;
using AircraftPlantContracts.StoragesContracts;
using AircraftPlantContracts.ViewModels;
using AircraftPlantFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace AircraftPlantFileImplement.Implements
{
/// <summary>
/// Реализация интерфейса хранилища для изделий
/// </summary>
public class PlaneStorage : IPlaneStorage
{
/// <summary>
/// Хранилище
/// </summary>
private readonly DataFileSingleton _source;
/// <summary>
/// Конструктор
/// </summary>
public PlaneStorage()
{
_source = DataFileSingleton.GetInstance();
}
/// <summary>
/// Получение полного списка
/// </summary>
/// <returns></returns>
public List<PlaneViewModel> GetFullList()
{
return _source.Planes
.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();
}
return _source.Planes
.Where(x => x.PlaneName.Contains(model.PlaneName))
.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;
}
return _source.Planes
.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)
{
model.Id = _source.Planes.Count > 0 ? _source.Planes.Max(x => x.Id) + 1 : 1;
var newPlane = Plane.Create(model);
if (newPlane == null)
{
return null;
}
_source.Planes.Add(newPlane);
_source.SavePlanes();
return newPlane.GetViewModel;
}
/// <summary>
/// Редактирование элемента
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public PlaneViewModel? Update(PlaneBindingModel model)
{
var plane = _source.Planes.FirstOrDefault(x => x.Id == model.Id);
if (plane == null)
{
return null;
}
plane.Update(model);
_source.SavePlanes();
return plane.GetViewModel;
}
/// <summary>
/// Удаление элемента
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public PlaneViewModel? Delete(PlaneBindingModel model)
{
var element = _source.Planes.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
_source.Planes.Remove(element);
_source.SavePlanes();
return element.GetViewModel;
}
return null;
}
}
}