103 lines
3.0 KiB
C#
Raw Normal View History

2024-05-24 12:23:50 +04:00
using FurnitureAssemblyContracts.BindingModels;
using FurnitureAssemblyContracts.SearchModels;
using FurnitureAssemblyContracts.StoragesContracts;
using FurnitureAssemblyContracts.ViewModels;
using FurnitureAssemblyDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FurnitureAssemblyDatabaseImplement.Implements
{
2024-05-24 13:05:47 +04:00
public class KommentStorage : IKommentStorage
2024-05-24 12:23:50 +04:00
{
2024-05-24 13:05:47 +04:00
public List<KommentViewModel> GetFullList()
2024-05-24 12:23:50 +04:00
{
using var context = new FurnitureAssemblyDatabase();
2024-05-24 14:23:37 +04:00
return context.Komments
2024-05-24 12:23:50 +04:00
.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();
}
using var context = new FurnitureAssemblyDatabase();
2024-05-24 14:23:37 +04:00
return context.Komments
2024-05-24 12:23:50 +04:00
.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;
}
using var context = new FurnitureAssemblyDatabase();
2024-05-24 14:23:37 +04:00
return context.Komments
2024-05-24 12:23:50 +04:00
.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
{
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;
}
using var context = new FurnitureAssemblyDatabase();
2024-05-24 14:23:37 +04:00
context.Komments.Add(newWorkPiece);
2024-05-24 12:23:50 +04:00
context.SaveChanges();
return newWorkPiece.GetViewModel;
}
2024-05-24 13:05:47 +04:00
public KommentViewModel? Update(KommentBindingModel model)
2024-05-24 12:23:50 +04:00
{
using var context = new FurnitureAssemblyDatabase();
2024-05-24 14:23:37 +04:00
var workPiece = context.Komments.FirstOrDefault(x => x.Id == model.Id);
2024-05-24 12:23:50 +04:00
if (workPiece == null)
{
return null;
}
workPiece.Update(model);
context.SaveChanges();
return workPiece.GetViewModel;
}
2024-05-24 13:05:47 +04:00
public KommentViewModel? Delete(KommentBindingModel model)
2024-05-24 12:23:50 +04:00
{
using var context = new FurnitureAssemblyDatabase();
2024-05-24 14:23:37 +04:00
var element = context.Komments.FirstOrDefault(rec => rec.Id == model.Id);
2024-05-24 12:23:50 +04:00
if (element != null)
{
2024-05-24 14:23:37 +04:00
context.Komments.Remove(element);
2024-05-24 12:23:50 +04:00
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}