Gismatullin.ISEbd-21.STO.Co.../ServiceStation/ServiceStationsDataBaseImplement/Implements/ReportStorage.cs
2024-08-03 19:12:18 +04:00

83 lines
2.6 KiB
C#

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<ReportViewModel> 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<ReportViewModel> 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;
}
}
}