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();
|
|
|
|
|
}
|
2024-02-28 15:49:54 +04:00
|
|
|
|
return source.Repairs.Where(x => x.RepairName.Contains(model.RepairName)).Select(x => x.GetViewModel).ToList();
|
2024-02-27 14:47:03 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RepairViewModel? GetElement(RepairSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(model.RepairName) && !model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2024-02-28 15:49:54 +04:00
|
|
|
|
return source.Repairs.FirstOrDefault(x =>(!string.IsNullOrEmpty(model.RepairName) && x.RepairName ==model.RepairName) ||(model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
|
2024-02-27 14:47:03 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|