2024-05-26 13:34:14 +04:00
|
|
|
|
using SewingDressesContracts.BindingModels;
|
|
|
|
|
using SewingDressesContracts.SearchModels;
|
|
|
|
|
using SewingDressesContracts.StoragesContracts;
|
|
|
|
|
using SewingDressesContracts.ViewModels;
|
|
|
|
|
using SewingDressesDatabaseImplement.Models;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace SewingDressesDatabaseImplement.Implements
|
|
|
|
|
{
|
|
|
|
|
public class ClientStorage : IClientStorage
|
|
|
|
|
{
|
|
|
|
|
public ClientViewModel? Insert(ClientBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new SewingDressesDatabase();
|
|
|
|
|
var newClient = Client.Create(model);
|
|
|
|
|
if (newClient == null) { return null; }
|
|
|
|
|
context.Clients.Add(newClient);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return newClient.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ClientViewModel? Update(ClientBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new SewingDressesDatabase();
|
|
|
|
|
var client = context.Clients.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (client == null) { return null; };
|
|
|
|
|
client.Update(model);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return client.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ClientViewModel? Delete(ClientBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new SewingDressesDatabase();
|
|
|
|
|
var client = context.Clients.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (client == null) { return null; }
|
|
|
|
|
context.Clients.Remove(client);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return client.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ClientViewModel? GetElement(ClientSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(model.Email) && !model.Id.HasValue) { return null; }
|
|
|
|
|
using var context = new SewingDressesDatabase();
|
2024-06-22 20:56:02 +04:00
|
|
|
|
return context.Clients.FirstOrDefault(x => (!(string.IsNullOrEmpty(model.Email)) && model.Email.Equals( x.Email)) || (model.Id.HasValue && model.Id == x.Id))?.GetViewModel;
|
2024-05-26 13:34:14 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(model.Email)) { return new(); }
|
|
|
|
|
using var context = new SewingDressesDatabase();
|
|
|
|
|
return context.Clients.Where(x => x.Email.Equals(model.Email)).Select(x => x.GetViewModel).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ClientViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new SewingDressesDatabase();
|
|
|
|
|
return context.Clients.Select(x => x.GetViewModel).ToList();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|