using ServiceStationContracts.BindingModels; using ServiceStationContracts.SearchModels; using ServiceStationContracts.ViewModels; using ServiceStationsContracts.StorageContracts; using ServiceStationsDataBaseImplement.Models; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ServiceStationsDataBaseImplement.Implements { public class ReportStorage : IReportStorage { public ReportViewModel? Delete(ReportBindingModel model) { using var context = new Database(); var element = context.Reports.FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Reports.Remove(element); context.SaveChanges(); return element.GetViewModel; } return null; } public ReportViewModel? GetElement(ReportSearchModel model) { if (!model.Id.HasValue) { return null; } using var context = new Database(); return context.Reports.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; } public List GetFilteredList(ReportSearchModel model) { if (!model.Id.HasValue) { return new(); } using var context = new Database(); return context.Reports.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList(); } public List GetFullList() { using var context = new Database(); return context.Reports.Select(x => x.GetViewModel).ToList(); } public ReportViewModel? Insert(ReportBindingModel model) { var newComponent = Report.Create(model); if (newComponent == null) { return null; } using var context = new Database(); context.Reports.Add(newComponent); context.SaveChanges(); return newComponent.GetViewModel; } public ReportViewModel? Update(ReportBindingModel model) { using var context = new Database(); var component = context.Reports.FirstOrDefault(x => x.Id == model.Id); if (component == null) { return null; } component.Update(model); context.SaveChanges(); return component.GetViewModel; } } }