PIbd-23-Yunusov-N.N.-CarRep.../CarRepairShop/CarRepairShopFileImplement/Implements/RepairStorage.cs

86 lines
2.6 KiB
C#
Raw Normal View History

2024-02-27 14:47:03 +04:00
using CarRepairShopContracts.BindingModels;
using CarRepairShopContracts.SearchModels;
using CarRepairShopContracts.StoragesContracts;
using CarRepairShopContracts.ViewModels;
using CarRepairShopFileImplement.Models;
namespace CarRepairShopFileImplement.Implements
{
public class RepairStorage : IRepairStorage
{
private readonly DataFileSingleton source;
public RepairStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<RepairViewModel> GetFullList()
{
return source.Repairs
.Select(x => x.GetViewModel)
.ToList();
}
public List<RepairViewModel> GetFilteredList(RepairSearchModel model)
{
if (string.IsNullOrEmpty(model.RepairName))
{
return new();
}
return source.Repairs
.Where(x => x.RepairName.Contains(model.RepairName))
.Select(x => x.GetViewModel)
.ToList();
}
public RepairViewModel? GetElement(RepairSearchModel model)
{
if (string.IsNullOrEmpty(model.RepairName) && !model.Id.HasValue)
{
return null;
}
return source.Repairs
.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.RepairName) && x.RepairName ==
model.RepairName) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public RepairViewModel? Insert(RepairBindingModel model)
{
model.Id = source.Repairs.Count > 0 ? source.Repairs.Max(x => x.Id) + 1 : 1;
var newRepair = Repair.Create(model);
if (newRepair == null)
{
return null;
}
source.Repairs.Add(newRepair);
source.SaveRepairs();
return newRepair.GetViewModel;
}
public RepairViewModel? Update(RepairBindingModel model)
{
var repair = source.Repairs.FirstOrDefault(x => x.Id == model.Id);
if (repair == null)
{
return null;
}
repair.Update(model);
source.SaveRepairs();
return repair.GetViewModel;
}
public RepairViewModel? Delete(RepairBindingModel model)
{
var repair = source.Repairs.FirstOrDefault(x => x.Id == model.Id);
if (repair != null)
{
source.Repairs.Remove(repair);
source.SaveRepairs();
return repair.GetViewModel;
}
return null;
}
}
}