Database: add clients
This commit is contained in:
parent
5c55c2584c
commit
87f51db748
@ -7,7 +7,7 @@ namespace SushiBarContracts.StoragesContracts;
|
|||||||
public interface IClientStorage
|
public interface IClientStorage
|
||||||
{
|
{
|
||||||
List<ClientViewModel> GetFullList();
|
List<ClientViewModel> GetFullList();
|
||||||
List<ClientViewModel> GetFilteredList(ClientSearchModel model);
|
List<ClientViewModel> GetFilteredList(ClientSearchModel? model);
|
||||||
ClientViewModel? GetElement(ClientSearchModel model);
|
ClientViewModel? GetElement(ClientSearchModel model);
|
||||||
ClientViewModel? Insert(ClientBindingModel model);
|
ClientViewModel? Insert(ClientBindingModel model);
|
||||||
ClientViewModel? Update(ClientBindingModel model);
|
ClientViewModel? Update(ClientBindingModel model);
|
||||||
|
101
SushiBar/SushiBarDatabaseImplement/Implements/ClientStorage.cs
Normal file
101
SushiBar/SushiBarDatabaseImplement/Implements/ClientStorage.cs
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
using SushiBarContracts.BindingModels;
|
||||||
|
using SushiBarContracts.SearchModels;
|
||||||
|
using SushiBarContracts.StoragesContracts;
|
||||||
|
using SushiBarContracts.ViewModels;
|
||||||
|
using SushiBarDatabaseImplement.Models;
|
||||||
|
using SushiBarDataModels.Models;
|
||||||
|
|
||||||
|
namespace SushiBarDatabaseImplement.Implements;
|
||||||
|
|
||||||
|
public class ClientStorage : IClientStorage
|
||||||
|
{
|
||||||
|
public List<ClientViewModel> GetFullList()
|
||||||
|
{
|
||||||
|
using var context = new SushiBarDatabase();
|
||||||
|
|
||||||
|
return context.Clients
|
||||||
|
.Select(x => x.GetViewModel)
|
||||||
|
.ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<ClientViewModel> GetFilteredList(ClientSearchModel? model)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(model?.Email) ||
|
||||||
|
string.IsNullOrEmpty(model.ClientFio))
|
||||||
|
{
|
||||||
|
return new List<ClientViewModel>();
|
||||||
|
}
|
||||||
|
|
||||||
|
using var context = new SushiBarDatabase();
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(model.Email))
|
||||||
|
{
|
||||||
|
return context.Clients
|
||||||
|
.Where(x => x.Email.Equals(model.Email))
|
||||||
|
.Select(x => x.GetViewModel)
|
||||||
|
.ToList();
|
||||||
|
}
|
||||||
|
return context.Clients
|
||||||
|
.Where(x => x.ClientFio.Equals(model.ClientFio))
|
||||||
|
.Select(x => x.GetViewModel)
|
||||||
|
.ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ClientViewModel? GetElement(ClientSearchModel model)
|
||||||
|
{
|
||||||
|
if (!model.Id.HasValue)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
using var context = new SushiBarDatabase();
|
||||||
|
return context.Clients
|
||||||
|
.FirstOrDefault(x => x.Id == model.Id)
|
||||||
|
?.GetViewModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ClientViewModel? Insert(ClientBindingModel model)
|
||||||
|
{
|
||||||
|
var newClient = Client.Create(model);
|
||||||
|
if (newClient == null)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
using var context = new SushiBarDatabase();
|
||||||
|
context.Clients.Add(newClient);
|
||||||
|
context.SaveChanges();
|
||||||
|
|
||||||
|
return newClient.GetViewModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ClientViewModel? Update(ClientBindingModel model)
|
||||||
|
{
|
||||||
|
using var context = new SushiBarDatabase();
|
||||||
|
|
||||||
|
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 SushiBarDatabase();
|
||||||
|
|
||||||
|
var client = context.Clients.FirstOrDefault(x => x.Id == model.Id);
|
||||||
|
|
||||||
|
if (client != null)
|
||||||
|
{
|
||||||
|
context.Clients.Remove(client);
|
||||||
|
context.SaveChanges();
|
||||||
|
|
||||||
|
return client.GetViewModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
56
SushiBar/SushiBarDatabaseImplement/Models/Client.cs
Normal file
56
SushiBar/SushiBarDatabaseImplement/Models/Client.cs
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using SushiBarContracts.BindingModels;
|
||||||
|
using SushiBarContracts.ViewModels;
|
||||||
|
using SushiBarDataModels.Models;
|
||||||
|
|
||||||
|
namespace SushiBarDatabaseImplement.Models;
|
||||||
|
|
||||||
|
public class Client : IClientModel
|
||||||
|
{
|
||||||
|
public int Id { get; private init; }
|
||||||
|
[Required]
|
||||||
|
public string ClientFio { get; private set; } = string.Empty;
|
||||||
|
[Required]
|
||||||
|
public string Email { get; private set; } = string.Empty;
|
||||||
|
[Required]
|
||||||
|
public string Password { get; private set; } = string.Empty;
|
||||||
|
|
||||||
|
public static Client? Create(ClientBindingModel? model)
|
||||||
|
{
|
||||||
|
if (model == null) return null;
|
||||||
|
return new Client
|
||||||
|
{
|
||||||
|
Id = model.Id,
|
||||||
|
ClientFio = model.ClientFio,
|
||||||
|
Email = model.Email,
|
||||||
|
Password = model.Password
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Client Create(ClientViewModel model)
|
||||||
|
{
|
||||||
|
return new Client
|
||||||
|
{
|
||||||
|
Id = model.Id,
|
||||||
|
ClientFio = model.ClientFio,
|
||||||
|
Email = model.Email,
|
||||||
|
Password = model.Password
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Update(ClientBindingModel? model)
|
||||||
|
{
|
||||||
|
if (model == null) return;
|
||||||
|
ClientFio = model.ClientFio;
|
||||||
|
Email = model.Email;
|
||||||
|
Password = model.Password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ClientViewModel GetViewModel => new()
|
||||||
|
{
|
||||||
|
Id = Id,
|
||||||
|
ClientFio = ClientFio,
|
||||||
|
Email = Email,
|
||||||
|
Password = Password
|
||||||
|
};
|
||||||
|
}
|
@ -17,5 +17,6 @@ namespace SushiBarDatabaseImplement
|
|||||||
public virtual DbSet<Sushi> Sushi { set; get; }
|
public virtual DbSet<Sushi> Sushi { set; get; }
|
||||||
public virtual DbSet<SushiComponent> SushiComponents { set; get; }
|
public virtual DbSet<SushiComponent> SushiComponents { set; get; }
|
||||||
public virtual DbSet<Order> Orders { set; get; }
|
public virtual DbSet<Order> Orders { set; get; }
|
||||||
|
public virtual DbSet<Client> Clients { set; get; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user