137 lines
4.2 KiB
C#
137 lines
4.2 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.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace AircraftPlantFileImplement.Implements
|
|
{
|
|
/// <summary>
|
|
/// Реализация интерфейса хранилища для компонентов
|
|
/// </summary>
|
|
public class ComponentStorage : IComponentStorage
|
|
{
|
|
/// <summary>
|
|
/// Хранилище
|
|
/// </summary>
|
|
private readonly DataFileSingleton _source;
|
|
|
|
/// <summary>
|
|
/// Конструктор
|
|
/// </summary>
|
|
public ComponentStorage()
|
|
{
|
|
_source = DataFileSingleton.GetInstance();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Получение полного списка
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public List<ComponentViewModel> GetFullList()
|
|
{
|
|
return _source.Components
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Получение фильтрованного списка
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public List<ComponentViewModel> GetFilteredList(ComponentSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.ComponentName))
|
|
{
|
|
return new();
|
|
}
|
|
|
|
return _source.Components
|
|
.Where(x => x.ComponentName.Contains(model.ComponentName))
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Получение элемента
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public ComponentViewModel? GetElement(ComponentSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return _source.Components
|
|
.FirstOrDefault(x =>
|
|
(!string.IsNullOrEmpty(model.ComponentName) &&
|
|
x.ComponentName == model.ComponentName) ||
|
|
(model.Id.HasValue && x.Id == model.Id))
|
|
?.GetViewModel;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Добавление элемента
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public ComponentViewModel? Insert(ComponentBindingModel model)
|
|
{
|
|
model.Id = _source.Components.Count > 0 ? _source.Components.Max(x => x.Id) + 1 : 1;
|
|
|
|
var newComponent = Component.Create(model);
|
|
if (newComponent == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
_source.Components.Add(newComponent);
|
|
_source.SaveComponents();
|
|
return newComponent.GetViewModel;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Редактирование элемента
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public ComponentViewModel? Update(ComponentBindingModel model)
|
|
{
|
|
var component = _source.Components.FirstOrDefault(x => x.Id == model.Id);
|
|
if (component == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
component.Update(model);
|
|
_source.SaveComponents();
|
|
return component.GetViewModel;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Удаление элемента
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public ComponentViewModel? Delete(ComponentBindingModel model)
|
|
{
|
|
var element = _source.Components.FirstOrDefault(x => x.Id == model.Id);
|
|
if (element != null)
|
|
{
|
|
_source.Components.Remove(element);
|
|
_source.SaveComponents();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
}
|