PIbd-22_Kamcharova_K.A_Reno.../RenovationWorkFileImplement/Implements/RepairStorage.cs
2024-04-17 09:25:01 +04:00

91 lines
2.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RenovationWorkContracts.BindingModels;
using RenovationWorkContracts.SearchModels;
using RenovationWorkContracts.StoragesContracts;
using RenovationWorkContracts.ViewModels;
using RenovationWorkFileImplement.Models;
namespace RenovationWorkFileImplement.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;
}
}
}