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

70 lines
2.4 KiB
C#
Raw Normal View History

2024-08-10 18:43:15 +04:00
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using ServiceStationContracts.BindingModels;
2024-04-30 18:44:37 +04:00
using ServiceStationContracts.SearchModels;
using ServiceStationContracts.ViewModels;
using ServiceStationsContracts.StorageContracts;
2024-04-30 20:55:37 +04:00
using ServiceStationsDataBaseImplement.Models;
2024-04-30 18:44:37 +04:00
namespace ServiceStationsDataBaseImplement.Implements
{
2024-08-10 18:43:15 +04:00
public class ClientStorage : IClientStorage {
2024-04-30 18:44:37 +04:00
2024-08-10 18:43:15 +04:00
public ClientViewModel? Insert(ClientBindingModel model) {
var newRec = Client.Create(model);
if (newRec == null) {
2024-04-30 20:55:37 +04:00
return null;
}
using var context = new Database();
2024-08-10 18:43:15 +04:00
context.Clients.Add(newRec);
context.SaveChanges();
return newRec.GetViewModel;
2024-04-30 18:44:37 +04:00
}
2024-08-10 18:43:15 +04:00
public ClientViewModel? Update(ClientBindingModel model) {
2024-04-30 20:55:37 +04:00
using var context = new Database();
2024-08-10 18:43:15 +04:00
var recUp = context.Clients.FirstOrDefault(x => x.Id == model.Id);
if (recUp == null) {
return null;
}
recUp.Update(model);
context.SaveChanges();
return recUp.GetViewModel;
2024-04-30 18:44:37 +04:00
}
2024-08-10 18:43:15 +04:00
public ClientViewModel? Delete(ClientBindingModel model) {
2024-04-30 20:55:37 +04:00
using var context = new Database();
2024-08-10 18:43:15 +04:00
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;
2024-04-30 18:44:37 +04:00
}
2024-08-10 18:43:15 +04:00
public ClientViewModel? GetElement(ClientSearchModel model) {
2024-04-30 20:55:37 +04:00
using var context = new Database();
2024-08-10 18:43:15 +04:00
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;
}
2024-04-30 18:44:37 +04:00
}
2024-08-10 18:43:15 +04:00
public List<ClientViewModel> GetFullList() {
2024-04-30 20:55:37 +04:00
using var context = new Database();
2024-08-10 18:43:15 +04:00
return context.Clients.Select(x => x.GetViewModel).ToList();
2024-04-30 18:44:37 +04:00
}
}
}