PIbd-22-Stroev-V.M.-Plumbin.../PlumbingRepair/PlumbingRepairListImplement/Implements/WorkStorage.cs

127 lines
3.2 KiB
C#
Raw Permalink Normal View History

2024-02-27 22:51:12 +04:00
using PlumbingRepairContracts.BindingModels;
using PlumbingRepairContracts.SearchModels;
using PlumbingRepairContracts.StoragesContracts;
using PlumbingRepairContracts.ViewModels;
using PlumbingRepairListImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PlumbingRepairListImplement.Implements
{
public class WorkStorage : IWorkStorage
{
private readonly DataListSingleton _source;
public WorkStorage()
{
_source = DataListSingleton.GetInstance();
}
public List<WorkViewModel> GetFullList()
{
var result = new List<WorkViewModel>();
foreach (var work in _source.Works)
{
result.Add(work.GetViewModel);
}
return result;
}
public List<WorkViewModel> GetFilteredList(WorkSearchModel model)
{
var result = new List<WorkViewModel>();
if (string.IsNullOrEmpty(model.WorkName))
{
return result;
}
foreach (var work in _source.Works)
{
if (work.WorkName.Contains(model.WorkName))
{
result.Add(work.GetViewModel);
}
}
return result;
}
public WorkViewModel? GetElement(WorkSearchModel model)
{
if (string.IsNullOrEmpty(model.WorkName) && !model.Id.HasValue)
{
return null;
}
foreach (var work in _source.Works)
{
if ((!string.IsNullOrEmpty(model.WorkName) && work.WorkName == model.WorkName) || (model.Id.HasValue && work.Id == model.Id))
{
return work.GetViewModel;
}
}
return null;
}
public WorkViewModel? Insert(WorkBindingModel model)
{
model.Id = 1;
foreach (var work in _source.Works)
{
if (model.Id <= work.Id)
{
model.Id = work.Id + 1;
}
}
var newWork = Work.Create(model);
if (newWork == null)
{
return null;
}
_source.Works.Add(newWork);
return newWork.GetViewModel;
}
public WorkViewModel? Update(WorkBindingModel model)
{
foreach (var work in _source.Works)
{
if (work.Id == model.Id)
{
work.Update(model);
return work.GetViewModel;
}
}
return null;
}
public WorkViewModel? Delete(WorkBindingModel model)
{
for (int i = 0; i < _source.Works.Count; ++i)
{
if (_source.Works[i].Id == model.Id)
{
var element = _source.Works[i];
_source.Works.RemoveAt(i);
return element.GetViewModel;
}
}
return null;
}
}
}