Gismatullin.ISEbd-21.STO.Co.../ServiceStation/ServiceStationsDataBaseImplement/Implements/ExecutorStorage.cs

79 lines
2.5 KiB
C#

using Microsoft.EntityFrameworkCore;
using ServiceStationContracts.BindingModels;
using ServiceStationContracts.SearchModels;
using ServiceStationContracts.ViewModels;
using ServiceStationDataModels;
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 ExecutorStorage : IExecutorStorage
{
public ExecutorViewModel? Insert(ExecutorBindingModel model) {
var newRec = Executor.Create(model);
if (newRec == null) {
return null;
}
using var context = new Database();
context.Executors.Add(newRec);
context.SaveChanges();
return newRec.GetViewModel;
}
public ExecutorViewModel? Update(ExecutorBindingModel model) {
using var context = new Database();
var recUp = context.Executors.FirstOrDefault(x => x.Id == model.Id);
if (recUp == null) {
return null;
}
recUp.Update(model);
context.SaveChanges();
return recUp.GetViewModel;
}
public ExecutorViewModel? Delete(ExecutorBindingModel model)
{
using var context = new Database();
var recDel = context.Executors
.Include(x => x.Works)
.FirstOrDefault(rec => rec.Id == model.Id);
if (recDel != null)
{
context.Executors.Remove(recDel);
context.SaveChanges();
return recDel.GetViewModel;
}
return null;
}
public ExecutorViewModel? GetElement(ExecutorSearchModel model)
{
using var context = new Database();
if (model.Id.HasValue) {
return context.Executors
.Include(x => x.Works)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
}
else {
return context.Executors
.Include (x => x.Works)
.FirstOrDefault(x => x.Email == model.Email)
?.GetViewModel;
}
}
public List<ExecutorViewModel> GetFullList()
{
using var context = new Database();
return context.Executors.Select(x => x.GetViewModel).ToList();
}
}
}