70 lines
2.4 KiB
C#
70 lines
2.4 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Metadata;
|
|
using ServiceStationContracts.BindingModels;
|
|
using ServiceStationContracts.SearchModels;
|
|
using ServiceStationContracts.ViewModels;
|
|
using ServiceStationsContracts.StorageContracts;
|
|
using ServiceStationsDataBaseImplement.Models;
|
|
|
|
namespace ServiceStationsDataBaseImplement.Implements
|
|
{
|
|
public class ClientStorage : IClientStorage {
|
|
|
|
public ClientViewModel? Insert(ClientBindingModel model) {
|
|
var newRec = Client.Create(model);
|
|
if (newRec == null) {
|
|
return null;
|
|
}
|
|
using var context = new Database();
|
|
context.Clients.Add(newRec);
|
|
context.SaveChanges();
|
|
return newRec.GetViewModel;
|
|
}
|
|
|
|
public ClientViewModel? Update(ClientBindingModel model) {
|
|
using var context = new Database();
|
|
var recUp = context.Clients.FirstOrDefault(x => x.Id == model.Id);
|
|
if (recUp == null) {
|
|
return null;
|
|
}
|
|
recUp.Update(model);
|
|
context.SaveChanges();
|
|
return recUp.GetViewModel;
|
|
}
|
|
|
|
public ClientViewModel? Delete(ClientBindingModel model) {
|
|
using var context = new Database();
|
|
var recDel = context.Clients
|
|
.Include(x => x.WorkClients)
|
|
.FirstOrDefault(x => x.Id == model.Id);
|
|
if (recDel != null) {
|
|
context.Clients.Remove(recDel);
|
|
context.SaveChanges();
|
|
return recDel.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public ClientViewModel? GetElement(ClientSearchModel model) {
|
|
using var context = new Database();
|
|
if (model.Id.HasValue) {
|
|
return context.Clients
|
|
.Include(x => x.WorkClients)
|
|
.FirstOrDefault(x => x.Id == model.Id)
|
|
?.GetViewModel;
|
|
}
|
|
else {
|
|
return context.Clients
|
|
.Include(x => x.WorkClients)
|
|
.FirstOrDefault(x => x.Login == model.Login)
|
|
?.GetViewModel;
|
|
}
|
|
}
|
|
|
|
public List<ClientViewModel> GetFullList() {
|
|
using var context = new Database();
|
|
return context.Clients.Select(x => x.GetViewModel).ToList();
|
|
}
|
|
}
|
|
}
|