From 5499952d38b0c39de134fb04fa64b6579fab974f Mon Sep 17 00:00:00 2001 From: bekodeg Date: Sun, 28 Apr 2024 20:26:49 +0400 Subject: [PATCH] =?UTF-8?q?=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8F=20=D1=85=D1=80=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8?= =?UTF-8?q?=D1=89=D0=B0=20=D0=BA=D0=BB=D0=B8=D0=B5=D0=BD=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/ClientStorage.cs | 90 +++++++++++++++++++ .../Models/Client.cs | 50 +++++++++++ .../SushiBarDatabase.cs | 1 + 3 files changed, 141 insertions(+) create mode 100644 SushiBar/SushiBarDatabaseImplement/Implements/ClientStorage.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/Models/Client.cs diff --git a/SushiBar/SushiBarDatabaseImplement/Implements/ClientStorage.cs b/SushiBar/SushiBarDatabaseImplement/Implements/ClientStorage.cs new file mode 100644 index 0000000..998a57f --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Implements/ClientStorage.cs @@ -0,0 +1,90 @@ +using Microsoft.EntityFrameworkCore; +using SushiBarContracts.BindingModels; +using SushiBarContracts.SearchModels; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; +using SushiBarDatabaseImplement.Models; + +namespace SushiBarDatabaseImplement.Implements +{ + public class ClientStorage : IClientStorage + { + public List GetFullList() + { + using var context = new SushiBarDatabase(); + return context.Clients + .Select(c => c.GetViewModel) + .ToList(); + } + public List GetFilteredList(ClientSearchModel model) + { + using var context = new SushiBarDatabase(); + return context.Clients + .Where(c => + (model.Id.HasValue && c.Id == model.Id) || + (!string.IsNullOrEmpty(model.ClientFIO) && model.ClientFIO == c.ClientFIO) || + (!string.IsNullOrEmpty(model.Email) && model.Email == c.Email)) + .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(o => o.Id == model.Id)? + .GetViewModel; + } + public ClientViewModel? Insert(ClientBindingModel model) + { + using var context = new SushiBarDatabase(); + 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 SushiBarDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var client = context.Clients + .FirstOrDefault(c => c.Id == model.Id); + if (client == null) + { + return null; + } + client.Update(model); + context.SaveChanges(); + transaction.Commit(); + return client.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public ClientViewModel? Delete(ClientBindingModel model) + { + using var context = new SushiBarDatabase(); + var element = context.Clients + .FirstOrDefault(c => c.Id == model.Id); + if (element != null) + { + context.Clients.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/SushiBar/SushiBarDatabaseImplement/Models/Client.cs b/SushiBar/SushiBarDatabaseImplement/Models/Client.cs new file mode 100644 index 0000000..a01cbba --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Models/Client.cs @@ -0,0 +1,50 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.ViewModels; +using SushiBarDataModels.Models; +using System.ComponentModel.DataAnnotations; + +namespace SushiBarDatabaseImplement.Models +{ + public class Client : IClientModel + { + public int Id { get; private set; } + [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() + { + Id = model.Id, + ClientFIO = model.ClientFIO, + Password = model.Password + }; + } + + public void Update(ClientBindingModel model) + { + if (model == null) + { + return; + } + ClientFIO = string.IsNullOrEmpty(model.ClientFIO) ? ClientFIO : model.ClientFIO; + Email = string.IsNullOrEmpty(model.Email) ? Email : model.Email; + Password = string.IsNullOrEmpty(model.Password) ? Password : model.Password; + } + public ClientViewModel GetViewModel => new() + { + Id = Id, + ClientFIO = ClientFIO, + Email = Email, + Password = Password + }; + } +} diff --git a/SushiBar/SushiBarDatabaseImplement/SushiBarDatabase.cs b/SushiBar/SushiBarDatabaseImplement/SushiBarDatabase.cs index 0aaf345..68f3807 100644 --- a/SushiBar/SushiBarDatabaseImplement/SushiBarDatabase.cs +++ b/SushiBar/SushiBarDatabaseImplement/SushiBarDatabase.cs @@ -19,5 +19,6 @@ namespace SushiBarDatabaseImplement public virtual DbSet Sushis { set; get; } public virtual DbSet SushiComponents { set; get; } public virtual DbSet Orders { set; get; } + public virtual DbSet Clients { set; get; } } }