From b6f8cf61d3fbda35c322a39d827e678ec3418df3 Mon Sep 17 00:00:00 2001 From: FLARJ <122087628+FLARJ@users.noreply.github.com> Date: Fri, 7 Apr 2023 17:42:34 +0400 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=BF=D0=BE=D0=BB=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20FurnitureFactoryDataBaseImplement=20Implem?= =?UTF-8?q?ent=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BindingModels/OrdersBindingModel.cs | 1 + .../SearchModels/OrderSearchModel.cs | 4 + .../SearchModels/SalesSalonsSearchModel.cs | 2 + .../ViewModel/OrdersViewModel.cs | 4 + .../Implements/HeadsetStorage.cs | 2 - .../Implements/ManagerStorage.cs | 105 +++++++++++++++ .../Implements/OrderStorage.cs | 123 ++++++++++++++++++ .../Implements/SalesSalonStorage.cs | 90 +++++++++++++ .../Models/Orders.cs | 4 + .../Models/IOrdersModel.cs | 1 + 10 files changed, 334 insertions(+), 2 deletions(-) create mode 100644 FurnitureFactory/FurnitureFactoryDataBaseImplement/Implements/ManagerStorage.cs create mode 100644 FurnitureFactory/FurnitureFactoryDataBaseImplement/Implements/OrderStorage.cs create mode 100644 FurnitureFactory/FurnitureFactoryDataBaseImplement/Implements/SalesSalonStorage.cs diff --git a/FurnitureFactory/FurnitureContracts/BindingModels/OrdersBindingModel.cs b/FurnitureFactory/FurnitureContracts/BindingModels/OrdersBindingModel.cs index f8268e6..bc4f2a4 100644 --- a/FurnitureFactory/FurnitureContracts/BindingModels/OrdersBindingModel.cs +++ b/FurnitureFactory/FurnitureContracts/BindingModels/OrdersBindingModel.cs @@ -4,6 +4,7 @@ namespace FurnitureContracts.BindingModels { public class OrdersBindingModel : IOrdersModel { + public string Title { get; set; } = string.Empty; public DateTime Date { get; set; } = DateTime.Now; public string Status { get; set; } = string.Empty; public int Id { get; set; } diff --git a/FurnitureFactory/FurnitureContracts/SearchModels/OrderSearchModel.cs b/FurnitureFactory/FurnitureContracts/SearchModels/OrderSearchModel.cs index d8715a4..69ee35f 100644 --- a/FurnitureFactory/FurnitureContracts/SearchModels/OrderSearchModel.cs +++ b/FurnitureFactory/FurnitureContracts/SearchModels/OrderSearchModel.cs @@ -9,5 +9,9 @@ namespace FurnitureContracts.SearchModels public class OrderSearchModel { public int? Id { get; set; } + public int? ManagerId { get; set; } + public string? Title { get; set; } + public DateTime? DateFrom { get; set; } + public DateTime? DateTo { get; set; } } } diff --git a/FurnitureFactory/FurnitureContracts/SearchModels/SalesSalonsSearchModel.cs b/FurnitureFactory/FurnitureContracts/SearchModels/SalesSalonsSearchModel.cs index 0a10c65..115e6d1 100644 --- a/FurnitureFactory/FurnitureContracts/SearchModels/SalesSalonsSearchModel.cs +++ b/FurnitureFactory/FurnitureContracts/SearchModels/SalesSalonsSearchModel.cs @@ -11,5 +11,7 @@ namespace FurnitureContracts.SearchModels public string? Name { get; set; } public int? Id { get; set; } + public int? ManagerId { get; set; } + } } diff --git a/FurnitureFactory/FurnitureContracts/ViewModel/OrdersViewModel.cs b/FurnitureFactory/FurnitureContracts/ViewModel/OrdersViewModel.cs index c0d420f..2c72ba4 100644 --- a/FurnitureFactory/FurnitureContracts/ViewModel/OrdersViewModel.cs +++ b/FurnitureFactory/FurnitureContracts/ViewModel/OrdersViewModel.cs @@ -10,6 +10,10 @@ namespace FurnitureContracts.ViewModel { public class OrdersViewModel : IOrdersModel { + + [DisplayName("Название")] + public string Title { get; set; } = string.Empty; + [DisplayName("Дата заказа")] public DateTime Date { get; set; } diff --git a/FurnitureFactory/FurnitureFactoryDataBaseImplement/Implements/HeadsetStorage.cs b/FurnitureFactory/FurnitureFactoryDataBaseImplement/Implements/HeadsetStorage.cs index 2d4454a..d4be540 100644 --- a/FurnitureFactory/FurnitureFactoryDataBaseImplement/Implements/HeadsetStorage.cs +++ b/FurnitureFactory/FurnitureFactoryDataBaseImplement/Implements/HeadsetStorage.cs @@ -63,8 +63,6 @@ namespace FurnitureFactoryDataBaseImplements.Implements .ToList() .Select(x => x.GetViewModel) .ToList(); - - } public HeadsetViewModel? GetElement(HeadsetSearchModel model) diff --git a/FurnitureFactory/FurnitureFactoryDataBaseImplement/Implements/ManagerStorage.cs b/FurnitureFactory/FurnitureFactoryDataBaseImplement/Implements/ManagerStorage.cs new file mode 100644 index 0000000..4830695 --- /dev/null +++ b/FurnitureFactory/FurnitureFactoryDataBaseImplement/Implements/ManagerStorage.cs @@ -0,0 +1,105 @@ +using Microsoft.EntityFrameworkCore; +using FurnitureContracts.BindingModels; +using FurnitureContracts.SearchModels; +using FurnitureContracts.StoragesContracts; +using FurnitureContracts.ViewModel; +using FurnitureFactoryDataBaseImplements.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.ConstrainedExecution; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureFactoryDataBaseImplements.Implements +{ + public class ManagerStorage : IManagerStorage + { + public ManagerViewModel? Delete(ManagerBindingModel model) + { + using var context = new FurnitureDataBase(); + var element = context.Managers + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Managers.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public ManagerViewModel? GetElement(ManagerSearchModel model) + { + if (string.IsNullOrEmpty(model.Login) && !model.Id.HasValue) + { + return null; + } + using var context = new FurnitureDataBase(); + return context.Managers + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Login) && x.Login == model.Login) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public List GetFilteredList(ManagerSearchModel model) + { + if (string.IsNullOrEmpty(model.UserName)) + { + return new(); + } + using var context = new FurnitureDataBase(); + return context.Managers + .Where(x => x.Name.Contains(model.UserName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new FurnitureDataBase(); + return context.Managers + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public ManagerViewModel? Insert(ManagerBindingModel model) + { + using var context = new FurnitureDataBase(); + var newManager = Manager.Create(context, model); + if (newManager == null) + { + return null; + } + context.Managers.Add(newManager); + context.SaveChanges(); + return newManager.GetViewModel; + } + + public ManagerViewModel? Update(ManagerBindingModel model) + { + using var context = new FurnitureDataBase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var manager = context.Managers.FirstOrDefault(rec => rec.Id == model.Id); + if (manager == null) + { + return null; + } + manager.Update(model); + context.SaveChanges(); + transaction.Commit(); + return manager.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + } +} + diff --git a/FurnitureFactory/FurnitureFactoryDataBaseImplement/Implements/OrderStorage.cs b/FurnitureFactory/FurnitureFactoryDataBaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..dc1e37c --- /dev/null +++ b/FurnitureFactory/FurnitureFactoryDataBaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,123 @@ +using Microsoft.EntityFrameworkCore; +using FurnitureContracts.BindingModels; +using FurnitureContracts.SearchModels; +using FurnitureContracts.StoragesContracts; +using FurnitureContracts.ViewModel; +using FurnitureFactoryDataBaseImplements.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.ConstrainedExecution; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureFactoryDataBaseImplements.Implements +{ + public class OrderStorage : IOrdersStorage + { + public List GetFullList() + { + using var context = new FurnitureDataBase(); + return context.Orders + .Include(x => x.SalesSalons) + .ThenInclude(x => x.SalesSalon) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.ManagerId.HasValue) + { + return new(); + } + using var context = new FurnitureDataBase(); + if (model.ManagerId.HasValue && model.DateFrom.HasValue && model.DateTo != null) + { + return context.Orders + .Include(x => x.SalesSalons) + .ThenInclude(x => x.SalesSalon) + .Where(x => x.Date >= model.DateFrom && x.Date <= model.DateTo && x.ManagerId == model.ManagerId) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + return context.Orders + .Include(x => x.SalesSalons) + .ThenInclude(x => x.SalesSalon) + .Where(x => x.ManagerId == model.ManagerId) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + + } + + public OrdersViewModel? GetElement(OrderSearchModel model) + { + if ((string.IsNullOrEmpty(model.Title) && !model.Id.HasValue) || !model.ManagerId.HasValue) + { + return null; + } + using var context = new FurnitureDataBase(); + return context.Orders + .Include(x => x.SalesSalons) + .ThenInclude(x => x.SalesSalon) + .FirstOrDefault(x => ((!string.IsNullOrEmpty(model.Title) && x.Title == model.Title) || + (model.Id.HasValue && x.Id == model.Id)) && x.ManagerId == model.ManagerId) + ?.GetViewModel; + } + + public OrdersViewModel? Insert(OrdersBindingModel model) + { + using var context = new FurnitureDataBase(); + var newOrder = Order.Create(context, model); + if (newOrder == null) + { + return null; + } + context.Orders.Add(newOrder); + context.SaveChanges(); + return newOrder.GetViewModel; + } + + public OrdersViewModel? Update(OrdersBindingModel model) + { + using var context = new FurnitureDataBase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var order = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + order.UpdateInterests(context, model); + transaction.Commit(); + return order.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public OrdersViewModel? Delete(OrdersBindingModel model) + { + using var context = new FurnitureDataBase(); + var element = context.Orders + .Include(x => x.SalesSalons) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.SalesSalons.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/FurnitureFactory/FurnitureFactoryDataBaseImplement/Implements/SalesSalonStorage.cs b/FurnitureFactory/FurnitureFactoryDataBaseImplement/Implements/SalesSalonStorage.cs new file mode 100644 index 0000000..443eabb --- /dev/null +++ b/FurnitureFactory/FurnitureFactoryDataBaseImplement/Implements/SalesSalonStorage.cs @@ -0,0 +1,90 @@ +using FurnitureContracts.BindingModels; +using FurnitureContracts.SearchModels; +using FurnitureContracts.StoragesContracts; +using FurnitureContracts.ViewModel; +using FurnitureFactoryDataBaseImplements.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureFactoryDataBaseImplements.Implements +{ + public class SalesSalonStorage : ISalesSalonsStorage + { + public List GetFullList() + { + using var context = new FurnitureDataBase(); + return context.SalesSalons + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(SalesSalonsSearchModel model) + { + if (!model.ManagerId.HasValue) + { + return new(); + } + using var context = new FurnitureDataBase(); + return context.Interests + .Where(x => x.StudentId == model.ManagerId) + .Select(x => x.GetViewModel) + .ToList(); + } + + public SalesSalonsViewModel? GetElement(SalesSalonsSearchModel model) + { + if ((string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) || !model.ManagerId.HasValue) + { + return null; + } + using var context = new FurnitureDataBase(); + return context.SalesSalons + .FirstOrDefault(x => ((!string.IsNullOrEmpty(model.Name) && x.Title == model.Name) || + (model.Id.HasValue && x.Id == model.Id)) && x.ManagerId == model.ManagerId) + ?.GetViewModel; + } + + public SalesSalonsViewModel? Insert(SalesSalonsBindingModel model) + { + var newSalesSalon = SalesSalons.Create(model); + if (newSalesSalon == null) + { + return null; + } + using var context = new FurnitureDataBase(); + context.SalesSalons.Add(newSalesSalon); + context.SaveChanges(); + return newSalesSalon.GetViewModel; + } + + public SalesSalonsViewModel? Update(SalesSalonsBindingModel model) + { + using var context = new FurnitureDataBase(); + var salessalon = context.SalesSalons.FirstOrDefault(x => x.Id == model.Id); + if (salessalon == null) + { + return null; + } + salessalon.Update(model); + context.SaveChanges(); + return salessalon.GetViewModel; + } + + public SalesSalonsViewModel? Delete(SalesSalonsBindingModel model) + { + using var context = new FurnitureDataBase(); + var element = context.SalesSalons.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.SalesSalons.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/FurnitureFactory/FurnitureFactoryDataBaseImplement/Models/Orders.cs b/FurnitureFactory/FurnitureFactoryDataBaseImplement/Models/Orders.cs index 7c8738e..2f8755c 100644 --- a/FurnitureFactory/FurnitureFactoryDataBaseImplement/Models/Orders.cs +++ b/FurnitureFactory/FurnitureFactoryDataBaseImplement/Models/Orders.cs @@ -12,6 +12,8 @@ namespace FurnitureFactoryDataBaseImplement.Models { public class Orders : IOrdersModel { + [Required] + public string Title { get; set; } = string.Empty; [Required] public string Status { get; set; } = string.Empty; @@ -46,6 +48,7 @@ namespace FurnitureFactoryDataBaseImplement.Models return new Orders() { Id = model.Id, + Title = model.Title, Status = model.Status, Date = model.Date, ManagerId = model.ManagerId, @@ -65,6 +68,7 @@ namespace FurnitureFactoryDataBaseImplement.Models public OrdersViewModel GetViewModel => new() { Id = Id, + Title = Title, Status = Status, Date = Date, ManagerId = ManagerId, diff --git a/FurnitureFactory/FurnitureFactoryDataModels/Models/IOrdersModel.cs b/FurnitureFactory/FurnitureFactoryDataModels/Models/IOrdersModel.cs index 82034fa..a8c3bb7 100644 --- a/FurnitureFactory/FurnitureFactoryDataModels/Models/IOrdersModel.cs +++ b/FurnitureFactory/FurnitureFactoryDataModels/Models/IOrdersModel.cs @@ -8,6 +8,7 @@ namespace FurnitureFactoryDataModels.Models { public interface IOrdersModel : IId { + string Title { get; } DateTime Date { get; } string Status { get; } public Dictionary OrdersSalesSalons { get; }