ISEbd-21_Chegodaev_A.Y._Law.../LawFirm/LawFirmFileImplement/Implements/LawStorage.cs
aleksandr chegodaev 093b43457b lab2
2024-04-13 02:05:00 +04:00

83 lines
2.5 KiB
C#

using LawFirmContracts.BindingModels;
using LawFirmContracts.SearchModels;
using LawFirmContracts.StoragesContracts;
using LawFirmContracts.ViewModels;
using LawFirmFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LawFirmFileImplement.Implements
{
public class LawStorage : ILawStorage
{
private readonly DataFileSingleton source;
public LawStorage()
{
source = DataFileSingleton.GetInstance();
}
public LawViewModel? GetElement(LawSearchModel model)
{
if (string.IsNullOrEmpty(model.LawName) && !model.Id.HasValue)
{
return null;
}
return source.Laws.FirstOrDefault(x => (!string.IsNullOrEmpty(model.LawName) && x.LawName == model.LawName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<LawViewModel> GetFilteredList(LawSearchModel model)
{
if (string.IsNullOrEmpty(model.LawName))
{
return new();
}
return source.Laws.Where(x => x.LawName.Contains(model.LawName)).Select(x => x.GetViewModel).ToList();
}
public List<LawViewModel> GetFullList()
{
return source.Laws.Select(x => x.GetViewModel).ToList();
}
public LawViewModel? Insert(LawBindingModel model)
{
model.Id = source.Laws.Count > 0 ? source.Laws.Max(x => x.Id) + 1 : 1;
var newDoc = Law.Create(model);
if (newDoc == null)
{
return null;
}
source.Laws.Add(newDoc);
source.SaveLaws();
return newDoc.GetViewModel;
}
public LawViewModel? Update(LawBindingModel model)
{
var document = source.Laws.FirstOrDefault(x => x.Id == model.Id);
if (document == null)
{
return null;
}
document.Update(model);
source.SaveLaws();
return document.GetViewModel;
}
public LawViewModel? Delete(LawBindingModel model)
{
var document = source.Laws.FirstOrDefault(x => x.Id == model.Id);
if (document == null)
{
return null;
}
document.Update(model);
source.SaveLaws();
return document.GetViewModel;
}
}
}