ISEbd-21_Agliullov.D.A._Con.../ConfectioneryDatabaseImplement/ClientStorage.cs

89 lines
3.0 KiB
C#
Raw Normal View History

using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.SearchModels;
using ConfectioneryContracts.StoragesContract;
using ConfectioneryContracts.ViewModels;
using ConfectioneryDatabaseImplement.Models;
namespace ConfectioneryDatabaseImplement.Implements
{
public class ClientStorage : IClientStorage
{
public ClientViewModel? Delete(ClientBindingModel model)
{
using var context = new ConfectioneryDatabase();
var res = context.Clients.FirstOrDefault(x => x.Id == model.Id);
if (res != null)
{
context.Clients.Remove(res);
2023-03-04 20:45:41 +04:00
context.SaveChanges();
}
return res?.GetViewModel;
}
public ClientViewModel? GetElement(ClientSearchModel model)
{
using var context = new ConfectioneryDatabase();
if (model.Id.HasValue)
return context.Clients.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
if (model.Email != null && model.Password != null)
return context.Clients
.FirstOrDefault(x => x.Email.Equals(model.Email)
&& x.Password.Equals(model.Password))
?.GetViewModel;
if (model.Email != null)
return context.Clients.FirstOrDefault(x => x.Email.Equals(model.Email))?.GetViewModel;
return null;
}
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
{
if (model == null)
{
return new();
}
if (model.Id.HasValue)
{
var res = GetElement(model);
return res != null ? new() { res } : new();
}
if (model.Email != null)
{
using var context = new ConfectioneryDatabase();
return context.Clients
.Where(x => x.Email.Contains(model.Email))
.Select(x => x.GetViewModel)
.ToList();
}
return new();
}
public List<ClientViewModel> GetFullList()
{
using var context = new ConfectioneryDatabase();
return context.Clients.Select(x => x.GetViewModel).ToList();
}
public ClientViewModel? Insert(ClientBindingModel model)
{
using var context = new ConfectioneryDatabase();
var res = Client.Create(model);
if (res != null)
{
context.Clients.Add(res);
2023-03-04 20:45:41 +04:00
context.SaveChanges();
}
return res?.GetViewModel;
}
public ClientViewModel? Update(ClientBindingModel model)
{
using var context = new ConfectioneryDatabase();
var res = context.Clients.FirstOrDefault(x => x.Id == model.Id);
res?.Update(model);
2023-03-04 20:45:41 +04:00
context.SaveChanges();
return res?.GetViewModel;
}
}
}