From 10ee9301d1f0dcaf783fa822e867f9ea75634f99 Mon Sep 17 00:00:00 2001 From: Yourdax Date: Sat, 4 May 2024 21:05:32 +0400 Subject: [PATCH] =?UTF-8?q?=D0=92=D1=80=D0=BE=D0=B4=D0=B5=20=D0=BA=D0=B0?= =?UTF-8?q?=D0=BA=20=D0=BF=D0=BE=D1=81=D0=BB=D0=B5=D0=B4=D0=BD=D0=B8=D0=B5?= =?UTF-8?q?=20=D1=84=D0=B8=D0=BA=D1=81=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/CardLogic.cs | 8 +- .../BusinessLogics/DrinkLogic.cs | 113 ++++++++++++++++++ .../BusinessLogicContracts/ICardLogic.cs | 1 - .../BusinessLogicContracts/IDrinkLogic.cs | 3 +- .../SearchModels/CardSearchModel.cs | 4 +- .../StorageContracts/ICardStorage.cs | 1 - .../StorageContracts/IDrinkStorage.cs | 3 +- .../Implements/CardStorage.cs | 113 ++++++++++++++++++ .../Implements/DrinkStorage.cs | 15 ++- ... 20240504170055_InitialCreate.Designer.cs} | 2 +- ...ate.cs => 20240504170055_InitialCreate.cs} | 0 .../Models/Card.cs | 1 + .../Models/Drink.cs | 5 + 13 files changed, 256 insertions(+), 13 deletions(-) create mode 100644 DiningRoom/DiningRoomBusinessLogic/BusinessLogics/DrinkLogic.cs create mode 100644 DiningRoom/DiningRoomDatabaseImplement/Implements/CardStorage.cs rename DiningRoom/DiningRoomDatabaseImplement/Migrations/{20240503210711_InitialCreate.Designer.cs => 20240504170055_InitialCreate.Designer.cs} (99%) rename DiningRoom/DiningRoomDatabaseImplement/Migrations/{20240503210711_InitialCreate.cs => 20240504170055_InitialCreate.cs} (100%) diff --git a/DiningRoom/DiningRoomBusinessLogic/BusinessLogics/CardLogic.cs b/DiningRoom/DiningRoomBusinessLogic/BusinessLogics/CardLogic.cs index 2be5688..ec2c87b 100644 --- a/DiningRoom/DiningRoomBusinessLogic/BusinessLogics/CardLogic.cs +++ b/DiningRoom/DiningRoomBusinessLogic/BusinessLogics/CardLogic.cs @@ -75,13 +75,9 @@ namespace DiningRoomBusinessLogic.BusinessLogics return true; } - public bool ConnectCardDrink(CardBindingModel model) - { - _logger.LogInformation("Connect Card {rId} with drink {aId}", model.Id, model.DrinkId); - return _CardStorage.ConnectCardDrink(model); - } + - public bool Delete(CardBindingModel model) + public bool Delete(CardBindingModel model) { CheckModel(model, false); _logger.LogInformation("Delete. Id:{Id}", model.Id); diff --git a/DiningRoom/DiningRoomBusinessLogic/BusinessLogics/DrinkLogic.cs b/DiningRoom/DiningRoomBusinessLogic/BusinessLogics/DrinkLogic.cs new file mode 100644 index 0000000..b5ecc53 --- /dev/null +++ b/DiningRoom/DiningRoomBusinessLogic/BusinessLogics/DrinkLogic.cs @@ -0,0 +1,113 @@ +using DiningRoomContracts.BindingModels; +using DiningRoomContracts.BusinessLogicContracts; +using DiningRoomContracts.SearchModels; +using DiningRoomContracts.StorageContracts; +using DiningRoomContracts.ViewModels; +using Microsoft.Extensions.Logging; + + +namespace DiningRoomBusinessLogic.BusinessLogics +{ + public class DrinkLogic : IDrinkLogic + { + private readonly ILogger _logger; + private readonly IDrinkStorage _DrinkStorage; + public DrinkLogic(ILogger logger, IDrinkStorage DrinkStorage) + { + _logger = logger; + _DrinkStorage = DrinkStorage; + } + public List? ReadList(DrinkSearchModel? model) + { + _logger.LogInformation("ReadList. DrinkName:{DrinkName}. Id:{ Id}", model?.DrinkName, model?.Id); + var list = model == null ? _DrinkStorage.GetFullList() : _DrinkStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + public DrinkViewModel? ReadElement(DrinkSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. DrinkName:{DrinkName}. Id:{ Id}", model.DrinkName, model.Id); + var element = _DrinkStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + return element; + } + public bool Create(DrinkBindingModel model) + { + CheckModel(model); + if (_DrinkStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + public bool Update(DrinkBindingModel model) + { + CheckModel(model); + if (_DrinkStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(DrinkBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_DrinkStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + private void CheckModel(DrinkBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.DrinkName)) + { + throw new ArgumentNullException("Нет названия напитка", nameof(model.DrinkName)); + } + if (model.Cost <= 0) + { + throw new ArgumentNullException("Цена напитка должна быть больше 0", nameof(model.Cost)); + } + _logger.LogInformation("Drink. DrinkName:{DrinkName}. Cost:{ Cost}. Id: { Id}", model.DrinkName, model.Cost, model.Id); + var element = _DrinkStorage.GetElement(new DrinkSearchModel + { + DrinkName = model.DrinkName + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Напиток с таким названием уже есть"); + } + } + public bool ConnectCardDrink(DrinkBindingModel model) + { + _logger.LogInformation("Connect Card {rId} with drink {aId}", model.Id, model.CardId); + return _DrinkStorage.ConnectCardDrink(model); + } + } +} diff --git a/DiningRoom/DiningRoomContracts/BusinessLogicContracts/ICardLogic.cs b/DiningRoom/DiningRoomContracts/BusinessLogicContracts/ICardLogic.cs index a79f2fa..656c298 100644 --- a/DiningRoom/DiningRoomContracts/BusinessLogicContracts/ICardLogic.cs +++ b/DiningRoom/DiningRoomContracts/BusinessLogicContracts/ICardLogic.cs @@ -16,6 +16,5 @@ namespace DiningRoomContracts.BusinessLogicContracts bool Create(CardBindingModel model); bool Update(CardBindingModel model); bool Delete(CardBindingModel model); - bool ConnectCardDrink(CardBindingModel model); } } diff --git a/DiningRoom/DiningRoomContracts/BusinessLogicContracts/IDrinkLogic.cs b/DiningRoom/DiningRoomContracts/BusinessLogicContracts/IDrinkLogic.cs index 0fdb4db..1a051af 100644 --- a/DiningRoom/DiningRoomContracts/BusinessLogicContracts/IDrinkLogic.cs +++ b/DiningRoom/DiningRoomContracts/BusinessLogicContracts/IDrinkLogic.cs @@ -15,5 +15,6 @@ namespace DiningRoomContracts.BusinessLogicContracts bool Update(DrinkBindingModel Model); bool Delete(DrinkBindingModel Model); - } + bool ConnectCardDrink(DrinkBindingModel model); + } } diff --git a/DiningRoom/DiningRoomContracts/SearchModels/CardSearchModel.cs b/DiningRoom/DiningRoomContracts/SearchModels/CardSearchModel.cs index c71ad3b..5e8f068 100644 --- a/DiningRoom/DiningRoomContracts/SearchModels/CardSearchModel.cs +++ b/DiningRoom/DiningRoomContracts/SearchModels/CardSearchModel.cs @@ -6,7 +6,9 @@ public int? UserId { get; set; } public DateTime? DateCardCreate { get; set; } - public int? DrinkId { get; set; } + public DateTime? DateFrom { get; set; } + public DateTime? DateTo { get; set; } + public int? DrinkId { get; set; } public string? Cardname { get; set; } } diff --git a/DiningRoom/DiningRoomContracts/StorageContracts/ICardStorage.cs b/DiningRoom/DiningRoomContracts/StorageContracts/ICardStorage.cs index ac75c37..a26452b 100644 --- a/DiningRoom/DiningRoomContracts/StorageContracts/ICardStorage.cs +++ b/DiningRoom/DiningRoomContracts/StorageContracts/ICardStorage.cs @@ -17,6 +17,5 @@ namespace DiningRoomContracts.StorageContracts CardViewModel? Update(CardBindingModel Model); CardViewModel? Delete(CardBindingModel Model); - bool ConnectCardDrink(CardBindingModel model); } } diff --git a/DiningRoom/DiningRoomContracts/StorageContracts/IDrinkStorage.cs b/DiningRoom/DiningRoomContracts/StorageContracts/IDrinkStorage.cs index d955ae9..2179e7a 100644 --- a/DiningRoom/DiningRoomContracts/StorageContracts/IDrinkStorage.cs +++ b/DiningRoom/DiningRoomContracts/StorageContracts/IDrinkStorage.cs @@ -17,5 +17,6 @@ namespace DiningRoomContracts.StorageContracts DrinkViewModel? Update(DrinkBindingModel Model); DrinkViewModel? Delete(DrinkBindingModel Model); - } + bool ConnectCardDrink(DrinkBindingModel model); + } } diff --git a/DiningRoom/DiningRoomDatabaseImplement/Implements/CardStorage.cs b/DiningRoom/DiningRoomDatabaseImplement/Implements/CardStorage.cs new file mode 100644 index 0000000..b41dde9 --- /dev/null +++ b/DiningRoom/DiningRoomDatabaseImplement/Implements/CardStorage.cs @@ -0,0 +1,113 @@ +using DiningRoomContracts.BindingModels; +using DiningRoomContracts.SearchModels; +using DiningRoomContracts.StorageContracts; +using DiningRoomContracts.ViewModels; +using DiningRoomDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DiningRoomDatabaseImplement.Implements +{ + public class CardStorage : ICardStorage + { + //id пользователя учитывается в GetFilteredList + public List GetFullList() + { + using var context = new DiningRoomDatabase(); + return context.Cards + .Include(x => x.Drinks) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(CardSearchModel model) { + using var context = new DiningRoomDatabase(); + + //сортировка по дате создания заявки + if (model.DateFrom.HasValue && model.DateTo.HasValue) + { + return context.Cards + .Where(x => (x.UserId == model.UserId) && (x.DateCardCreate >= model.DateFrom && x.DateCardCreate <= model.DateTo)) + .Include(x => x.Drinks) + .Select(x => x.GetViewModel) + .ToList(); + } + //возвращение просто всех заявок пользователя (замена GetFullList) + return context.Cards + .Where(x => x.UserId == model.UserId) + .Include(x => x.Drinks) + .Select(x => x.GetViewModel) + .ToList(); + } + + //Поиск только по id, потому что другие поля не уникальные - нет смысла искать 1 элемент + public CardViewModel? GetElement(CardSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new DiningRoomDatabase(); + return context.Cards + .Include(x => x.Drinks) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + } + + public CardViewModel? Insert(CardBindingModel model) + { + using var context = new DiningRoomDatabase(); + var newCard = Card.Create(context, model); + if (newCard == null) + { + return null; + } + context.Cards.Add(newCard); + context.SaveChanges(); + return newCard.GetViewModel; + } + + public CardViewModel? Update(CardBindingModel model) + { + using var context = new DiningRoomDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var Card = context.Cards.FirstOrDefault(x => x.Id == model.Id); + if (Card == null) + { + return null; + } + Card.Update(model); + context.SaveChanges(); + transaction.Commit(); + return Card.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public CardViewModel? Delete(CardBindingModel model) + { + using var context = new DiningRoomDatabase(); + var Card = context.Cards + .Include(x => x.Drinks) + .FirstOrDefault(y => y.Id == model.Id); + if (Card != null) + { + context.Cards.Remove(Card); + context.SaveChanges(); + return Card.GetViewModel; + } + return null; + } + } +} diff --git a/DiningRoom/DiningRoomDatabaseImplement/Implements/DrinkStorage.cs b/DiningRoom/DiningRoomDatabaseImplement/Implements/DrinkStorage.cs index b8cc9b0..135f44a 100644 --- a/DiningRoom/DiningRoomDatabaseImplement/Implements/DrinkStorage.cs +++ b/DiningRoom/DiningRoomDatabaseImplement/Implements/DrinkStorage.cs @@ -102,5 +102,18 @@ namespace DiningRoomDatabaseImplement.Implements } return null; } - } + public bool ConnectCardDrink(DrinkBindingModel model) + { + using var context = new DiningRoomDatabase(); + var Card = context.Cards.FirstOrDefault(x => x.Id == model.Id); + var Drink = context.Drinks.FirstOrDefault(x => x.Id == model.CardId); + if (Card == null || Drink == null) + { + return false; + } + Drink.ConnectDrink(context, model); + context.SaveChanges(); + return true; + } + } } \ No newline at end of file diff --git a/DiningRoom/DiningRoomDatabaseImplement/Migrations/20240503210711_InitialCreate.Designer.cs b/DiningRoom/DiningRoomDatabaseImplement/Migrations/20240504170055_InitialCreate.Designer.cs similarity index 99% rename from DiningRoom/DiningRoomDatabaseImplement/Migrations/20240503210711_InitialCreate.Designer.cs rename to DiningRoom/DiningRoomDatabaseImplement/Migrations/20240504170055_InitialCreate.Designer.cs index f2dae02..afbc938 100644 --- a/DiningRoom/DiningRoomDatabaseImplement/Migrations/20240503210711_InitialCreate.Designer.cs +++ b/DiningRoom/DiningRoomDatabaseImplement/Migrations/20240504170055_InitialCreate.Designer.cs @@ -12,7 +12,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace DiningRoomDatabaseImplement.Migrations { [DbContext(typeof(DiningRoomDatabase))] - [Migration("20240503210711_InitialCreate")] + [Migration("20240504170055_InitialCreate")] partial class InitialCreate { /// diff --git a/DiningRoom/DiningRoomDatabaseImplement/Migrations/20240503210711_InitialCreate.cs b/DiningRoom/DiningRoomDatabaseImplement/Migrations/20240504170055_InitialCreate.cs similarity index 100% rename from DiningRoom/DiningRoomDatabaseImplement/Migrations/20240503210711_InitialCreate.cs rename to DiningRoom/DiningRoomDatabaseImplement/Migrations/20240504170055_InitialCreate.cs diff --git a/DiningRoom/DiningRoomDatabaseImplement/Models/Card.cs b/DiningRoom/DiningRoomDatabaseImplement/Models/Card.cs index adc6b01..14c5e62 100644 --- a/DiningRoom/DiningRoomDatabaseImplement/Models/Card.cs +++ b/DiningRoom/DiningRoomDatabaseImplement/Models/Card.cs @@ -44,5 +44,6 @@ namespace DiningRoomDatabaseImplement.Models UserId = UserId, DateCardCreate = DateCardCreate, }; + } } diff --git a/DiningRoom/DiningRoomDatabaseImplement/Models/Drink.cs b/DiningRoom/DiningRoomDatabaseImplement/Models/Drink.cs index 3184de7..8c98d74 100644 --- a/DiningRoom/DiningRoomDatabaseImplement/Models/Drink.cs +++ b/DiningRoom/DiningRoomDatabaseImplement/Models/Drink.cs @@ -112,5 +112,10 @@ namespace DiningRoomDatabaseImplement.Models } _drinkComponents = null; } + public void ConnectDrink(DiningRoomDatabase context, DrinkBindingModel model) + { + CardId = model.CardId; + Card = context.Cards.First(x => x.Id == model.CardId); + } } }