SUBD_Gerimovich_Ilya_PIbd-22/FurnitureAssembly/FurnitureAssemblyFileImplement/Implements/KommentStorage.cs

97 lines
2.8 KiB
C#
Raw Permalink Normal View History

2024-05-24 12:23:50 +04:00
using FurnitureAssemblyContracts.BindingModels;
using FurnitureAssemblyContracts.SearchModels;
using FurnitureAssemblyContracts.StoragesContracts;
using FurnitureAssemblyContracts.ViewModels;
using FurnitureAssemblyFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FurnitureAssemblyFileImplement.Implements
{
2024-05-24 13:05:47 +04:00
public class KommentStorage : IKommentStorage
2024-05-24 12:23:50 +04:00
{
private readonly DataFileSingleton source;
2024-05-24 13:05:47 +04:00
public KommentStorage()
2024-05-24 12:23:50 +04:00
{
source = DataFileSingleton.GetInstance();
}
2024-05-24 13:05:47 +04:00
public List<KommentViewModel> GetFullList()
2024-05-24 12:23:50 +04:00
{
return source.WorkPieces.Select(x => x.GetViewModel).ToList();
}
2024-05-24 13:05:47 +04:00
public List<KommentViewModel> GetFilteredList(KommentSearchModel model)
2024-05-24 12:23:50 +04:00
{
if (string.IsNullOrEmpty(model.WorkPieceName))
{
return new();
}
return source.WorkPieces.Where(x => x.WorkPieceName.Contains(model.WorkPieceName)).Select(x => x.GetViewModel).ToList();
}
2024-05-24 13:05:47 +04:00
public KommentViewModel? GetElement(KommentSearchModel model)
2024-05-24 12:23:50 +04:00
{
if (string.IsNullOrEmpty(model.WorkPieceName) && !model.Id.HasValue)
{
return null;
}
return source.WorkPieces.FirstOrDefault(x => (!string.IsNullOrEmpty(model.WorkPieceName) && x.WorkPieceName == model.WorkPieceName)
|| (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
2024-05-24 13:05:47 +04:00
public KommentViewModel? Insert(KommentBindingModel model)
2024-05-24 12:23:50 +04:00
{
model.Id = source.WorkPieces.Count > 0 ? source.WorkPieces.Max(x => x.Id) + 1 : 1;
2024-05-24 13:05:47 +04:00
var newWorkPiece = Komment.Create(model);
2024-05-24 12:23:50 +04:00
if (newWorkPiece == null)
{
return null;
}
source.WorkPieces.Add(newWorkPiece);
source.SaveWorkPieces();
return newWorkPiece.GetViewModel;
}
2024-05-24 13:05:47 +04:00
public KommentViewModel? Update(KommentBindingModel model)
2024-05-24 12:23:50 +04:00
{
var workPiece = source.WorkPieces.FirstOrDefault(x => x.Id == model.Id);
if (workPiece == null)
{
return null;
}
workPiece.Update(model);
source.SaveWorkPieces();
return workPiece.GetViewModel;
}
2024-05-24 13:05:47 +04:00
public KommentViewModel? Delete(KommentBindingModel model)
2024-05-24 12:23:50 +04:00
{
var element = source.WorkPieces.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
source.WorkPieces.Remove(element);
source.SaveWorkPieces();
return element.GetViewModel;
}
return null;
}
}
}