From 96f8c64d3c088712499438315460fa5380975124 Mon Sep 17 00:00:00 2001 From: platoff aeeee Date: Wed, 8 May 2024 11:24:46 +0400 Subject: [PATCH] lab5proccess --- FurnitureAssembly/FurnitureAssembly.sln | 12 +- .../BussinessLogic/ClientLogic.cs | 123 ++++++++++++++++++ .../BindingModels/ClientBindingModel.cs | 17 +++ .../BusinessLogicsContracts/IClientLogic.cs | 20 +++ .../SearchModels/ClientSearchModel.cs | 16 +++ .../StoragesContracts/IClientStorage.cs | 21 +++ .../ViewModels/ClientViewModel.cs | 21 +++ .../Models/IClientModel.cs | 15 +++ .../FurnitureAssemblyDatabase.cs | 5 +- .../Implements/ClientStorage.cs | 90 +++++++++++++ .../Models/Client.cs | 66 ++++++++++ .../DataFileSingleton.cs | 4 + .../Implements/ClientStorage.cs | 90 +++++++++++++ .../Models/Client.cs | 79 +++++++++++ .../DataListSingleton.cs | 3 + .../Implements/ClientStorage.cs | 114 ++++++++++++++++ .../Models/Client.cs | 54 ++++++++ .../Controllers/ClientController.cs | 66 ++++++++++ .../Controllers/MainController.cs | 82 ++++++++++++ .../FurnitureAssemblyRestApi.csproj | 22 ++++ .../FurnitureAssemblyRestApi/Program.cs | 46 +++++++ .../Properties/launchSettings.json | 31 +++++ .../WeatherForecast.cs | 13 ++ .../appsettings.Development.json | 8 ++ .../FurnitureAssemblyRestApi/appsettings.json | 9 ++ 25 files changed, 1022 insertions(+), 5 deletions(-) create mode 100644 FurnitureAssembly/FurnitureAssemblyBusinessLogic/BussinessLogic/ClientLogic.cs create mode 100644 FurnitureAssembly/FurnitureAssemblyContracts/BindingModels/ClientBindingModel.cs create mode 100644 FurnitureAssembly/FurnitureAssemblyContracts/BusinessLogicsContracts/IClientLogic.cs create mode 100644 FurnitureAssembly/FurnitureAssemblyContracts/SearchModels/ClientSearchModel.cs create mode 100644 FurnitureAssembly/FurnitureAssemblyContracts/StoragesContracts/IClientStorage.cs create mode 100644 FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/ClientViewModel.cs create mode 100644 FurnitureAssembly/FurnitureAssemblyDataModels/Models/IClientModel.cs create mode 100644 FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Implements/ClientStorage.cs create mode 100644 FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Models/Client.cs create mode 100644 FurnitureAssembly/FurnitureAssemblyFileImplement/Implements/ClientStorage.cs create mode 100644 FurnitureAssembly/FurnitureAssemblyFileImplement/Models/Client.cs create mode 100644 FurnitureAssembly/FurnitureAssemblyListImplement/Implements/ClientStorage.cs create mode 100644 FurnitureAssembly/FurnitureAssemblyListImplement/Models/Client.cs create mode 100644 FurnitureAssembly/FurnitureAssemblyRestApi/Controllers/ClientController.cs create mode 100644 FurnitureAssembly/FurnitureAssemblyRestApi/Controllers/MainController.cs create mode 100644 FurnitureAssembly/FurnitureAssemblyRestApi/FurnitureAssemblyRestApi.csproj create mode 100644 FurnitureAssembly/FurnitureAssemblyRestApi/Program.cs create mode 100644 FurnitureAssembly/FurnitureAssemblyRestApi/Properties/launchSettings.json create mode 100644 FurnitureAssembly/FurnitureAssemblyRestApi/WeatherForecast.cs create mode 100644 FurnitureAssembly/FurnitureAssemblyRestApi/appsettings.Development.json create mode 100644 FurnitureAssembly/FurnitureAssemblyRestApi/appsettings.json diff --git a/FurnitureAssembly/FurnitureAssembly.sln b/FurnitureAssembly/FurnitureAssembly.sln index 6c18e66..41a1853 100644 --- a/FurnitureAssembly/FurnitureAssembly.sln +++ b/FurnitureAssembly/FurnitureAssembly.sln @@ -22,12 +22,18 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FurnitureAssemblyFileImplem {A5F526DF-C1CD-4FCA-87E6-BDB447FF959C} = {A5F526DF-C1CD-4FCA-87E6-BDB447FF959C} EndProjectSection EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FurnitureAssemblyDatabaseImplement", "FurnitureAssemblyDatabaseImplement\FurnitureAssemblyDatabaseImplement.csproj", "{AB4E4750-5DA4-494F-A45F-B9CEE69299BD}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FurnitureAssemblyDatabaseImplement", "FurnitureAssemblyDatabaseImplement\FurnitureAssemblyDatabaseImplement.csproj", "{AB4E4750-5DA4-494F-A45F-B9CEE69299BD}" ProjectSection(ProjectDependencies) = postProject {7A1F9F2F-DDA5-4C97-8FAA-149D5372365D} = {7A1F9F2F-DDA5-4C97-8FAA-149D5372365D} {A5F526DF-C1CD-4FCA-87E6-BDB447FF959C} = {A5F526DF-C1CD-4FCA-87E6-BDB447FF959C} EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FurnitureAssemblyRestApi", "FurnitureAssemblyRestApi\FurnitureAssemblyRestApi.csproj", "{117FB767-3A58-4EB4-BA60-9E0A5A69D30F}" + ProjectSection(ProjectDependencies) = postProject + {27F73911-7647-475A-875D-C7A4046B0958} = {27F73911-7647-475A-875D-C7A4046B0958} + {AB4E4750-5DA4-494F-A45F-B9CEE69299BD} = {AB4E4750-5DA4-494F-A45F-B9CEE69299BD} + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -62,6 +68,10 @@ Global {AB4E4750-5DA4-494F-A45F-B9CEE69299BD}.Debug|Any CPU.Build.0 = Debug|Any CPU {AB4E4750-5DA4-494F-A45F-B9CEE69299BD}.Release|Any CPU.ActiveCfg = Release|Any CPU {AB4E4750-5DA4-494F-A45F-B9CEE69299BD}.Release|Any CPU.Build.0 = Release|Any CPU + {117FB767-3A58-4EB4-BA60-9E0A5A69D30F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {117FB767-3A58-4EB4-BA60-9E0A5A69D30F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {117FB767-3A58-4EB4-BA60-9E0A5A69D30F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {117FB767-3A58-4EB4-BA60-9E0A5A69D30F}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/FurnitureAssembly/FurnitureAssemblyBusinessLogic/BussinessLogic/ClientLogic.cs b/FurnitureAssembly/FurnitureAssemblyBusinessLogic/BussinessLogic/ClientLogic.cs new file mode 100644 index 0000000..05fdd38 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyBusinessLogic/BussinessLogic/ClientLogic.cs @@ -0,0 +1,123 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.BusinessLogicsContracts; +using FurnitureAssemblyContracts.SearchModels; +using FurnitureAssemblyContracts.StoragesContracts; +using FurnitureAssemblyContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssemblyBusinessLogic.BussinessLogic +{ + public class ClientLogic : IClientLogic + { + private readonly ILogger _logger; + private readonly IClientStorage _clientStorage; + + public ClientLogic(ILogger logger, IClientStorage clientStorage) + { + _logger = logger; + _clientStorage = clientStorage; + } + + public bool Create(ClientBindingModel model) + { + CheckModel(model); + if (_clientStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Delete(ClientBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_clientStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + public ClientViewModel? ReadElement(ClientSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. Email: {Email} Id:{ Id}", model.Email, model.Id); + var element = _clientStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + return element; + } + + public List? ReadList(ClientSearchModel? model) + { + _logger.LogInformation("ReadList. Email: {Email}. Id:{ Id}", model?.Email, model?.Id); + var list = model == null ? _clientStorage.GetFullList() : _clientStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + public bool Update(ClientBindingModel model) + { + CheckModel(model); + if (_clientStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + private void CheckModel(ClientBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.ClientFIO)) + { + throw new ArgumentNullException("Нет ФИО пользователя", + nameof(model.ClientFIO)); + } + if (string.IsNullOrEmpty(model.Email)) + { + throw new ArgumentNullException("Нет почтового адреса пользователя", + nameof(model.Email)); + } + if (string.IsNullOrEmpty(model.Password)) + { + throw new ArgumentNullException("Нет пароля пользователя", + nameof(model.Password)); + } + _logger.LogInformation("Component. ClientFIO:{ClientFIO}. Email:{ Email}. Id: { Id}", model.ClientFIO, model.Email, model.Id); + var element = _clientStorage.GetElement(new ClientSearchModel { Email = model.Email }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Пользователь с таким логином уже есть"); + } + } + } +} diff --git a/FurnitureAssembly/FurnitureAssemblyContracts/BindingModels/ClientBindingModel.cs b/FurnitureAssembly/FurnitureAssemblyContracts/BindingModels/ClientBindingModel.cs new file mode 100644 index 0000000..22bce2c --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyContracts/BindingModels/ClientBindingModel.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using FurnitureAssemblyDataModels.Models; + +namespace FurnitureAssemblyContracts.BindingModels +{ + public class ClientBindingModel : IClientModel + { + public int Id { get; set; } + public string ClientFIO { get; set; } = string.Empty; + public string Email { get; set; } = string.Empty; + public string Password { get; set; } = string.Empty; + } +} diff --git a/FurnitureAssembly/FurnitureAssemblyContracts/BusinessLogicsContracts/IClientLogic.cs b/FurnitureAssembly/FurnitureAssemblyContracts/BusinessLogicsContracts/IClientLogic.cs new file mode 100644 index 0000000..871a8f4 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyContracts/BusinessLogicsContracts/IClientLogic.cs @@ -0,0 +1,20 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.ViewModels; +using FurnitureAssemblyContracts.SearchModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssemblyContracts.BusinessLogicsContracts +{ + public interface IClientLogic + { + List? ReadList(ClientSearchModel? model); + ClientViewModel? ReadElement(ClientSearchModel model); + bool Create(ClientBindingModel model); + bool Update(ClientBindingModel model); + bool Delete(ClientBindingModel model); + } +} diff --git a/FurnitureAssembly/FurnitureAssemblyContracts/SearchModels/ClientSearchModel.cs b/FurnitureAssembly/FurnitureAssemblyContracts/SearchModels/ClientSearchModel.cs new file mode 100644 index 0000000..3a802d3 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyContracts/SearchModels/ClientSearchModel.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssemblyContracts.SearchModels +{ + public class ClientSearchModel + { + public int? Id { get; set; } + public string? ClientFIO { get; set; } + public string? Email { get; set; } + public string? Password { get; set; } + } +} diff --git a/FurnitureAssembly/FurnitureAssemblyContracts/StoragesContracts/IClientStorage.cs b/FurnitureAssembly/FurnitureAssemblyContracts/StoragesContracts/IClientStorage.cs new file mode 100644 index 0000000..3bdb908 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyContracts/StoragesContracts/IClientStorage.cs @@ -0,0 +1,21 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.SearchModels; +using FurnitureAssemblyContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssemblyContracts.StoragesContracts +{ + public interface IClientStorage + { + List GetFullList(); + List GetFilteredList(ClientSearchModel model); + ClientViewModel? GetElement(ClientSearchModel model); + ClientViewModel? Insert(ClientBindingModel model); + ClientViewModel? Update(ClientBindingModel model); + ClientViewModel? Delete(ClientBindingModel model); + } +} diff --git a/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/ClientViewModel.cs b/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/ClientViewModel.cs new file mode 100644 index 0000000..cac94f5 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/ClientViewModel.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using FurnitureAssemblyDataModels.Models; + +namespace FurnitureAssemblyContracts.ViewModels +{ + public class ClientViewModel : IClientModel + { + public int Id { get; set; } + [DisplayName("ФИО клиента")] + public string ClientFIO { get; set; } = string.Empty; + [DisplayName("Логин (эл. почта)")] + public string Email { get; set; } = string.Empty; + [DisplayName("Пароль")] + public string Password { get; set; } = string.Empty; + } +} diff --git a/FurnitureAssembly/FurnitureAssemblyDataModels/Models/IClientModel.cs b/FurnitureAssembly/FurnitureAssemblyDataModels/Models/IClientModel.cs new file mode 100644 index 0000000..70c3063 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyDataModels/Models/IClientModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssemblyDataModels.Models +{ + public interface IClientModel : IId + { + string ClientFIO { get; } + string Email { get; } + string Password { get; } + } +} diff --git a/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/FurnitureAssemblyDatabase.cs b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/FurnitureAssemblyDatabase.cs index 5f939e3..e3f8ccf 100644 --- a/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/FurnitureAssemblyDatabase.cs +++ b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/FurnitureAssemblyDatabase.cs @@ -19,13 +19,10 @@ namespace FurnitureAssemblyDatabaseImplement base.OnConfiguring(optionsBuilder); } - public virtual DbSet WorkPieces { set; get; } - public virtual DbSet Furnitures { set; get; } - public virtual DbSet FurnitureWorkPieces { set; get; } - public virtual DbSet Orders { set; get; } + public virtual DbSet Clients { set; get; } } } diff --git a/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Implements/ClientStorage.cs b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Implements/ClientStorage.cs new file mode 100644 index 0000000..520c7a0 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Implements/ClientStorage.cs @@ -0,0 +1,90 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.SearchModels; +using FurnitureAssemblyContracts.StoragesContracts; +using FurnitureAssemblyContracts.ViewModels; +using FurnitureAssemblyDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssemblyDatabaseImplement.Implements +{ + public class ClientStorage : IClientStorage + { + public ClientViewModel? Delete(ClientBindingModel model) + { + using var context = new FurnitureAssemblyDatabase(); + 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) + { + if (string.IsNullOrEmpty(model.Email) && !model.Id.HasValue) + { + return null; + } + using var context = new FurnitureAssemblyDatabase(); + return context.Clients.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.Email) && x.Email == model.Email) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public List GetFilteredList(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.Email) || string.IsNullOrEmpty(model.Password)) + { + return new(); + } + using var context = new FurnitureAssemblyDatabase(); + return context.Clients + .Where(x => x.Email.Equals(model.Email) && x.Password.Equals(model.Password)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new FurnitureAssemblyDatabase(); + return context.Clients + .Select(x => x.GetViewModel) + .ToList(); + } + + public ClientViewModel? Insert(ClientBindingModel model) + { + var newClient = Client.Create(model); + if (newClient == null) + { + return null; + } + using var context = new FurnitureAssemblyDatabase(); + context.Clients.Add(newClient); + context.SaveChanges(); + return newClient.GetViewModel; + } + + public ClientViewModel? Update(ClientBindingModel model) + { + using var context = new FurnitureAssemblyDatabase(); + var client = context.Clients.FirstOrDefault(x => x.Id == model.Id); + if (client == null) + { + return null; + } + client.Update(model); + context.SaveChanges(); + return client.GetViewModel; + } + } +} diff --git a/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Models/Client.cs b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Models/Client.cs new file mode 100644 index 0000000..c7a5157 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Models/Client.cs @@ -0,0 +1,66 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.ViewModels; +using FurnitureAssemblyDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssemblyDatabaseImplement.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 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 + }; + } +} diff --git a/FurnitureAssembly/FurnitureAssemblyFileImplement/DataFileSingleton.cs b/FurnitureAssembly/FurnitureAssemblyFileImplement/DataFileSingleton.cs index 079512c..d03ca45 100644 --- a/FurnitureAssembly/FurnitureAssemblyFileImplement/DataFileSingleton.cs +++ b/FurnitureAssembly/FurnitureAssemblyFileImplement/DataFileSingleton.cs @@ -17,12 +17,14 @@ namespace FurnitureAssemblyFileImplement private readonly string OrderFileName = "Order.xml"; private readonly string FurnitureFileName = "Furniture.xml"; + private readonly string ClientFileName = "Client.xml"; public List WorkPieces { get; private set; } public List Orders { get; private set; } public List Furnitures { get; private set; } + public List Clients { get; private set; } public static DataFileSingleton GetInstance() { @@ -39,6 +41,7 @@ namespace FurnitureAssemblyFileImplement public void SaveFurnitures() => SaveData(Furnitures, FurnitureFileName, "Furnitures", x => x.GetXElement); public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + public void SaveClients() => SaveData(Clients, ClientFileName, "Clients", x => x.GetXElement); private DataFileSingleton() @@ -46,6 +49,7 @@ namespace FurnitureAssemblyFileImplement WorkPieces = LoadData(WorkPieceFileName, "WorkPiece", x => WorkPiece.Create(x)!)!; Furnitures = LoadData(FurnitureFileName, "Furniture", x => Furniture.Create(x)!)!; Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + Clients = LoadData(ClientFileName, "Client", x => Client.Create(x)!)!; } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) diff --git a/FurnitureAssembly/FurnitureAssemblyFileImplement/Implements/ClientStorage.cs b/FurnitureAssembly/FurnitureAssemblyFileImplement/Implements/ClientStorage.cs new file mode 100644 index 0000000..261f820 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyFileImplement/Implements/ClientStorage.cs @@ -0,0 +1,90 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.SearchModels; +using FurnitureAssemblyContracts.StoragesContracts; +using FurnitureAssemblyContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssemblyFileImplement.Implements +{ + public class ClientStorage : IClientStorage + { + private readonly DataFileSingleton source; + public ClientStorage() + { + source = DataFileSingleton.GetInstance(); + } + public ClientViewModel? Delete(ClientBindingModel model) + { + var client = source.Clients.FirstOrDefault(x => x.Id == model.Id); + if (client != null) + { + source.Clients.Remove(client); + source.SaveClients(); + return client.GetViewModel; + } + return null; + } + + public ClientViewModel? GetElement(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.Email) && !model.Id.HasValue) + { + return null; + } + return source.Clients + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.Email) && x.Email == + model.Email) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public List GetFilteredList(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.Email) || string.IsNullOrEmpty(model.Password)) + { + return new(); + } + return source.Clients + .Where(x => x.Email.Equals(model.Email) && x.Password.Equals(model.Password)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + return source.Clients + .Select(x => x.GetViewModel) + .ToList(); + } + + public ClientViewModel? Insert(ClientBindingModel model) + { + model.Id = source.Clients.Count > 0 ? source.Clients.Max(x => x.Id) + 1 : 1; + var newClient = Client.Create(model); + if (newClient == null) + { + return null; + } + source.Clients.Add(newClient); + source.SaveClients(); + return newClient.GetViewModel; + } + + public ClientViewModel? Update(ClientBindingModel model) + { + var client = source.Clients.FirstOrDefault(x => x.Id == model.Id); + if (client == null) + { + return null; + } + client.Update(model); + source.SaveClients(); + return client.GetViewModel; + } + } +} diff --git a/FurnitureAssembly/FurnitureAssemblyFileImplement/Models/Client.cs b/FurnitureAssembly/FurnitureAssemblyFileImplement/Models/Client.cs new file mode 100644 index 0000000..4f2cbf3 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyFileImplement/Models/Client.cs @@ -0,0 +1,79 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.ViewModels; +using FurnitureAssemblyDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace FurnitureAssemblyFileImplement.Models +{ + public class Client : IClientModel + { + public int Id { get; private set; } + + public string ClientFIO { get; private set; } = string.Empty; + + public string Email { get; private set; } = string.Empty; + + 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(XElement element) + { + if (element == null) + { + return null; + } + return new Client() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ClientFIO = element.Element("ClientFIO")!.Value, + Email = element.Element("Email")!.Value, + Password = element.Element("Password")!.Value + }; + } + + 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 + }; + + public XElement GetXElement => new("Client", + new XAttribute("Id", Id), + new XElement("ClientFIO", ClientFIO), + new XElement("Email", Email), + new XElement("Password", Password) + ); + } +} diff --git a/FurnitureAssembly/FurnitureAssemblyListImplement/DataListSingleton.cs b/FurnitureAssembly/FurnitureAssemblyListImplement/DataListSingleton.cs index 6fa7e16..80b7a94 100644 --- a/FurnitureAssembly/FurnitureAssemblyListImplement/DataListSingleton.cs +++ b/FurnitureAssembly/FurnitureAssemblyListImplement/DataListSingleton.cs @@ -14,6 +14,7 @@ namespace FurnitureAssemblyListImplement public List Furnitures { get; set; } public List WorkPiece { get; set; } + public List Clients { get; set; } public DataListSingleton() @@ -21,6 +22,8 @@ namespace FurnitureAssemblyListImplement WorkPiece = new List(); Furnitures = new List(); Orders = new List(); + Clients = new List(); + } public static DataListSingleton GetInstance() diff --git a/FurnitureAssembly/FurnitureAssemblyListImplement/Implements/ClientStorage.cs b/FurnitureAssembly/FurnitureAssemblyListImplement/Implements/ClientStorage.cs new file mode 100644 index 0000000..b49b91f --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyListImplement/Implements/ClientStorage.cs @@ -0,0 +1,114 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.SearchModels; +using FurnitureAssemblyContracts.StoragesContracts; +using FurnitureAssemblyContracts.ViewModels; +using FurnitureAssemblyListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssemblyListImplement.Implements +{ + public class ClientStorage : IClientStorage + { + private readonly DataListSingleton _source; + + public ClientStorage() + { + _source = DataListSingleton.GetInstance(); + } + + public ClientViewModel? Delete(ClientBindingModel model) + { + for (int i = 0; i < _source.Clients.Count; ++i) + { + if (_source.Clients[i].Id == model.Id) + { + var element = _source.Clients[i]; + _source.Clients.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } + + public ClientViewModel? GetElement(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.Email) && !model.Id.HasValue) + { + return null; + } + + foreach (var client in _source.Clients) + { + if ((!string.IsNullOrEmpty(model.Email) && client.Email == model.Email) + || (model.Id.HasValue && client.Id == model.Id)) + { + return client.GetViewModel; + } + } + return null; + } + + public List GetFilteredList(ClientSearchModel model) + { + var result = new List(); + if (string.IsNullOrEmpty(model.Email)) + { + return result; + } + foreach (var client in _source.Clients) + { + if (client.Email.Equals(model.Email) && client.Password.Equals(model.Password)) + { + result.Add(client.GetViewModel); + } + } + return result; + } + + public List GetFullList() + { + var result = new List(); + foreach (var client in _source.Clients) + { + result.Add(client.GetViewModel); + } + return result; + } + + public ClientViewModel? Insert(ClientBindingModel model) + { + model.Id = 1; + foreach (var client in _source.Clients) + { + if (model.Id <= client.Id) + { + model.Id = client.Id + 1; + } + } + var newClient = Client.Create(model); + if (newClient == null) + { + return null; + } + _source.Clients.Add(newClient); + return newClient.GetViewModel; + } + + public ClientViewModel? Update(ClientBindingModel model) + { + foreach (var client in _source.Clients) + { + if (client.Id == model.Id) + { + client.Update(model); + return client.GetViewModel; + } + } + return null; + } + } +} diff --git a/FurnitureAssembly/FurnitureAssemblyListImplement/Models/Client.cs b/FurnitureAssembly/FurnitureAssemblyListImplement/Models/Client.cs new file mode 100644 index 0000000..38d4b28 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyListImplement/Models/Client.cs @@ -0,0 +1,54 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.ViewModels; +using FurnitureAssemblyDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssemblyListImplement.Models +{ + public class Client : IClientModel + { + public int Id { get; private set; } + + public string ClientFIO { get; private set; } = string.Empty; + + public string Email { get; private set; } = string.Empty; + + 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 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 + }; + } +} diff --git a/FurnitureAssembly/FurnitureAssemblyRestApi/Controllers/ClientController.cs b/FurnitureAssembly/FurnitureAssemblyRestApi/Controllers/ClientController.cs new file mode 100644 index 0000000..3b39abb --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyRestApi/Controllers/ClientController.cs @@ -0,0 +1,66 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.BusinessLogicsContracts; +using FurnitureAssemblyContracts.SearchModels; +using FurnitureAssemblyContracts.ViewModels; +using Microsoft.AspNetCore.Mvc; + +namespace FurnitureAssemblyRestApi.Controllers +{ + [Route("api/[controller]/[action]")] + [ApiController] + public class ClientController : Controller + { + private readonly ILogger _logger; + private readonly IClientLogic _logic; + + public ClientController(IClientLogic logic, ILogger logger) + { + _logger = logger; + _logic = logic; + } + + [HttpGet] + public ClientViewModel? Login(string login, string password) + { + try + { + return _logic.ReadElement(new ClientSearchModel + { + Email = login, + Password = password + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка входа в систему"); + throw; + } + } + [HttpPost] + public void Register(ClientBindingModel model) + { + try + { + _logic.Create(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка регистрации"); + throw; + } + } + [HttpPost] + public void UpdateData(ClientBindingModel model) + { + try + { + _logic.Update(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка обновления данных"); + throw; + } + } + } +} diff --git a/FurnitureAssembly/FurnitureAssemblyRestApi/Controllers/MainController.cs b/FurnitureAssembly/FurnitureAssemblyRestApi/Controllers/MainController.cs new file mode 100644 index 0000000..979cbe7 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyRestApi/Controllers/MainController.cs @@ -0,0 +1,82 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.BusinessLogicsContracts; +using FurnitureAssemblyContracts.SearchModels; +using FurnitureAssemblyContracts.ViewModels; +using Microsoft.AspNetCore.Mvc; + +namespace FurnitureAssemblyRestApi.Controllers +{ + [Route("api/[controller]/[action]")] + [ApiController] + public class MainController : Controller + { + private readonly ILogger _logger; + private readonly IOrderLogic _order; + private readonly IFurnitureLogic _furniture; + public MainController(ILogger logger, IOrderLogic order, IFurnitureLogic furniture) + { + _logger = logger; + _order = order; + _furniture = furniture; + } + [HttpGet] + public List? GetFurnitureList() + { + try + { + return _furniture.ReadList(null); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения списка мебели"); + throw; + } + } + [HttpGet] + public FurnitureViewModel? GetFurniture(int furnitureId) + { + try + { + return _furniture.ReadElement(new FurnitureSearchModel + { + Id = furnitureId + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения мебели по id={Id}", + furnitureId); + throw; + } + } + [HttpGet] + public List? GetOrders(int clientId) + { + try + { + return _order.ReadList(new OrderSearchModel + { + ClientId = clientId + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения списка заказов клиента id ={ Id}", clientId); + throw; + } + } + [HttpPost] + public void CreateOrder(OrderBindingModel model) + { + try + { + _order.CreateOrder(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка создания заказа"); + throw; + } + } + } +} diff --git a/FurnitureAssembly/FurnitureAssemblyRestApi/FurnitureAssemblyRestApi.csproj b/FurnitureAssembly/FurnitureAssemblyRestApi/FurnitureAssemblyRestApi.csproj new file mode 100644 index 0000000..9e67636 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyRestApi/FurnitureAssemblyRestApi.csproj @@ -0,0 +1,22 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + + + + + + diff --git a/FurnitureAssembly/FurnitureAssemblyRestApi/Program.cs b/FurnitureAssembly/FurnitureAssemblyRestApi/Program.cs new file mode 100644 index 0000000..60494ed --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyRestApi/Program.cs @@ -0,0 +1,46 @@ +using FurnitureAssemblyBusinessLogic.BussinessLogic; +using FurnitureAssemblyContracts.BusinessLogicsContracts; +using FurnitureAssemblyContracts.StoragesContracts; +using FurnitureAssemblyDatabaseImplement.Implements; +using Microsoft.OpenApi.Models; + + +var builder = WebApplication.CreateBuilder(args); + +builder.Logging.SetMinimumLevel(LogLevel.Trace); +builder.Logging.AddLog4Net("log4net.config"); +// Add services to the container. +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddControllers(); +builder.Services.AddControllers(); + +// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(c => +{ + c.SwaggerDoc("v1", new OpenApiInfo + { + Title = "FurnitureAssemblyRestApi", Version = "v1" + }); +}); +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "FurnitureAssemblyRestApi v1")); +} + +app.UseHttpsRedirection(); + +app.UseAuthorization(); + +app.MapControllers(); + +app.Run(); diff --git a/FurnitureAssembly/FurnitureAssemblyRestApi/Properties/launchSettings.json b/FurnitureAssembly/FurnitureAssemblyRestApi/Properties/launchSettings.json new file mode 100644 index 0000000..ece7b1d --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyRestApi/Properties/launchSettings.json @@ -0,0 +1,31 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:10164", + "sslPort": 44314 + } + }, + "profiles": { + "FurnitureAssemblyRestApi": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7042;http://localhost:5042", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/FurnitureAssembly/FurnitureAssemblyRestApi/WeatherForecast.cs b/FurnitureAssembly/FurnitureAssemblyRestApi/WeatherForecast.cs new file mode 100644 index 0000000..847ab2b --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyRestApi/WeatherForecast.cs @@ -0,0 +1,13 @@ +namespace FurnitureAssemblyRestApi +{ + public class WeatherForecast + { + public DateTime Date { get; set; } + + public int TemperatureC { get; set; } + + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); + + public string? Summary { get; set; } + } +} \ No newline at end of file diff --git a/FurnitureAssembly/FurnitureAssemblyRestApi/appsettings.Development.json b/FurnitureAssembly/FurnitureAssemblyRestApi/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyRestApi/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/FurnitureAssembly/FurnitureAssemblyRestApi/appsettings.json b/FurnitureAssembly/FurnitureAssemblyRestApi/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyRestApi/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +}