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 { public class KommentStorage : IKommentStorage { public List GetFullList() { using var context = new FurnitureAssemblyDatabase(); return context.Komments .Select(x => x.GetViewModel) .ToList(); } public List GetFilteredList(KommentSearchModel model) { if (string.IsNullOrEmpty(model.WorkPieceName)) { return new(); } using var context = new FurnitureAssemblyDatabase(); return context.Komments .Where(x => x.WorkPieceName.Contains(model.WorkPieceName)) .Select(x => x.GetViewModel) .ToList(); } public KommentViewModel? GetElement(KommentSearchModel model) { if (string.IsNullOrEmpty(model.WorkPieceName) && !model.Id.HasValue) { return null; } using var context = new FurnitureAssemblyDatabase(); return context.Komments .FirstOrDefault(x => (!string.IsNullOrEmpty(model.WorkPieceName) && x.WorkPieceName == model.WorkPieceName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; } public KommentViewModel? Insert(KommentBindingModel model) { var newWorkPiece = Komment.Create(model); if (newWorkPiece == null) { return null; } using var context = new FurnitureAssemblyDatabase(); context.Komments.Add(newWorkPiece); context.SaveChanges(); return newWorkPiece.GetViewModel; } public KommentViewModel? Update(KommentBindingModel model) { using var context = new FurnitureAssemblyDatabase(); var workPiece = context.Komments.FirstOrDefault(x => x.Id == model.Id); if (workPiece == null) { return null; } workPiece.Update(model); context.SaveChanges(); return workPiece.GetViewModel; } public KommentViewModel? Delete(KommentBindingModel model) { using var context = new FurnitureAssemblyDatabase(); var element = context.Komments.FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Komments.Remove(element); context.SaveChanges(); return element.GetViewModel; } return null; } } }