SUBD_PIbd-21_Balberova_D.N./BeautySaloon/BeautySaloonDatabaseImplement/Implements/ClientStorage.cs

89 lines
2.8 KiB
C#
Raw Normal View History

2023-03-28 21:24:52 +04:00
using BeautySaloonContracts.BindingModels;
using BeautySaloonContracts.SearchModels;
using BeautySaloonContracts.StoragesContracts;
using BeautySaloonContracts.ViewModels;
namespace BeautySaloonDatabaseImplement.Implements
{
public class ClientStorage : IClientStorage
{
public ClientViewModel? Delete(ClientBindingModel model)
{
using var context = new NewdbContext();
var element = context.Clients
.FirstOrDefault(rec => rec.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 NewdbContext();
if (model.Id.HasValue)
return context.Clients
.FirstOrDefault(x => x.Id == model.Id)?
.GetViewModel;
if (!string.IsNullOrEmpty(model.Phone))
return context.Clients
.FirstOrDefault(x => x.Phone.Equals(model.Phone))?
.GetViewModel;
return null;
}
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
{
if (string.IsNullOrEmpty(model.Surname))
{
return new();
}
using var context = new NewdbContext();
return context.Clients
.Where(x => x.Surname.Contains(model.Surname))
.Select(x => x.GetViewModel)
.ToList();
}
public List<ClientViewModel> GetFullList()
{
using var context = new NewdbContext();
return context.Clients
.Select(x => x.GetViewModel)
.ToList();
}
public ClientViewModel? Insert(ClientBindingModel model)
{
2023-03-29 20:00:53 +04:00
using var context = new NewdbContext();
model.Id = context.Clients
.Count() > 0 ? context.Clients.Max(x => x.Id) + 1 : 1;
2023-03-28 21:24:52 +04:00
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 NewdbContext();
var client = context.Clients
.FirstOrDefault(x => x.Id == model.Id);
if (client == null)
{
return null;
}
client.Update(model);
context.SaveChanges();
return client.GetViewModel;
}
}
}