73 lines
2.5 KiB
C#
73 lines
2.5 KiB
C#
using ConstructionFirmDatabaseImplement.Models;
|
|
using Subd_4.BindingModels;
|
|
using Subd_4.SearchModels;
|
|
using Subd_4.StoragesContracts;
|
|
using Subd_4.ViewModels;
|
|
|
|
namespace ConstructionFirmDatabaseImplement.Implements
|
|
{
|
|
public class ClientStorage : IClientStorage
|
|
{
|
|
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.Organization))
|
|
{
|
|
return new();
|
|
}
|
|
using var context = new ConstructionFirmDatabase();
|
|
return context.Clients.Where(x => x.Organization.Contains(model.Organization)).Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
public List<ClientViewModel> GetFullList()
|
|
{
|
|
using var context = new ConstructionFirmDatabase();
|
|
return context.Clients.Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
public ClientViewModel? Delete(ClientBindingModel model)
|
|
{
|
|
using var context = new ConstructionFirmDatabase();
|
|
var element = context.Clients.FirstOrDefault(x => x.Id == model.Id);
|
|
if (element != null)
|
|
{
|
|
context.Clients.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public ClientViewModel? GetElement(ClientSearchModel model)
|
|
{
|
|
using var context = new ConstructionFirmDatabase();
|
|
return context.Clients.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Organization)) && x.Organization == model.Organization || model.Id.HasValue && x.Id == model.Id)?.GetViewModel;
|
|
}
|
|
|
|
public ClientViewModel? Insert(ClientBindingModel model)
|
|
{
|
|
var newClient = Client.Create(model);
|
|
if (newClient == null)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new ConstructionFirmDatabase();
|
|
context.Clients.Add(newClient);
|
|
context.SaveChanges();
|
|
return newClient.GetViewModel;
|
|
}
|
|
|
|
public ClientViewModel? Update(ClientBindingModel model)
|
|
{
|
|
using var context = new ConstructionFirmDatabase();
|
|
var component = context.Clients.FirstOrDefault(x => x.Id == model.Id);
|
|
if (component == null)
|
|
{
|
|
return null;
|
|
}
|
|
component.Update(model);
|
|
context.SaveChanges();
|
|
return component.GetViewModel;
|
|
}
|
|
}
|
|
}
|