From 726011d7a521e143b68b26ec66f8d465b5ab3fd8 Mon Sep 17 00:00:00 2001 From: dasha Date: Sat, 1 Apr 2023 20:48:53 +0400 Subject: [PATCH 1/4] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BF=D1=80=D0=BE=D1=81=D1=82=D1=80?= =?UTF-8?q?=D0=B0=D0=BD=D1=81=D1=82=D0=B2=D0=B0=20=D0=B8=D0=BC=D0=B5=D0=BD?= =?UTF-8?q?=20+=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8?= =?UTF-8?q?=D1=8F=20=D1=85=D1=80=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8=D1=89=D0=B0?= =?UTF-8?q?=20=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0=D1=82=D0=B5?= =?UTF-8?q?=D0=BB=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/IUserStorage.cs | 39 -------- .../Implements/UserStorage.cs | 92 +++++++++++++++++++ .../Implements/Worker/BuildStorage.cs | 4 +- .../Implements/Worker/CommentStorage.cs | 4 +- .../Implements/Worker/PurchaseStorage.cs | 5 +- 5 files changed, 98 insertions(+), 46 deletions(-) delete mode 100644 HardwareShop/HardwareShopDatabaseImplement/Implements/IUserStorage.cs create mode 100644 HardwareShop/HardwareShopDatabaseImplement/Implements/UserStorage.cs diff --git a/HardwareShop/HardwareShopDatabaseImplement/Implements/IUserStorage.cs b/HardwareShop/HardwareShopDatabaseImplement/Implements/IUserStorage.cs deleted file mode 100644 index c04f7fc..0000000 --- a/HardwareShop/HardwareShopDatabaseImplement/Implements/IUserStorage.cs +++ /dev/null @@ -1,39 +0,0 @@ -using HardwareShopContracts.BindingModels; -using HardwareShopContracts.SearchModels; -using HardwareShopContracts.ViewModels; - -namespace HardwareShopContracts.StoragesContracts -{ - public class UserStorage : IUserStorage - { - public UserViewModel? Delete(UserBindingModel model) - { - throw new NotImplementedException(); - } - - public UserViewModel? GetElement(UserSearchModel model) - { - throw new NotImplementedException(); - } - - public List GetFilteredList(UserSearchModel model) - { - throw new NotImplementedException(); - } - - public List GetFullList() - { - throw new NotImplementedException(); - } - - public UserViewModel? Insert(UserBindingModel model) - { - throw new NotImplementedException(); - } - - public UserViewModel? Update(UserBindingModel model) - { - throw new NotImplementedException(); - } - } -} \ No newline at end of file diff --git a/HardwareShop/HardwareShopDatabaseImplement/Implements/UserStorage.cs b/HardwareShop/HardwareShopDatabaseImplement/Implements/UserStorage.cs new file mode 100644 index 0000000..0240de6 --- /dev/null +++ b/HardwareShop/HardwareShopDatabaseImplement/Implements/UserStorage.cs @@ -0,0 +1,92 @@ +using HardwareShopContracts.BindingModels; +using HardwareShopContracts.SearchModels; +using HardwareShopContracts.StoragesContracts; +using HardwareShopContracts.ViewModels; +using HardwareShopDatabaseImplement.Models; + +namespace HardwareShopDatabaseImplement.Implements +{ + public class UserStorage : IUserStorage + { + public UserViewModel? Delete(UserBindingModel model) + { + using var context = new HardwareShopDatabase(); + var element = context.Users + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Users.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public UserViewModel? GetElement(UserSearchModel model) + { + using var context = new HardwareShopDatabase(); + if (model.Id.HasValue) + return context.Users + .FirstOrDefault(x => x.Id == model.Id)? + .GetViewModel; + if (!string.IsNullOrEmpty(model.Email) && !string.IsNullOrEmpty(model.Password)) + return context.Users + .FirstOrDefault(x => x.Email + .Equals(model.Email) && x.Password + .Equals(model.Password))? + .GetViewModel; + if (!string.IsNullOrEmpty(model.Email)) + return context.Users + .FirstOrDefault(x => x.Email.Equals(model.Email))?.GetViewModel; + return null; + } + + public List GetFilteredList(UserSearchModel model) + { + if (string.IsNullOrEmpty(model.Login)) + { + return new(); + } + using var context = new HardwareShopDatabase(); + return context.Users + .Where(x => x.Login.Contains(model.Login)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new HardwareShopDatabase(); + return context.Users + .Select(x => x.GetViewModel) + .ToList(); + } + + public UserViewModel? Insert(UserBindingModel model) + { + var newUser = User.Create(model); + if (newUser == null) + { + return null; + } + using var context = new HardwareShopDatabase(); + context.Users.Add(newUser); + context.SaveChanges(); + return newUser.GetViewModel; + } + + public UserViewModel? Update(UserBindingModel model) + { + using var context = new HardwareShopDatabase(); + var user = context.Users + .FirstOrDefault(x => x.Id == model.Id); + if (user == null) + { + return null; + } + user.Update(model); + context.SaveChanges(); + return user.GetViewModel; + } + } +} \ No newline at end of file diff --git a/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/BuildStorage.cs b/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/BuildStorage.cs index 10e0f11..ce43dae 100644 --- a/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/BuildStorage.cs +++ b/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/BuildStorage.cs @@ -1,11 +1,11 @@ using HardwareShopContracts.BindingModels; using HardwareShopContracts.SearchModels; +using HardwareShopContracts.StoragesContracts; using HardwareShopContracts.ViewModels; -using HardwareShopDatabaseImplement; using HardwareShopDatabaseImplement.Models.Worker; using Microsoft.EntityFrameworkCore; -namespace HardwareShopContracts.StoragesContracts +namespace HardwareShopDatabaseImplement.Implements.Worker { public class BuildStorage : IBuildStorage { diff --git a/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/CommentStorage.cs b/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/CommentStorage.cs index 2f92b6f..84d61a9 100644 --- a/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/CommentStorage.cs +++ b/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/CommentStorage.cs @@ -1,11 +1,11 @@ using HardwareShopContracts.BindingModels; using HardwareShopContracts.SearchModels; +using HardwareShopContracts.StoragesContracts; using HardwareShopContracts.ViewModels; -using HardwareShopDatabaseImplement; using HardwareShopDatabaseImplement.Models.Worker; using Microsoft.EntityFrameworkCore; -namespace HardwareShopContracts.StoragesContracts +namespace HardwareShopDatabaseImplement.Implements.Worker { public class CommentStorage : ICommentStorage { diff --git a/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/PurchaseStorage.cs b/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/PurchaseStorage.cs index 562c24e..428a796 100644 --- a/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/PurchaseStorage.cs +++ b/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/PurchaseStorage.cs @@ -1,12 +1,11 @@ using HardwareShopContracts.BindingModels; using HardwareShopContracts.SearchModels; +using HardwareShopContracts.StoragesContracts; using HardwareShopContracts.ViewModels; -using HardwareShopDatabaseImplement; -using HardwareShopDatabaseImplement.Models.Storekeeper; using HardwareShopDatabaseImplement.Models.Worker; using Microsoft.EntityFrameworkCore; -namespace HardwareShopContracts.StoragesContracts +namespace HardwareShopDatabaseImplement.Implements.Worker { public class PurchaseStorage : IPurchaseStorage { From 18fc1b0e67de989541ff72a8b9254f2b54cd8425 Mon Sep 17 00:00:00 2001 From: dasha Date: Sat, 1 Apr 2023 21:43:08 +0400 Subject: [PATCH 2/4] =?UTF-8?q?=D0=91=D0=B8=D0=B7=D0=BD=D0=B5=D1=81-=D0=BB?= =?UTF-8?q?=D0=BE=D0=B3=D0=B8=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Storekeeper/ComponentLogic.cs | 122 +++++++++++++++++ .../BusinessLogics/Storekeeper/GoodLogic.cs | 122 +++++++++++++++++ .../BusinessLogics/Storekeeper/OrderLogic.cs | 124 +++++++++++++++++ .../BusinessLogics/UserLogic.cs | 128 ++++++++++++++++++ .../HardwareShopBusinessLogic.csproj | 4 - 5 files changed, 496 insertions(+), 4 deletions(-) create mode 100644 HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Storekeeper/ComponentLogic.cs create mode 100644 HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Storekeeper/GoodLogic.cs create mode 100644 HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Storekeeper/OrderLogic.cs create mode 100644 HardwareShop/HardwareShopBusinessLogic/BusinessLogics/UserLogic.cs diff --git a/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Storekeeper/ComponentLogic.cs b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Storekeeper/ComponentLogic.cs new file mode 100644 index 0000000..e24794f --- /dev/null +++ b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Storekeeper/ComponentLogic.cs @@ -0,0 +1,122 @@ +using HardwareShopContracts.BindingModels; +using HardwareShopContracts.BuisnessLogicsContracts; +using HardwareShopContracts.SearchModels; +using HardwareShopContracts.StoragesContracts; +using HardwareShopContracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace HardwareShopBusinessLogic.BusinessLogics.Storekeeper +{ + public class ComponentLogic : IComponentLogic + { + private readonly ILogger _logger; + private readonly IComponentStorage _componentStorage; + public ComponentLogic(ILogger logger, IComponentStorage componentStorage) + { + _logger = logger; + _componentStorage = componentStorage; + } + public bool Create(ComponentBindingModel model) + { + CheckModel(model); + if (_componentStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Delete(ComponentBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id: {Id}", model.Id); + if (_componentStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + public ComponentViewModel? ReadElement(ComponentSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. ComponentName: {ComponentName}. Id: {Id}", + model.ComponentName, model.Id); + var element = _componentStorage.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(ComponentSearchModel? model) + { + _logger.LogInformation("ReadList. ComponentName: {ComponentName}. Id: {Id}", + model?.ComponentName, model?.Id); + var list = model == null ? _componentStorage.GetFullList() : + _componentStorage.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(ComponentBindingModel model) + { + CheckModel(model); + if (_componentStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + private void CheckModel(ComponentBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.ComponentName)) + { + throw new ArgumentNullException("Нет названия комплектующего", nameof(model.ComponentName)); + } + if (model.Cost <= 0) + { + throw new ArgumentNullException("Цена комплектующего должна быть больше 0", + nameof(model.Cost)); + } + if (model.UserId < 0) + { + throw new ArgumentNullException("Некорректный идентификатор у пользователя", + nameof(model.UserId)); + } + _logger.LogInformation("Component. ComponentName: {ComponentName}. Cost: {Cost}. Id: {Id}", + model.ComponentName, model.Cost, model.Id); + var element = _componentStorage.GetElement(new ComponentSearchModel + { + ComponentName = model.ComponentName + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Комплектующее с таким названием уже есть"); + } + } + } +} diff --git a/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Storekeeper/GoodLogic.cs b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Storekeeper/GoodLogic.cs new file mode 100644 index 0000000..2120a54 --- /dev/null +++ b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Storekeeper/GoodLogic.cs @@ -0,0 +1,122 @@ +using HardwareShopContracts.BindingModels; +using HardwareShopContracts.BuisnessLogicsContracts; +using HardwareShopContracts.SearchModels; +using HardwareShopContracts.StoragesContracts; +using HardwareShopContracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace HardwareShopBusinessLogic.BusinessLogics.Storekeeper +{ + public class GoodLogic : IGoodLogic + { + private readonly ILogger _logger; + private readonly IGoodStorage _goodStorage; + public GoodLogic(ILogger logger, IGoodStorage goodStorage) + { + _logger = logger; + _goodStorage = goodStorage; + } + public bool Create(GoodBindingModel model) + { + CheckModel(model); + if (_goodStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Delete(GoodBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id: {Id}", model.Id); + if (_goodStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + public GoodViewModel? ReadElement(GoodSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. GoodName: {GoodName}. Id: {Id}", + model.GoodName, model.Id); + var element = _goodStorage.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(GoodSearchModel? model) + { + _logger.LogInformation("ReadList. GoodName: {GoodName}. Id: {Id}", + model?.GoodName, model?.Id); + var list = model == null ? _goodStorage.GetFullList() : + _goodStorage.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(GoodBindingModel model) + { + CheckModel(model); + if (_goodStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + private void CheckModel(GoodBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.GoodName)) + { + throw new ArgumentNullException("Нет названия товара", nameof(model.GoodName)); + } + if (model.Price <= 0) + { + throw new ArgumentNullException("Цена товара должна быть больше 0", + nameof(model.Price)); + } + if (model.UserId < 0) + { + throw new ArgumentNullException("Некорректный идентификатор у пользователя", + nameof(model.Price)); + } + _logger.LogInformation("Good. GoodName: {GoodName}. Price: {Price}. Id: {Id}", + model.GoodName, model.Price, model.Id); + var element = _goodStorage.GetElement(new GoodSearchModel + { + GoodName = model.GoodName + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Товар с таким названием уже есть"); + } + } + } +} diff --git a/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Storekeeper/OrderLogic.cs b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Storekeeper/OrderLogic.cs new file mode 100644 index 0000000..5410cc0 --- /dev/null +++ b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Storekeeper/OrderLogic.cs @@ -0,0 +1,124 @@ +using HardwareShopContracts.BindingModels; +using HardwareShopContracts.BuisnessLogicsContracts; +using HardwareShopContracts.SearchModels; +using HardwareShopContracts.StoragesContracts; +using HardwareShopContracts.ViewModels; +using HardwareShopDataModels.Enums; +using Microsoft.Extensions.Logging; + +namespace HardwareShopBusinessLogic.BusinessLogics.Storekeeper +{ + public class OrderLogic : IOrderLogic + { + private readonly ILogger _logger; + private readonly IOrderStorage _orderStorage; + + public OrderLogic(ILogger logger, IOrderStorage orderStorage) + { + _logger = logger; + _orderStorage = orderStorage; + } + public bool CreateOrder(OrderBindingModel model) + { + CheckModel(model); + if (model.Status != OrderStatus.Неизвестен) + { + _logger.LogWarning("Insert operation failed. Order status incorrect."); + return false; + } + model.Status = OrderStatus.Принят; + if (_orderStorage.Insert(model) == null) + { + model.Status = OrderStatus.Неизвестен; + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public List? ReadList(OrderSearchModel? model) + { + _logger.LogInformation("Order. OrderId: {Id}", model?.Id); + var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count: {Count}", list.Count); + return list; + } + + private bool StatusUpdate(OrderBindingModel model, OrderStatus newStatus) + { + var viewModel = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id }); + if (viewModel == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (viewModel.Status + 1 != newStatus) + { + _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Order status incorrect."); + return false; + } + model.Status = newStatus; + if (model.Status == OrderStatus.Готов) model.DateImplement = DateTime.Now; + else + { + model.DateImplement = viewModel.DateImplement; + } + CheckModel(model, false); + if (_orderStorage.Update(model) == null) + { + model.Status--; + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + private void CheckModel(OrderBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (model.GoodId < 0) + { + throw new ArgumentNullException("Некорректный идентификатор у товара", nameof(model.GoodId)); + } + if (model.UserId < 0) + { + throw new ArgumentNullException("Некорректный идентификатор у пользователя", nameof(model.UserId)); + } + if (model.Count <= 0) + { + throw new ArgumentNullException("Количество товара в заказе должно быть больше 0", nameof(model.Count)); + } + if (model.Sum <= 0) + { + throw new ArgumentNullException("Сумма заказа должна быть больше 0", nameof(model.Sum)); + } + _logger.LogInformation("Order. OrderId: {Id}. Sum: {Sum}. GoodId: {GoodId}", model.Id, model.Sum, model.GoodId); + } + + public bool TakeOrderInWork(OrderBindingModel model) + { + return StatusUpdate(model, OrderStatus.Выполняется); + } + + public bool FinishOrder(OrderBindingModel model) + { + return StatusUpdate(model, OrderStatus.Готов); + } + + public bool DeliveryOrder(OrderBindingModel model) + { + return StatusUpdate(model, OrderStatus.Выдан); + } + } +} diff --git a/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/UserLogic.cs b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/UserLogic.cs new file mode 100644 index 0000000..de4e507 --- /dev/null +++ b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/UserLogic.cs @@ -0,0 +1,128 @@ +using HardwareShopContracts.BindingModels; +using HardwareShopContracts.BusinessLogicsContracts; +using HardwareShopContracts.SearchModels; +using HardwareShopContracts.StoragesContracts; +using HardwareShopContracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace HardwareShopBusinessLogic.BusinessLogics +{ + public class UserLogic : IUserLogic + { + private readonly ILogger _logger; + private readonly IUserStorage _userStorage; + public UserLogic(ILogger logger, IUserStorage userStorage) + { + _logger = logger; + _userStorage = userStorage; + } + public bool Create(UserBindingModel model) + { + CheckModel(model); + if (_userStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Delete(UserBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id: {Id}", model.Id); + if (_userStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + public UserViewModel? ReadElement(UserSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. Login: {Login}. Email: {Email}. Id: {Id}.", + model.Login, model.Email, model.Id); + var element = _userStorage.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(UserSearchModel? model) + { + _logger.LogInformation("ReadList. Login: {Login}. Email: {Email}. Id: {Id}.", + model?.Login, model?.Email, model?.Id); + var list = model == null ? _userStorage.GetFullList() : + _userStorage.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(UserBindingModel model) + { + CheckModel(model); + if (_userStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + private void CheckModel(UserBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.Login)) + { + throw new ArgumentNullException("Нет логина пользователя", nameof(model.Login)); + } + if (string.IsNullOrEmpty(model.Email)) + { + throw new ArgumentNullException("Нет почты пользователя", nameof(model.Email)); + } + if (string.IsNullOrEmpty(model.Password)) + { + throw new ArgumentNullException("Нет пароля пользователя", nameof(model.Password)); + } + _logger.LogInformation("User. Login: {Login}. Email: {Email}. Id: {Id}", + model.Login, model.Email, model.Id); + var elementLogin = _userStorage.GetElement(new UserSearchModel + { + Login = model.Login + }); + var elementEmail = _userStorage.GetElement(new UserSearchModel + { + Email = model.Email + }); + if (elementEmail != null && elementEmail.Id != model.Id) + { + throw new InvalidOperationException("Клиент с такой почтой уже есть"); + } + if (elementLogin != null && elementLogin.Id != model.Id) + { + throw new InvalidOperationException("Клиент с таким логином уже есть"); + } + } + } +} diff --git a/HardwareShop/HardwareShopBusinessLogic/HardwareShopBusinessLogic.csproj b/HardwareShop/HardwareShopBusinessLogic/HardwareShopBusinessLogic.csproj index b47f27c..c758485 100644 --- a/HardwareShop/HardwareShopBusinessLogic/HardwareShopBusinessLogic.csproj +++ b/HardwareShop/HardwareShopBusinessLogic/HardwareShopBusinessLogic.csproj @@ -6,10 +6,6 @@ enable - - - - From 289697c26de50be186285302cb8611e99f18607d Mon Sep 17 00:00:00 2001 From: dasha Date: Sat, 1 Apr 2023 22:06:59 +0400 Subject: [PATCH 3/4] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D1=80=D0=B5=D1=81=D1=82-=D0=BF=D1=80?= =?UTF-8?q?=D0=BE=D0=B5=D0=BA=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../HardwareShopRestApi.csproj | 24 ++++++++ HardwareShop/HardwareShopRestApi/Program.cs | 58 +++++++++++++++++++ .../Properties/launchSettings.json | 31 ++++++++++ .../appsettings.Development.json | 8 +++ .../HardwareShopRestApi/appsettings.json | 9 +++ HardwareShop/HardwareShopRestApi/nlog.config | 15 +++++ HardwareShop/HardwareShopView.sln | 18 +++--- 7 files changed, 154 insertions(+), 9 deletions(-) create mode 100644 HardwareShop/HardwareShopRestApi/HardwareShopRestApi.csproj create mode 100644 HardwareShop/HardwareShopRestApi/Program.cs create mode 100644 HardwareShop/HardwareShopRestApi/Properties/launchSettings.json create mode 100644 HardwareShop/HardwareShopRestApi/appsettings.Development.json create mode 100644 HardwareShop/HardwareShopRestApi/appsettings.json create mode 100644 HardwareShop/HardwareShopRestApi/nlog.config diff --git a/HardwareShop/HardwareShopRestApi/HardwareShopRestApi.csproj b/HardwareShop/HardwareShopRestApi/HardwareShopRestApi.csproj new file mode 100644 index 0000000..40effd1 --- /dev/null +++ b/HardwareShop/HardwareShopRestApi/HardwareShopRestApi.csproj @@ -0,0 +1,24 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + + + + + + + + diff --git a/HardwareShop/HardwareShopRestApi/Program.cs b/HardwareShop/HardwareShopRestApi/Program.cs new file mode 100644 index 0000000..0a34d12 --- /dev/null +++ b/HardwareShop/HardwareShopRestApi/Program.cs @@ -0,0 +1,58 @@ +using HardwareShopBusinessLogic.BusinessLogics; +using HardwareShopBusinessLogic.BusinessLogics.Storekeeper; +using HardwareShopContracts.BuisnessLogicsContracts; +using HardwareShopContracts.BusinessLogicsContracts; +using HardwareShopContracts.StoragesContracts; +using HardwareShopDatabaseImplement.Implements; +using HardwareShopDatabaseImplement.Implements.Storekeeper; +using HardwareShopDatabaseImplement.Implements.Worker; +using Microsoft.OpenApi.Models; +using NLog.Extensions.Logging; + +var builder = WebApplication.CreateBuilder(args); + +builder.Logging.SetMinimumLevel(LogLevel.Information); +builder.Logging.AddNLog("nlog.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.AddTransient(); + +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); + +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 = "HardwareShopRestApi", 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", "HardwareShopRestApi v1")); +} + +app.UseHttpsRedirection(); + +app.UseAuthorization(); + +app.MapControllers(); + +app.Run(); diff --git a/HardwareShop/HardwareShopRestApi/Properties/launchSettings.json b/HardwareShop/HardwareShopRestApi/Properties/launchSettings.json new file mode 100644 index 0000000..0750b89 --- /dev/null +++ b/HardwareShop/HardwareShopRestApi/Properties/launchSettings.json @@ -0,0 +1,31 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:45313", + "sslPort": 44367 + } + }, + "profiles": { + "HardwareShopRestApi": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7205;http://localhost:5205", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/HardwareShop/HardwareShopRestApi/appsettings.Development.json b/HardwareShop/HardwareShopRestApi/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/HardwareShop/HardwareShopRestApi/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/HardwareShop/HardwareShopRestApi/appsettings.json b/HardwareShop/HardwareShopRestApi/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/HardwareShop/HardwareShopRestApi/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/HardwareShop/HardwareShopRestApi/nlog.config b/HardwareShop/HardwareShopRestApi/nlog.config new file mode 100644 index 0000000..85797a7 --- /dev/null +++ b/HardwareShop/HardwareShopRestApi/nlog.config @@ -0,0 +1,15 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/HardwareShop/HardwareShopView.sln b/HardwareShop/HardwareShopView.sln index cbb23be..7f6fc08 100644 --- a/HardwareShop/HardwareShopView.sln +++ b/HardwareShop/HardwareShopView.sln @@ -3,15 +3,15 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.3.32929.385 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HardwareShopView", "HardwareShopView\HardwareShopView.csproj", "{14E5377A-FF6B-4FBA-B18A-5F6EB513123C}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HardwareShopDataModels", "HardwareShopDataModels\HardwareShopDataModels.csproj", "{1589524F-918D-40B8-A44B-8C7FCABFD29A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HardwareShopDataModels", "HardwareShopDataModels\HardwareShopDataModels.csproj", "{1589524F-918D-40B8-A44B-8C7FCABFD29A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HardwareShopContracts", "HardwareShopContracts\HardwareShopContracts.csproj", "{91B12343-FCFB-4B7D-BBB8-C6A2C9D73996}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HardwareShopContracts", "HardwareShopContracts\HardwareShopContracts.csproj", "{91B12343-FCFB-4B7D-BBB8-C6A2C9D73996}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HardwareShopBusinessLogic", "HardwareShopBusinessLogic\HardwareShopBusinessLogic.csproj", "{3B96A73E-3385-4712-A7C4-9D67D424CE43}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HardwareShopBusinessLogic", "HardwareShopBusinessLogic\HardwareShopBusinessLogic.csproj", "{3B96A73E-3385-4712-A7C4-9D67D424CE43}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HardwareShopDatabaseImplement", "HardwareShopDatabaseImplement\HardwareShopDatabaseImplement.csproj", "{1E5156F6-1F67-497C-A660-8AC61BC451BC}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HardwareShopDatabaseImplement", "HardwareShopDatabaseImplement\HardwareShopDatabaseImplement.csproj", "{1E5156F6-1F67-497C-A660-8AC61BC451BC}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HardwareShopRestApi", "HardwareShopRestApi\HardwareShopRestApi.csproj", "{623AF3E7-85DA-4DB7-BC0E-8D669CEE9402}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -19,10 +19,6 @@ Global Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {14E5377A-FF6B-4FBA-B18A-5F6EB513123C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {14E5377A-FF6B-4FBA-B18A-5F6EB513123C}.Debug|Any CPU.Build.0 = Debug|Any CPU - {14E5377A-FF6B-4FBA-B18A-5F6EB513123C}.Release|Any CPU.ActiveCfg = Release|Any CPU - {14E5377A-FF6B-4FBA-B18A-5F6EB513123C}.Release|Any CPU.Build.0 = Release|Any CPU {1589524F-918D-40B8-A44B-8C7FCABFD29A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {1589524F-918D-40B8-A44B-8C7FCABFD29A}.Debug|Any CPU.Build.0 = Debug|Any CPU {1589524F-918D-40B8-A44B-8C7FCABFD29A}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -39,6 +35,10 @@ Global {1E5156F6-1F67-497C-A660-8AC61BC451BC}.Debug|Any CPU.Build.0 = Debug|Any CPU {1E5156F6-1F67-497C-A660-8AC61BC451BC}.Release|Any CPU.ActiveCfg = Release|Any CPU {1E5156F6-1F67-497C-A660-8AC61BC451BC}.Release|Any CPU.Build.0 = Release|Any CPU + {623AF3E7-85DA-4DB7-BC0E-8D669CEE9402}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {623AF3E7-85DA-4DB7-BC0E-8D669CEE9402}.Debug|Any CPU.Build.0 = Debug|Any CPU + {623AF3E7-85DA-4DB7-BC0E-8D669CEE9402}.Release|Any CPU.ActiveCfg = Release|Any CPU + {623AF3E7-85DA-4DB7-BC0E-8D669CEE9402}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE From cbbd31f09618cdc833042ca507920e5b548b6245 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=BE=D0=BB=D0=B0=D0=B9?= Date: Sat, 1 Apr 2023 22:18:09 +0400 Subject: [PATCH 4/4] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=BA=D0=BE=D0=BD=D1=82=D1=80=D0=BE=D0=BB=D0=BB=D0=B5?= =?UTF-8?q?=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/ClientController.cs | 55 +++++++++++++++++++ .../HardwareShopRestApi.csproj | 4 -- 2 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 HardwareShop/HardwareShopRestApi/Controllers/ClientController.cs diff --git a/HardwareShop/HardwareShopRestApi/Controllers/ClientController.cs b/HardwareShop/HardwareShopRestApi/Controllers/ClientController.cs new file mode 100644 index 0000000..8041bad --- /dev/null +++ b/HardwareShop/HardwareShopRestApi/Controllers/ClientController.cs @@ -0,0 +1,55 @@ +using HardwareShopContracts.BindingModels; +using HardwareShopContracts.BusinessLogicsContracts; +using HardwareShopContracts.SearchModels; +using HardwareShopContracts.ViewModels; +using Microsoft.AspNetCore.Mvc; + +namespace HardwareShopRestApi.Controllers +{ + [Route("api/[controller]/[action]")] + [ApiController] + public class ClientController : Controller + { + private readonly ILogger _logger; + + private readonly IUserLogic _logic; + + public ClientController(IUserLogic logic, ILogger logger) + { + _logger = logger; + _logic = logic; + } + + [HttpGet] + public UserViewModel? Login(string login, string password) + { + try + { + return _logic.ReadElement(new UserSearchModel + { + Email = login, + Password = password + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка входа в систему"); + throw; + } + } + + [HttpPost] + public void Register(UserBindingModel model) + { + try + { + _logic.Create(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка регистрации"); + throw; + } + } + } +} \ No newline at end of file diff --git a/HardwareShop/HardwareShopRestApi/HardwareShopRestApi.csproj b/HardwareShop/HardwareShopRestApi/HardwareShopRestApi.csproj index 40effd1..8e97e82 100644 --- a/HardwareShop/HardwareShopRestApi/HardwareShopRestApi.csproj +++ b/HardwareShop/HardwareShopRestApi/HardwareShopRestApi.csproj @@ -11,10 +11,6 @@ - - - -