2024-08-10 18:43:15 +04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using ServiceStationContracts.BindingModels;
|
2024-04-30 20:55:37 +04:00
|
|
|
|
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
|
|
|
|
|
{
|
2024-08-10 18:43:15 +04:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 20:55:37 +04:00
|
|
|
|
public ExecutorViewModel? Delete(ExecutorBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new Database();
|
2024-08-10 18:43:15 +04:00
|
|
|
|
var recDel = context.Executors
|
|
|
|
|
.Include(x => x.Works)
|
|
|
|
|
.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
|
if (recDel != null)
|
2024-04-30 20:55:37 +04:00
|
|
|
|
{
|
2024-08-10 18:43:15 +04:00
|
|
|
|
context.Executors.Remove(recDel);
|
2024-04-30 20:55:37 +04:00
|
|
|
|
context.SaveChanges();
|
2024-08-10 18:43:15 +04:00
|
|
|
|
return recDel.GetViewModel;
|
2024-04-30 20:55:37 +04:00
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ExecutorViewModel? GetElement(ExecutorSearchModel model)
|
|
|
|
|
{
|
2024-08-10 18:43:15 +04:00
|
|
|
|
using var context = new Database();
|
|
|
|
|
if (model.Id.HasValue) {
|
|
|
|
|
return context.Executors
|
|
|
|
|
.Include(x => x.Works)
|
|
|
|
|
.FirstOrDefault(x => x.Id == model.Id)
|
|
|
|
|
?.GetViewModel;
|
2024-04-30 20:55:37 +04:00
|
|
|
|
}
|
2024-08-10 18:43:15 +04:00
|
|
|
|
else {
|
|
|
|
|
return context.Executors
|
|
|
|
|
.Include (x => x.Works)
|
|
|
|
|
.FirstOrDefault(x => x.Email == model.Email)
|
|
|
|
|
?.GetViewModel;
|
2024-04-30 20:55:37 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ExecutorViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new Database();
|
|
|
|
|
return context.Executors.Select(x => x.GetViewModel).ToList();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|