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

85 lines
2.8 KiB
C#
Raw Normal View History

2024-04-30 20:55:37 +04:00
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? Delete(ExecutorBindingModel model)
{
using var context = new Database();
var element = context.Executors.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Executors.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public ExecutorViewModel? GetElement(ExecutorSearchModel model)
{
if (string.IsNullOrEmpty(model.Email) && !model.Id.HasValue)
{
return null;
}
using var context = new Database();
return context.Executors.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.Email) && x.Email == model.Email ||
(model.Id.HasValue && x.Id == model.Id)))?.GetViewModel;
}
public List<ExecutorViewModel> GetFilteredList(ExecutorSearchModel model)
{
if (string.IsNullOrEmpty(model.Email))
{
return new();
}
using var context = new Database();
return context.Executors.Where(x => x.Email.Contains(model.Email)).Select(x => x.GetViewModel).ToList();
}
public List<ExecutorViewModel> GetFullList()
{
using var context = new Database();
return context.Executors.Select(x => x.GetViewModel).ToList();
}
public ExecutorViewModel? Insert(ExecutorBindingModel model)
{
var newComponent = Executor.Create(model);
if (newComponent == null)
{
return null;
}
using var context = new Database();
context.Executors.Add(newComponent);
context.SaveChanges();
return newComponent.GetViewModel;
}
public ExecutorViewModel? Update(ExecutorBindingModel model)
{
using var context = new Database();
var component = context.Executors.FirstOrDefault(x => x.Id == model.Id);
if (component == null)
{
return null;
}
component.Update(model);
context.SaveChanges();
return component.GetViewModel;
}
}
}