PIbd-22_Safiulova_K.N._Airc.../AircraftPlant/AircraftPlantFileImplement/PlaneStorage.cs

83 lines
2.7 KiB
C#
Raw Permalink Normal View History

2024-03-13 00:15:40 +04:00
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
{
public class PlaneStorage : IPlaneStorage
{
private readonly DataFileSingleton _source;
public PlaneStorage()
{
_source = DataFileSingleton.GetInstance();
}
public List<PlaneViewModel> GetFullList()
{
return _source.Planes.Select(x => x.GetViewModel).ToList();
}
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();
}
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;
}
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;
}
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;
}
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;
}
}
}