From 2c40048bc3793f08b632e0ebeb9bd890489544ad Mon Sep 17 00:00:00 2001 From: Yourdax Date: Tue, 30 Apr 2024 00:05:31 +0400 Subject: [PATCH] =?UTF-8?q?DataImplement=20+=20(=D0=BD=D0=B5=20=D0=BF?= =?UTF-8?q?=D0=BE=D0=BB=D0=BD=D1=8B=D0=B9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DiningRoom/DiningRoom.sln | 12 +- .../BindingModels/CardBindingModel.cs | 2 - .../BindingModels/DrinkBindingModel.cs | 3 +- .../BindingModels/OrderBindingModel.cs | 10 +- .../SearchModels/DrinkSearchModel.cs | 1 + .../SearchModels/OrderSearchModel.cs | 3 +- .../ViewModels/DrinkViewModel.cs | 4 +- .../ViewModels/OrderViewModel.cs | 11 +- .../DiningRoomDataModels/Models/ICardModel.cs | 7 +- .../Models/IDrinkModel.cs | 1 + .../Models/IOrderModel.cs | 22 +-- .../ComputerShopDatabase.cs | 37 +++++ .../DiningRoomDatabaseImplement.csproj | 24 +++ .../Implements/OrderStorage.cs | 152 ++++++++++++++++++ .../Implements/UserStorage.cs | 106 ++++++++++++ .../Models/Card.cs | 40 +++++ .../Models/Component.cs | 57 +++++++ .../Models/Drink.cs | 116 +++++++++++++ .../Models/DrinkComponent.cs | 22 +++ .../Models/Order.cs | 84 ++++++++++ .../Models/Product.cs | 101 ++++++++++++ .../Models/ProductComponent.cs | 22 +++ .../Models/User.cs | 62 +++++++ README.md | 1 + 24 files changed, 864 insertions(+), 36 deletions(-) create mode 100644 DiningRoom/DiningRoomDatabaseImplement/ComputerShopDatabase.cs create mode 100644 DiningRoom/DiningRoomDatabaseImplement/DiningRoomDatabaseImplement.csproj create mode 100644 DiningRoom/DiningRoomDatabaseImplement/Implements/OrderStorage.cs create mode 100644 DiningRoom/DiningRoomDatabaseImplement/Implements/UserStorage.cs create mode 100644 DiningRoom/DiningRoomDatabaseImplement/Models/Card.cs create mode 100644 DiningRoom/DiningRoomDatabaseImplement/Models/Component.cs create mode 100644 DiningRoom/DiningRoomDatabaseImplement/Models/Drink.cs create mode 100644 DiningRoom/DiningRoomDatabaseImplement/Models/DrinkComponent.cs create mode 100644 DiningRoom/DiningRoomDatabaseImplement/Models/Order.cs create mode 100644 DiningRoom/DiningRoomDatabaseImplement/Models/Product.cs create mode 100644 DiningRoom/DiningRoomDatabaseImplement/Models/ProductComponent.cs create mode 100644 DiningRoom/DiningRoomDatabaseImplement/Models/User.cs diff --git a/DiningRoom/DiningRoom.sln b/DiningRoom/DiningRoom.sln index 0015a90..6b723aa 100644 --- a/DiningRoom/DiningRoom.sln +++ b/DiningRoom/DiningRoom.sln @@ -3,11 +3,13 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.9.34728.123 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DiningRoomView", "DiningRoomView\DiningRoomView.csproj", "{611756F0-9DBA-41CB-ABD1-15A094CCAFFC}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DiningRoomView", "DiningRoomView\DiningRoomView.csproj", "{611756F0-9DBA-41CB-ABD1-15A094CCAFFC}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DiningRoomContracts", "DiningRoomContracts\DiningRoomContracts.csproj", "{432450B8-A672-40A6-98D5-FA0F3FD8BF78}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DiningRoomContracts", "DiningRoomContracts\DiningRoomContracts.csproj", "{432450B8-A672-40A6-98D5-FA0F3FD8BF78}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DiningRoomDataModels", "DiningRoomDataModels\DiningRoomDataModels.csproj", "{49474CED-88C8-440C-AAFA-5DCCF868D03F}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DiningRoomDataModels", "DiningRoomDataModels\DiningRoomDataModels.csproj", "{49474CED-88C8-440C-AAFA-5DCCF868D03F}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DiningRoomDatabaseImplement", "DiningRoomDatabaseImplement\DiningRoomDatabaseImplement.csproj", "{348BD2CC-C93D-42E8-A890-FC7807476625}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -27,6 +29,10 @@ Global {49474CED-88C8-440C-AAFA-5DCCF868D03F}.Debug|Any CPU.Build.0 = Debug|Any CPU {49474CED-88C8-440C-AAFA-5DCCF868D03F}.Release|Any CPU.ActiveCfg = Release|Any CPU {49474CED-88C8-440C-AAFA-5DCCF868D03F}.Release|Any CPU.Build.0 = Release|Any CPU + {348BD2CC-C93D-42E8-A890-FC7807476625}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {348BD2CC-C93D-42E8-A890-FC7807476625}.Debug|Any CPU.Build.0 = Debug|Any CPU + {348BD2CC-C93D-42E8-A890-FC7807476625}.Release|Any CPU.ActiveCfg = Release|Any CPU + {348BD2CC-C93D-42E8-A890-FC7807476625}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/DiningRoom/DiningRoomContracts/BindingModels/CardBindingModel.cs b/DiningRoom/DiningRoomContracts/BindingModels/CardBindingModel.cs index 924946d..07bf7ac 100644 --- a/DiningRoom/DiningRoomContracts/BindingModels/CardBindingModel.cs +++ b/DiningRoom/DiningRoomContracts/BindingModels/CardBindingModel.cs @@ -15,7 +15,5 @@ namespace DiningRoomContracts.BindingModels public int UserId { get; set; } public string CardName { get; set; } = string.Empty; - - public Dictionary DrinkCard { get; set; } = new(); } } diff --git a/DiningRoom/DiningRoomContracts/BindingModels/DrinkBindingModel.cs b/DiningRoom/DiningRoomContracts/BindingModels/DrinkBindingModel.cs index 81e58b4..9c6628a 100644 --- a/DiningRoom/DiningRoomContracts/BindingModels/DrinkBindingModel.cs +++ b/DiningRoom/DiningRoomContracts/BindingModels/DrinkBindingModel.cs @@ -7,8 +7,9 @@ namespace DiningRoomContracts.BindingModels public int Id { get; set; } public int UserId { get; set; } + public int CardId { get; set; } - public string DrinkName { get; set; } = string.Empty; + public string DrinkName { get; set; } = string.Empty; public double Cost { get; set; } diff --git a/DiningRoom/DiningRoomContracts/BindingModels/OrderBindingModel.cs b/DiningRoom/DiningRoomContracts/BindingModels/OrderBindingModel.cs index 2135ded..9202b01 100644 --- a/DiningRoom/DiningRoomContracts/BindingModels/OrderBindingModel.cs +++ b/DiningRoom/DiningRoomContracts/BindingModels/OrderBindingModel.cs @@ -13,13 +13,13 @@ namespace DiningRoomContracts.BindingModels public int Id { get; set; } public int UserId { get; set; } + public int ProductId { get; set; } - public DateTime DateCreate { get; set; } = DateTime.Now; - - public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; - public Dictionary ProductOrder { get; set; } = new(); - public Dictionary DrinkOrder { get; set; } = new(); + public DateTime DateCreate { get; set; } = DateTime.Now; + public string ProductName { get; set; } = string.Empty; + public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; + public int Count { get; set; } public double Sum { get; set; } } } diff --git a/DiningRoom/DiningRoomContracts/SearchModels/DrinkSearchModel.cs b/DiningRoom/DiningRoomContracts/SearchModels/DrinkSearchModel.cs index ce3247e..1a8bbeb 100644 --- a/DiningRoom/DiningRoomContracts/SearchModels/DrinkSearchModel.cs +++ b/DiningRoom/DiningRoomContracts/SearchModels/DrinkSearchModel.cs @@ -5,6 +5,7 @@ public int? Id { get; set; } public int? UserId { get; set; } + public int? CardId { get; set; } public string? DrinkName { get; set; } } diff --git a/DiningRoom/DiningRoomContracts/SearchModels/OrderSearchModel.cs b/DiningRoom/DiningRoomContracts/SearchModels/OrderSearchModel.cs index 4d60ccc..bcc1ebb 100644 --- a/DiningRoom/DiningRoomContracts/SearchModels/OrderSearchModel.cs +++ b/DiningRoom/DiningRoomContracts/SearchModels/OrderSearchModel.cs @@ -15,10 +15,9 @@ namespace DiningRoomContracts.SearchModels public DateTime? DateFrom { get; set; } public DateTime? DateTo { get; set; } + public string? ProductName { get; set; } public OrderStatus? Status { get; set; } - public Dictionary ProductOrder { get; set; } = new(); - public Dictionary DrinkOrder { get; set; } = new(); public double? Sum { get; set; } } diff --git a/DiningRoom/DiningRoomContracts/ViewModels/DrinkViewModel.cs b/DiningRoom/DiningRoomContracts/ViewModels/DrinkViewModel.cs index e8f0839..9e9fbc4 100644 --- a/DiningRoom/DiningRoomContracts/ViewModels/DrinkViewModel.cs +++ b/DiningRoom/DiningRoomContracts/ViewModels/DrinkViewModel.cs @@ -8,8 +8,10 @@ namespace DiningRoomContracts.ViewModels public int Id { get; set; } public int UserId { get; set; } + public int CardId { get; set; } + public string CardName { get; set; } = string.Empty; - [DisplayName("Название товара")] + [DisplayName("Название товара")] public string DrinkName { get; set; } = string.Empty; [DisplayName("Стоимость")] diff --git a/DiningRoom/DiningRoomContracts/ViewModels/OrderViewModel.cs b/DiningRoom/DiningRoomContracts/ViewModels/OrderViewModel.cs index 26834b7..cdcbfa4 100644 --- a/DiningRoom/DiningRoomContracts/ViewModels/OrderViewModel.cs +++ b/DiningRoom/DiningRoomContracts/ViewModels/OrderViewModel.cs @@ -13,15 +13,16 @@ namespace DiningRoomContracts.ViewModels { [DisplayName("Номер")] public int Id { get; set; } - + public int ProductId { get; set; } public int UserId { get; set; } [DisplayName("Дата Создания")] public DateTime DateCreate { get; set; } = DateTime.Now; - public Dictionary ProductOrder { get; set; } = new(); - public Dictionary DrinkOrder { get; set; } = new(); - [DisplayName("Статус")] + [DisplayName("Блюдо")] + public string ProductName { get; set; } = string.Empty; + [DisplayName("Статус")] public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; - + [DisplayName("Стоимость")] + public int Count { get; set; } [DisplayName("Стоимость")] public double Sum { get; set; } diff --git a/DiningRoom/DiningRoomDataModels/Models/ICardModel.cs b/DiningRoom/DiningRoomDataModels/Models/ICardModel.cs index a84bb5b..358d754 100644 --- a/DiningRoom/DiningRoomDataModels/Models/ICardModel.cs +++ b/DiningRoom/DiningRoomDataModels/Models/ICardModel.cs @@ -11,13 +11,8 @@ int UserId { get; } /// - /// Название напитка + /// Название карты /// string CardName { get; } - - /// - /// Список комплектующих - /// - Dictionary DrinkCard { get; } } } diff --git a/DiningRoom/DiningRoomDataModels/Models/IDrinkModel.cs b/DiningRoom/DiningRoomDataModels/Models/IDrinkModel.cs index 6d1ec74..0c01525 100644 --- a/DiningRoom/DiningRoomDataModels/Models/IDrinkModel.cs +++ b/DiningRoom/DiningRoomDataModels/Models/IDrinkModel.cs @@ -19,6 +19,7 @@ /// Стоимость товара /// double Cost { get; } + int CardId { get; } /// /// Список комплектующих diff --git a/DiningRoom/DiningRoomDataModels/Models/IOrderModel.cs b/DiningRoom/DiningRoomDataModels/Models/IOrderModel.cs index e8457c0..25dec81 100644 --- a/DiningRoom/DiningRoomDataModels/Models/IOrderModel.cs +++ b/DiningRoom/DiningRoomDataModels/Models/IOrderModel.cs @@ -16,16 +16,16 @@ namespace DiningRoomDataModels.Models /// DateTime DateCreate { get; } - /// - /// Статус заказа - /// - OrderStatus Status { get; } - - /// - /// Стоимость заказа - /// - double Sum { get; } - Dictionary ProductOrder { get; } - Dictionary DrinkOrder { get; } + int ProductId { get; } + /// + /// Статус заказа + /// + OrderStatus Status { get; } + int Count { get; } + /// + /// Стоимость заказа + /// + double Sum { get; } + } } diff --git a/DiningRoom/DiningRoomDatabaseImplement/ComputerShopDatabase.cs b/DiningRoom/DiningRoomDatabaseImplement/ComputerShopDatabase.cs new file mode 100644 index 0000000..4e591fb --- /dev/null +++ b/DiningRoom/DiningRoomDatabaseImplement/ComputerShopDatabase.cs @@ -0,0 +1,37 @@ +using DiningRoomDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace DiningRoomDatabaseImplement +{ + public class DiningRoomDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder OptionsBuilder) + { + if (!OptionsBuilder.IsConfigured) + { + OptionsBuilder.UseNpgsql(@"Host=localhost;Port=5432;Database=DiningRoom;Username=postgres;Password=postgres"); + } + + base.OnConfiguring(OptionsBuilder); + + AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); + AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true); + } + public virtual DbSet Users { get; set; } + + public virtual DbSet Components { get; set; } + + public virtual DbSet Drinks { get; set; } + + public virtual DbSet DrinkComponents { get; set; } + + public virtual DbSet Products { get; set; } + + public virtual DbSet ProductComponents { get; set; } + + + public virtual DbSet Orders { get; set; } + public virtual DbSet Cards { get; set; } + + } +} diff --git a/DiningRoom/DiningRoomDatabaseImplement/DiningRoomDatabaseImplement.csproj b/DiningRoom/DiningRoomDatabaseImplement/DiningRoomDatabaseImplement.csproj new file mode 100644 index 0000000..26b33aa --- /dev/null +++ b/DiningRoom/DiningRoomDatabaseImplement/DiningRoomDatabaseImplement.csproj @@ -0,0 +1,24 @@ + + + + net6.0 + enable + enable + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + + diff --git a/DiningRoom/DiningRoomDatabaseImplement/Implements/OrderStorage.cs b/DiningRoom/DiningRoomDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..f1c92a7 --- /dev/null +++ b/DiningRoom/DiningRoomDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,152 @@ +using DiningRoomContracts.BindingModels; +using DiningRoomContracts.SearchModels; +using DiningRoomContracts.StorageContracts; +using DiningRoomContracts.ViewModels; +using DiningRoomDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Internal; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DiningRoomDatabaseImplement.Implements +{ + //!!!ПОДОБИЕ component + public class OrderStorage : IOrderStorage + { + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new DiningRoomDatabase(); + + var element = context.Orders + .Include(x => x.Product) + .FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + + return element.GetViewModel; + } + + return null; + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + + using var context = new DiningRoomDatabase(); + + return context.Orders + .Include(x => x.Client) + .Include(x => x.Implementer) + .FirstOrDefault(x => + (model.Statuses == null || model.Statuses != null && model.Statuses.Contains(x.Status)) && + model.ImplementerId.HasValue && x.ImplementerId == model.ImplementerId || + model.Id.HasValue && x.Id == model.Id + ) + ?.GetViewModel; + } + + public List GetFilteredList(OrderSearchModel model) + { + if (model.Id.HasValue) + { + var result = GetElement(model); + return result != null ? new() { result } : new(); + } + + using var context = new CarpentryWorkshopDatabase(); + IQueryable? queryWhere = null; + + if (model.DateFrom.HasValue && model.DateTo.HasValue) + { + queryWhere = context.Orders + .Where(x => model.DateFrom <= x.DateCreate.Date && + x.DateCreate.Date <= model.DateTo); + } + + else if (model.Statuses != null) + { + queryWhere = context.Orders.Where(x => model.Statuses.Contains(x.Status)); + } + + else if (model.ClientId.HasValue) + { + queryWhere = context.Orders.Where(x => x.ClientId == model.ClientId); + } + + else + { + return new(); + } + + return queryWhere + .Include(x => x.Client) + .Include(x => x.Implementer) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new CarpentryWorkshopDatabase(); + + return context.Orders + .Include(x => x.Wood) + .Include(x => x.Client) + .Include(x => x.Implementer) + .Select(x => x.GetViewModel).ToList(); + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + + if (newOrder == null) + { + return null; + } + + using var context = new CarpentryWorkshopDatabase(); + + context.Orders.Add(newOrder); + context.SaveChanges(); + + return context.Orders + .Include(x => x.Wood) + .Include(x => x.Client) + .Include(x => x.Implementer) + .FirstOrDefault(x => x.Id == newOrder.Id) + ?.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new CarpentryWorkshopDatabase(); + + var order = context.Orders + .Include(x => x.Wood) + .Include(x => x.Client) + .Include(x => x.Implementer) + .FirstOrDefault(x => x.Id == model.Id); + + if (order == null) + { + return null; + } + + order.Update(model); + context.SaveChanges(); + + return order.GetViewModel; + } + } +} diff --git a/DiningRoom/DiningRoomDatabaseImplement/Implements/UserStorage.cs b/DiningRoom/DiningRoomDatabaseImplement/Implements/UserStorage.cs new file mode 100644 index 0000000..36b11bb --- /dev/null +++ b/DiningRoom/DiningRoomDatabaseImplement/Implements/UserStorage.cs @@ -0,0 +1,106 @@ +using DiningRoomContracts.BindingModels; +using DiningRoomContracts.SearchModels; +using DiningRoomContracts.StorageContracts; +using DiningRoomContracts.ViewModels; +using DiningRoomDatabaseImplement.Models; +using DiningRoomDataModels.Enums; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DiningRoomDatabaseImplement.Implements +{ + //!!!МБ У USER ХРАНИТЬ СПИСКИ ВСЕХ СОЗДАННЫХ СУШНОСТЕЙ И ТОГДА ПРИ СОЗДАНИИ, УДАЛЕНИИ СУЩНОСТЕЙ ЕЩЁ СОЗДАВАТЬ И УДАЛЯТЬ ИЗ СПИСКА У ПОЛЬЗОВАТЕЛЯ + public class UserStorage : IUserStorage + { + public List GetFullList() + { + using var context = new DiningRoomDatabase(); + return context.Users.Select(x => x.GetViewModel).ToList(); + } + + //Фильтрация пользователей логически нужна только для роли (по остальным параметрам будет 1 или 0 пользователей) + public List GetFilteredList(UserSearchModel model) + { + if (model.Role == null || model.Role == UserRole.Неизвестная) + { + return new(); + } + using var context = new DiningRoomDatabase(); + return context.Users.Where(x => x.Role == model.Role).Select(x => x.GetViewModel).ToList(); + } + + //!!!ПРОВЕРИТЬ + //id, почта и логин уникальны, можно получать по ним + public UserViewModel? GetElement(UserSearchModel model) + { + //!!!МБ ЭТУ ПРОВЕРКУ УБРАТЬ + if (string.IsNullOrEmpty(model.Login) && string.IsNullOrEmpty(model.Email) && !model.Id.HasValue) + { + return null; + } + using var context = new DiningRoomDatabase(); + + //Поиск пользователя при входе в систему по логину, паролю и его роли (чтобы не могли войти в аккаунты другой роли) + if (!string.IsNullOrEmpty(model.Login) && !string.IsNullOrEmpty(model.Password) && model.Role.HasValue) + { + return context.Users.FirstOrDefault(x => x.Login == model.Login && x.Password == model.Password && x.Role == model.Role)?.GetViewModel; + } + //!!!НИЖЕ МБ НЕ НАДО + //Получение по логину (пользователей с таким логином будет 1 или 0) + if (!string.IsNullOrEmpty(model.Login)) + { + return context.Users.FirstOrDefault(x => x.Login == model.Login)?.GetViewModel; + } + //Получение по почте (пользователей с такой почтой будет 1 или 0) + else if (!string.IsNullOrEmpty(model.Email)) + { + return context.Users.FirstOrDefault(x => x.Email == model.Email)?.GetViewModel; + } + //Получение по id + return context.Users.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + + public UserViewModel? Insert(UserBindingModel model) + { + var newUser = User.Create(model); + if (newUser == null) + { + return null; + } + using var context = new DiningRoomDatabase(); + context.Users.Add(newUser); + context.SaveChanges(); + return newUser.GetViewModel; + } + + public UserViewModel? Update(UserBindingModel model) + { + using var context = new DiningRoomDatabase(); + var user = context.Users.FirstOrDefault(x => x.Id == model.Id); + if (user == null) + { + return null; + } + user.Update(model); + context.SaveChanges(); + return user.GetViewModel; + } + + //!!!МБ И НЕ НУЖЕН + public UserViewModel? Delete(UserBindingModel model) + { + using var context = new DiningRoomDatabase(); + var user = context.Users.FirstOrDefault(rec => rec.Id == model.Id); + if (user != null) + { + context.Users.Remove(user); + context.SaveChanges(); + return user.GetViewModel; + } + return null; + } + } +} diff --git a/DiningRoom/DiningRoomDatabaseImplement/Models/Card.cs b/DiningRoom/DiningRoomDatabaseImplement/Models/Card.cs new file mode 100644 index 0000000..87ebbe9 --- /dev/null +++ b/DiningRoom/DiningRoomDatabaseImplement/Models/Card.cs @@ -0,0 +1,40 @@ +using DiningRoomContracts.BindingModels; +using DiningRoomContracts.ViewModels; +using DiningRoomDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DiningRoomDatabaseImplement.Models +{ + public class Card : ICardModel + { + public int Id { get; set; } + public int UserId { get; set; } + public string CardName { get; set; } = string.Empty; + public static Card Create(DiningRoomDatabase context, CardBindingModel model) + { + return new Card() + { + Id = model.Id, + CardName = model.CardName, + UserId = model.UserId, + }; + } + public void Update(CardBindingModel model) + { + CardName = model.CardName; + } + public CardViewModel GetViewModel => new() + { + Id = Id, + CardName = CardName, + UserId = UserId, + }; + } +} diff --git a/DiningRoom/DiningRoomDatabaseImplement/Models/Component.cs b/DiningRoom/DiningRoomDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..df3fcb8 --- /dev/null +++ b/DiningRoom/DiningRoomDatabaseImplement/Models/Component.cs @@ -0,0 +1,57 @@ +using DiningRoomContracts.BindingModels; +using DiningRoomContracts.ViewModels; +using DiningRoomDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace DiningRoomDatabaseImplement.Models +{ + public class Component : IComponentModel + { + public int Id { get; private set; } + + [Required] + public int UserId { get; private set; } + + [Required] + public string ComponentName { get; private set; } = string.Empty; + public string Unit { get; private set; } = string.Empty; + + [Required] + public double Cost { get; private set; } + + [ForeignKey("ComponentId")] + public virtual List DrinkComponents { get; set; } = new(); + [ForeignKey("ComponentId")] + public virtual List ProductComponents { get; set; } = new(); + + public static Component Create(ComponentBindingModel Model) + { + return new() + { + Id = Model.Id, + UserId = Model.UserId, + ComponentName = Model.ComponentName, + Cost = Model.Cost, + Unit = Model.Unit, + }; + } + + public void Update(ComponentBindingModel Model) + { + ComponentName = Model.ComponentName; + Cost = Model.Cost; + Unit = Model.Unit; + } + + public ComponentViewModel ViewModel => new() + { + Id = Id, + UserId = UserId, + ComponentName = ComponentName, + Cost = Cost, + Unit = Unit, + + }; + } +} diff --git a/DiningRoom/DiningRoomDatabaseImplement/Models/Drink.cs b/DiningRoom/DiningRoomDatabaseImplement/Models/Drink.cs new file mode 100644 index 0000000..e7da4c6 --- /dev/null +++ b/DiningRoom/DiningRoomDatabaseImplement/Models/Drink.cs @@ -0,0 +1,116 @@ +using DiningRoomContracts.BindingModels; +using DiningRoomContracts.ViewModels; +using DiningRoomDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Diagnostics; + +namespace DiningRoomDatabaseImplement.Models +{ + public class Drink : IDrinkModel + { + public int Id { get; private set; } + + [Required] + public int UserId { get; private set; } + + [Required] + public string DrinkName { get; private set; } = string.Empty; + + [Required] + public double Cost { get; private set; } + + public int CardId { get; private set; } + + [Required] + public string Category { get; private set; } = string.Empty; + + [ForeignKey("ComponentId")] + public virtual List Components { get; set; } = new(); + + private Dictionary? _drinkComponents; + + [NotMapped] + public Dictionary DrinkComponents + { + get + { + if (_drinkComponents == null) + { + _drinkComponents = Components.ToDictionary( + DrkComp => DrkComp.ComponentId, + DrkComp => (DrkComp.Component as IComponentModel, DrkComp.Count) + ); + } + + return _drinkComponents; + } + } + + public static Drink Create(DiningRoomDatabase Context, DrinkBindingModel Model) + { + return new() + { + Id = Model.Id, + UserId = Model.UserId, + DrinkName = Model.DrinkName, + Cost = Model.Cost, + CardId = Model.CardId, + Components = Model.DrinkComponents.Select(x => new DrinkComponent + { + Component = Context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList(), + }; + } + public void Update(DrinkBindingModel model) + { + DrinkName = model.DrinkName; + Cost = model.Cost; + } + public DrinkViewModel GetViewModel + { + + get + { + using var context = new DiningRoomDatabase(); + return new DrinkViewModel + { + Id = Id, + DrinkName = DrinkName, + CardName = context.Cards.FirstOrDefault(x => x.Id == CardId)?.CardName ?? string.Empty, + Cost = Cost, + DrinkComponents = DrinkComponents + }; + } + } + public void UpdateComponents(DiningRoomDatabase context, DrinkBindingModel model) + { + var drinkComponents = context.DrinkComponents.Where(rec => rec.DrinkId == model.Id).ToList(); + if (drinkComponents != null && drinkComponents.Count > 0) + { // удалили те, которых нет в модели + context.DrinkComponents.RemoveRange(drinkComponents.Where(rec => !model.DrinkComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateComponent in drinkComponents) + { + updateComponent.Count = model.DrinkComponents[updateComponent.ComponentId].Item2; + model.DrinkComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var drink = context.Drinks.First(x => x.Id == Id); + foreach (var pc in model.DrinkComponents) + { + context.DrinkComponents.Add(new DrinkComponent + { + Drink = drink, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _drinkComponents = null; + } + } +} diff --git a/DiningRoom/DiningRoomDatabaseImplement/Models/DrinkComponent.cs b/DiningRoom/DiningRoomDatabaseImplement/Models/DrinkComponent.cs new file mode 100644 index 0000000..afd3724 --- /dev/null +++ b/DiningRoom/DiningRoomDatabaseImplement/Models/DrinkComponent.cs @@ -0,0 +1,22 @@ +using System.ComponentModel.DataAnnotations; + +namespace DiningRoomDatabaseImplement.Models +{ + public class DrinkComponent + { + public int Id { get; set; } + + [Required] + public int DrinkId { get; set; } + + [Required] + public int ComponentId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Drink Drink { get; set; } = new(); + + public virtual Component Component { get; set; } = new(); + } +} diff --git a/DiningRoom/DiningRoomDatabaseImplement/Models/Order.cs b/DiningRoom/DiningRoomDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..6f6f1f8 --- /dev/null +++ b/DiningRoom/DiningRoomDatabaseImplement/Models/Order.cs @@ -0,0 +1,84 @@ +using DiningRoomContracts.BindingModels; +using DiningRoomContracts.ViewModels; +using DiningRoomDataModels.Enums; +using DiningRoomDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DiningRoomDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; set; } + [ForeignKey("ProductId")] + public int ProductId { get; set; } + [ForeignKey("UserId")] + [Required] + public int UserId { get; private set; } + public User User { get; set; } + public virtual Product Product { get; set; } + [Required] + public double Sum { get; private set; } + [Required] + public int Count { get; private set; } + + [Required] + public DateTime DateCreate { get; private set; } = DateTime.Now; + + [Required] + public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; + + public static Order? Create(OrderBindingModel model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + ProductId = model.ProductId, + UserId = model.UserId, + DateCreate = model.DateCreate, + Status = model.Status, + Count = model.Count, + Sum = model.Sum, + }; + } + + public void Update(OrderBindingModel model) + { + if (model == null) + { + return; + } + using var context = new DiningRoomDatabase(); + Status = model.Status; + context.SaveChanges(); + } + + public OrderViewModel GetViewModel + { + get + { + using var context = new DiningRoomDatabase(); + return new OrderViewModel + { + Id = Id, + ProductId = ProductId, + ProductName = context.Products.FirstOrDefault(x => x.Id == ProductId)?.ProductName ?? string.Empty, + UserId = UserId, + DateCreate = DateCreate, + Status = Status, + Count = Count, + Sum = Sum + }; + } + } + } +} diff --git a/DiningRoom/DiningRoomDatabaseImplement/Models/Product.cs b/DiningRoom/DiningRoomDatabaseImplement/Models/Product.cs new file mode 100644 index 0000000..dd1f7f9 --- /dev/null +++ b/DiningRoom/DiningRoomDatabaseImplement/Models/Product.cs @@ -0,0 +1,101 @@ +using DiningRoomContracts.BindingModels; +using DiningRoomContracts.ViewModels; +using DiningRoomDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Diagnostics; + +namespace DiningRoomDatabaseImplement.Models +{ + public class Product : IProductModel + { + public int Id { get; set; } + + [Required] + public int UserId { get; set; } + + [Required] + public string ProductName { get; set; } = string.Empty; + + [Required] + public double Cost { get; set; } + + [ForeignKey("ComponentId")] + public virtual List Components { get; set; } = new(); + + private Dictionary? _productComponents; + + [NotMapped] + public Dictionary ProductComponents + { + get + { + if (_productComponents == null) + { + _productComponents = Components.ToDictionary( + ProdComp => ProdComp.ComponentId, + ProdComp => (ProdComp.Component as IComponentModel, ProdComp.Count) + ); + } + + return _productComponents; + } + } + + public static Product Create(DiningRoomDatabase Context, ProductBindingModel Model) + { + return new() + { + Id = Model.Id, + UserId = Model.UserId, + ProductName = Model.ProductName, + Cost = Model.Cost, + Components = Model.ProductComponents.Select(x => new ProductComponent + { + Component = Context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList(), + }; + } + public void Update(ProductBindingModel model) + { + ProductName = model.ProductName; + Cost = model.Cost; + } + public ProductViewModel GetViewModel => new() + { + Id = Id, + ProductName = ProductName, + Cost = Cost, + ProductComponents = ProductComponents + }; + public void UpdateComponents(DiningRoomDatabase context, ProductBindingModel model) + { + var productComponents = context.ProductComponents.Where(rec => rec.ProductId == model.Id).ToList(); + if (productComponents != null && productComponents.Count > 0) + { // удалили те, которых нет в модели + context.ProductComponents.RemoveRange(productComponents.Where(rec => !model.ProductComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateComponent in productComponents) + { + updateComponent.Count = model.ProductComponents[updateComponent.ComponentId].Item2; + model.ProductComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var product = context.Products.First(x => x.Id == Id); + foreach (var pc in model.ProductComponents) + { + context.ProductComponents.Add(new ProductComponent + { + Product = product, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _productComponents = null; + } + } +} diff --git a/DiningRoom/DiningRoomDatabaseImplement/Models/ProductComponent.cs b/DiningRoom/DiningRoomDatabaseImplement/Models/ProductComponent.cs new file mode 100644 index 0000000..5531e35 --- /dev/null +++ b/DiningRoom/DiningRoomDatabaseImplement/Models/ProductComponent.cs @@ -0,0 +1,22 @@ +using System.ComponentModel.DataAnnotations; + +namespace DiningRoomDatabaseImplement.Models +{ + public class ProductComponent + { + public int Id { get; set; } + + [Required] + public int ProductId { get; set; } + + [Required] + public int ComponentId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Product Product { get; set; } = new(); + + public virtual Component Component { get; set; } = new(); + } +} diff --git a/DiningRoom/DiningRoomDatabaseImplement/Models/User.cs b/DiningRoom/DiningRoomDatabaseImplement/Models/User.cs new file mode 100644 index 0000000..788d74c --- /dev/null +++ b/DiningRoom/DiningRoomDatabaseImplement/Models/User.cs @@ -0,0 +1,62 @@ +using DiningRoomContracts.BindingModels; +using DiningRoomContracts.ViewModels; +using DiningRoomDataModels.Enums; +using DiningRoomDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DiningRoomDatabaseImplement.Models +{ + public class User : IUserModel + { + public int Id { get; private set; } + + [Required] + public string Login { get; set; } = string.Empty; + + [Required] + public string Password { get; set; } = string.Empty; + + //!!!МБ ТУТ НУЖНА ДОП. АННОТАЦИЯ ПРОВЕРКИ ПОЧТЫ + [Required] + public string Email { get; set; } = string.Empty; + + + //!!!МБ ТУТ НАДО БУДЕТ СОЗДАТЬ 2 РАЗНЫХ МЕТОДА: СОЗДАНИЕ ИСПОЛНИТЕЛЯ и СОЗДАНИЕ ПОРУЧИТЕЛЯ (хотя мб где-то потом будем задавать роль в BindingModel) + public static User Create(UserBindingModel model) + { + return new User + { + Id = model.Id, + Login = model.Login, + Password = model.Password, + Email = model.Email + }; + } + + public void Update(UserBindingModel model) + { + if (model == null) + { + return; + } + Login = model.Login; + Password = model.Password; + Email = model.Email; + } + + //!!!МБ ТУТ НЕ НАДО РОЛЬ + public UserViewModel GetViewModel => new() + { + Id = Id, + Login = Login, + Password = Password, + Email = Email, + Role = Role + }; + } +} diff --git a/README.md b/README.md index 5da3c9b..8af5d7b 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # PIbd-23_Tikhonenkov_A_E_CourseWork_DiningRoom +Курсовая работа по дисциплине "Разработка профессиональных приложений". Тема курсовой работы: «Столовая «Рога и копыта» роль: поручитель. Студент: Тихоненков А.Е. \ No newline at end of file