84 lines
2.7 KiB
C#
84 lines
2.7 KiB
C#
using SYBDContracts.BindingModels;
|
|
using SYBDContracts.SearchModels;
|
|
using SYBDContracts.StoragesContracts;
|
|
using SYBDContracts.ViewModels;
|
|
using SYBDDatabaseImplement.Models;
|
|
|
|
namespace SYBDDatabaseImplement.Implements
|
|
{
|
|
public class ClientStorage : IClientStorage
|
|
{
|
|
public List<ClientViewModel> GetFullList()
|
|
{
|
|
using var context = new SYBDDatabase();
|
|
return context.client
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.Fullname))
|
|
{
|
|
return new();
|
|
}
|
|
using var context = new SYBDDatabase();
|
|
return context.client
|
|
.Where(x => x.Fullname.Contains(model.Fullname))
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public ClientViewModel? GetElement(ClientSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.Fullname) && !model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new SYBDDatabase();
|
|
return context.client
|
|
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Fullname) && x.Fullname == model.Fullname) ||
|
|
(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 SYBDDatabase();
|
|
context.client.Add(newClient);
|
|
context.SaveChanges();
|
|
return newClient.GetViewModel;
|
|
}
|
|
|
|
public ClientViewModel? Update(ClientBindingModel model)
|
|
{
|
|
using var context = new SYBDDatabase();
|
|
var component = context.client.FirstOrDefault(x => x.Id == model.Id);
|
|
if (component == null)
|
|
{
|
|
return null;
|
|
}
|
|
component.Update(model);
|
|
context.SaveChanges();
|
|
return component.GetViewModel;
|
|
}
|
|
|
|
public ClientViewModel? Delete(ClientBindingModel model)
|
|
{
|
|
using var context = new SYBDDatabase();
|
|
var element = context.client.FirstOrDefault(rec => rec.Id == model.Id);
|
|
if (element != null)
|
|
{
|
|
context.client.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
} |