From 3e20f039bfca8e8bd9aaef45d306ea50e5f8b03f Mon Sep 17 00:00:00 2001 From: the Date: Wed, 19 Apr 2023 15:46:04 +0400 Subject: [PATCH 1/3] Work started --- .../Models/IImplementerModel.cs | 16 ++ .../Models/IOrderModel.cs | 2 + .../BusinessLogics/ImplementerLogic.cs | 126 +++++++++ .../BusinessLogics/OrderLogic.cs | 21 ++ .../BusinessLogics/WorkModeling.cs | 140 ++++++++++ .../BindingModels/ImplementerBindingModel.cs | 22 ++ .../BindingModels/OrderBindingModel.cs | 1 + .../IImplementerLogic.cs | 24 ++ .../BusinessLogicsContracts/IOrderLogic.cs | 1 + .../BusinessLogicsContracts/IWorkProcess.cs | 13 + .../SearchModels/ImplementerSearchModel.cs | 17 ++ .../SearchModels/OrderSearchModel.cs | 5 +- .../StoragesContracts/IImplementerStorage.cs | 26 ++ .../ViewModels/ImplementerViewModel.cs | 27 ++ .../ViewModels/OrderViewModel.cs | 3 + .../ComputerShopDatabase.cs | 1 + .../Implements/ImplementerStorage.cs | 83 ++++++ .../Implements/OrderStorage.cs | 44 ++- .../20230419114414_ImplementerMig.Designer.cs | 261 ++++++++++++++++++ .../20230419114414_ImplementerMig.cs | 67 +++++ .../ComputerShopDatabaseModelSnapshot.cs | 43 +++ .../Models/Implementer.cs | 65 +++++ ComputerShopDatabaseImplement/Models/Order.cs | 8 +- ComputerShopFileImplement/Models/Order.cs | 2 + ComputerShopListImplement/Models/Order.cs | 2 + .../Controllers/ImplementerController.cs | 100 +++++++ ComputersShop/FormImplementer.Designer.cs | 173 ++++++++++++ ComputersShop/FormImplementer.cs | 101 +++++++ ComputersShop/FormImplementer.resx | 60 ++++ ComputersShop/FormImplementers.Designer.cs | 120 ++++++++ ComputersShop/FormImplementers.cs | 114 ++++++++ ComputersShop/FormImplementers.resx | 60 ++++ ComputersShop/FormMain.Designer.cs | 72 +++-- ComputersShop/FormMain.cs | 98 ++----- ComputersShop/Program.cs | 5 + 35 files changed, 1800 insertions(+), 123 deletions(-) create mode 100644 AbstractComputerDataModel/Models/IImplementerModel.cs create mode 100644 ComputerShopBusinessLogic/BusinessLogics/ImplementerLogic.cs create mode 100644 ComputerShopBusinessLogic/BusinessLogics/WorkModeling.cs create mode 100644 ComputerShopContracts/BindingModels/ImplementerBindingModel.cs create mode 100644 ComputerShopContracts/BusinessLogicsContracts/IImplementerLogic.cs create mode 100644 ComputerShopContracts/BusinessLogicsContracts/IWorkProcess.cs create mode 100644 ComputerShopContracts/SearchModels/ImplementerSearchModel.cs create mode 100644 ComputerShopContracts/StoragesContracts/IImplementerStorage.cs create mode 100644 ComputerShopContracts/ViewModels/ImplementerViewModel.cs create mode 100644 ComputerShopDatabaseImplement/Implements/ImplementerStorage.cs create mode 100644 ComputerShopDatabaseImplement/Migrations/20230419114414_ImplementerMig.Designer.cs create mode 100644 ComputerShopDatabaseImplement/Migrations/20230419114414_ImplementerMig.cs create mode 100644 ComputerShopDatabaseImplement/Models/Implementer.cs create mode 100644 ComputerShopRestApi/Controllers/ImplementerController.cs create mode 100644 ComputersShop/FormImplementer.Designer.cs create mode 100644 ComputersShop/FormImplementer.cs create mode 100644 ComputersShop/FormImplementer.resx create mode 100644 ComputersShop/FormImplementers.Designer.cs create mode 100644 ComputersShop/FormImplementers.cs create mode 100644 ComputersShop/FormImplementers.resx diff --git a/AbstractComputerDataModel/Models/IImplementerModel.cs b/AbstractComputerDataModel/Models/IImplementerModel.cs new file mode 100644 index 0000000..bd73c0c --- /dev/null +++ b/AbstractComputerDataModel/Models/IImplementerModel.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopDataModels.Models +{ + public interface IImplementerModel : IId + { + string ImplementerFIO { get; } + string Password { get; } + int WorkExperience { get; } + int Qualification { get; } + } +} diff --git a/AbstractComputerDataModel/Models/IOrderModel.cs b/AbstractComputerDataModel/Models/IOrderModel.cs index 682d963..94bdbd2 100644 --- a/AbstractComputerDataModel/Models/IOrderModel.cs +++ b/AbstractComputerDataModel/Models/IOrderModel.cs @@ -11,6 +11,8 @@ namespace ComputerShopDataModels.Models { int ComputerId { get; } string ComputerName { get; } + int ClientId { get; } + int? ImplementerId { get; } int Count { get; } double Sum { get; } OrderStatus Status { get; } diff --git a/ComputerShopBusinessLogic/BusinessLogics/ImplementerLogic.cs b/ComputerShopBusinessLogic/BusinessLogics/ImplementerLogic.cs new file mode 100644 index 0000000..3bca2aa --- /dev/null +++ b/ComputerShopBusinessLogic/BusinessLogics/ImplementerLogic.cs @@ -0,0 +1,126 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.BusinessLogicsContracts; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.StoragesContracts; +using ComputerShopContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopBusinessLogic.BusinessLogics +{ + public class ImplementerLogic : IImplementerLogic + { + private readonly ILogger _logger; + private readonly IImplementerStorage _implementerStorage; + public ImplementerLogic(ILogger logger, IImplementerStorage implementerStorage) + { + _logger = logger; + _implementerStorage = implementerStorage; + } + + public bool Create(ImplementerBindingModel model) + { + CheckModel(model); + if (_implementerStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(ImplementerBindingModel model) + { + CheckModel(model); + if (_implementerStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public bool Delete(ImplementerBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_implementerStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + public ImplementerViewModel? ReadElement(ImplementerSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. FIO:{FIO}.Id:{ Id}", model.ImplementerFIO, model.Id); + var element = _implementerStorage.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(ImplementerSearchModel? model) + { + _logger.LogInformation("ReadList. FIO:{FIO}.Id:{ Id} ", model?.ImplementerFIO, model?.Id); + var list = (model == null) ? _implementerStorage.GetFullList() : _implementerStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + private void CheckModel(ImplementerBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (model.WorkExperience < 0) + { + throw new ArgumentException("Стаж работы не может быть меньше нуля", nameof(model.WorkExperience)); + } + if (model.Qualification < 0) + { + throw new ArgumentException("Квалификация не должна быть меньше нуля", nameof(model.Qualification)); + } + if (string.IsNullOrEmpty(model.Password)) + { + throw new ArgumentNullException("Нет пароля исполнителя", nameof(model.ImplementerFIO)); + } + if (string.IsNullOrEmpty(model.ImplementerFIO)) + { + throw new ArgumentNullException("Нет ФИО исполнителя", nameof(model.ImplementerFIO)); + } + _logger.LogInformation("Implementer. Id: {Id}, FIO: {FIO}", model.Id, model.ImplementerFIO); + var element = _implementerStorage.GetElement(new ImplementerSearchModel + { + ImplementerFIO = model.ImplementerFIO, + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Исполнитель с таким ФИО уже есть"); + } + } + } +} diff --git a/ComputerShopBusinessLogic/BusinessLogics/OrderLogic.cs b/ComputerShopBusinessLogic/BusinessLogics/OrderLogic.cs index 28770f5..3c1adc2 100644 --- a/ComputerShopBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/ComputerShopBusinessLogic/BusinessLogics/OrderLogic.cs @@ -58,6 +58,10 @@ namespace ComputerShopBusinessLogic.BusinessLogics _logger.LogWarning("Update operation failed"); return false; } + if (model.ImplementerId.HasValue) + { + model.ImplementerId = model.ImplementerId; + } return true; } @@ -89,6 +93,23 @@ namespace ComputerShopBusinessLogic.BusinessLogics return list; } + public OrderViewModel? ReadElement(OrderSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. Id:{ Id}", model.Id); + var element = _orderStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + return element; + } + private void CheckModel(OrderBindingModel model, bool withParams = true) { if (model == null) diff --git a/ComputerShopBusinessLogic/BusinessLogics/WorkModeling.cs b/ComputerShopBusinessLogic/BusinessLogics/WorkModeling.cs new file mode 100644 index 0000000..52c8a13 --- /dev/null +++ b/ComputerShopBusinessLogic/BusinessLogics/WorkModeling.cs @@ -0,0 +1,140 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.BusinessLogicsContracts; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.ViewModels; +using ComputerShopDataModels.Enums; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopBusinessLogic.BusinessLogics +{ + public class WorkModeling : IWorkProcess + { + private readonly ILogger _logger; + + private readonly Random _rnd; + + private IOrderLogic? _orderLogic; + + public WorkModeling(ILogger logger) + { + _logger = logger; + _rnd = new Random(1000); + } + + public void DoWork(IImplementerLogic implementerLogic, IOrderLogic orderLogic) + { + _orderLogic = orderLogic; + var implementers = implementerLogic.ReadList(null); + if (implementers == null) + { + _logger.LogWarning("DoWork. Implementers is null"); + return; + } + var orders = _orderLogic.ReadList(new OrderSearchModel { Status = OrderStatus.Принят }); + if (orders == null || orders.Count == 0) + { + _logger.LogWarning("DoWork. Orders is null or empty"); + return; + } + _logger.LogDebug("DoWork for {Count} orders", orders.Count); + foreach (var implementer in implementers) + { + Task.Run(() => WorkerWorkAsync(implementer, orders)); + } + } + + private async Task WorkerWorkAsync(ImplementerViewModel implementer, List orders) + { + if (_orderLogic == null || implementer == null) + { + return; + } + await RunOrderInWork(implementer, orders); + + await Task.Run(() => + { + foreach (var order in orders) + { + try + { + _logger.LogDebug("DoWork. Worker {Id} try get order {Order}", implementer.Id, order.Id); + // пытаемся назначить заказ на исполнителя + if (_orderLogic.TakeOrderInWork(new OrderBindingModel + { + Id = order.Id, + ImplementerId = implementer.Id + })) + { + // делаем работу + Thread.Sleep(implementer.WorkExperience * _rnd.Next(100, 1000) * order.Count); + _logger.LogDebug("DoWork. Worker {Id} finish order {Order}", implementer.Id, order.Id); + _orderLogic.DeliveryOrder(new OrderBindingModel + { + Id = order.Id, + ImplementerId = implementer.Id + }); + // отдыхаем + Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100)); + } + + } + // кто-то мог уже перехватить заказ, игнорируем ошибку + catch (InvalidOperationException ex) + { + _logger.LogWarning(ex, "Error try get work"); + } + // заканчиваем выполнение имитации в случае иной ошибки + catch (Exception ex) + { + _logger.LogError(ex, "Error while do work"); + throw; + } + } + }); + } + + private async Task RunOrderInWork(ImplementerViewModel implementer, List allOrders) + { + if (_orderLogic == null || implementer == null || allOrders == null || allOrders.Count == 0) + { + return; + } + try + { + // Выбираем из всех заказов тот, который выполняется данным исполнителем + var runOrder = await Task.Run(() => allOrders.FirstOrDefault(x => x.ImplementerId == implementer.Id && x.Status == OrderStatus.Выполняется)); + if (runOrder == null) + { + return; + } + + _logger.LogDebug("DoWork. Worker {Id} back to order {Order}", implementer.Id, runOrder.Id); + // доделываем работу + Thread.Sleep(implementer.WorkExperience * _rnd.Next(100, 300) * runOrder.Count); + _logger.LogDebug("DoWork. Worker {Id} finish order {Order}", implementer.Id, runOrder.Id); + _orderLogic.FinishOrder(new OrderBindingModel + { + Id = runOrder.Id + }); + // отдыхаем + Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100)); + } + // заказа может не быть, просто игнорируем ошибку + catch (InvalidOperationException ex) + { + _logger.LogWarning(ex, "Error try get work"); + } + // а может возникнуть иная ошибка, тогда просто заканчиваем выполнение имитации + catch (Exception ex) + { + _logger.LogError(ex, "Error while do work"); + throw; + } + } + } +} diff --git a/ComputerShopContracts/BindingModels/ImplementerBindingModel.cs b/ComputerShopContracts/BindingModels/ImplementerBindingModel.cs new file mode 100644 index 0000000..bd81e8b --- /dev/null +++ b/ComputerShopContracts/BindingModels/ImplementerBindingModel.cs @@ -0,0 +1,22 @@ +using ComputerShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopContracts.BindingModels +{ + public class ImplementerBindingModel : IImplementerModel + { + public int Id { get; set; } + + public string ImplementerFIO { get; set; } = string.Empty; + + public string Password { get; set; } = string.Empty; + + public int WorkExperience { get; set; } + + public int Qualification { get; set; } + } +} diff --git a/ComputerShopContracts/BindingModels/OrderBindingModel.cs b/ComputerShopContracts/BindingModels/OrderBindingModel.cs index 1743d63..7012e4b 100644 --- a/ComputerShopContracts/BindingModels/OrderBindingModel.cs +++ b/ComputerShopContracts/BindingModels/OrderBindingModel.cs @@ -12,6 +12,7 @@ namespace ComputerShopContracts.BindingModels { public int Id { get; set; } public int ClientId { get; set; } + public int? ImplementerId { get; set; } public int ComputerId { get; set; } public string ComputerName { get; set; } = string.Empty; public int Count { get; set; } diff --git a/ComputerShopContracts/BusinessLogicsContracts/IImplementerLogic.cs b/ComputerShopContracts/BusinessLogicsContracts/IImplementerLogic.cs new file mode 100644 index 0000000..a3b75e0 --- /dev/null +++ b/ComputerShopContracts/BusinessLogicsContracts/IImplementerLogic.cs @@ -0,0 +1,24 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopContracts.BusinessLogicsContracts +{ + public interface IImplementerLogic + { + List? ReadList(ImplementerSearchModel? model); + + ImplementerViewModel? ReadElement(ImplementerSearchModel model); + + bool Create(ImplementerBindingModel model); + + bool Update(ImplementerBindingModel model); + + bool Delete(ImplementerBindingModel model); + } +} diff --git a/ComputerShopContracts/BusinessLogicsContracts/IOrderLogic.cs b/ComputerShopContracts/BusinessLogicsContracts/IOrderLogic.cs index 1826ec0..0149576 100644 --- a/ComputerShopContracts/BusinessLogicsContracts/IOrderLogic.cs +++ b/ComputerShopContracts/BusinessLogicsContracts/IOrderLogic.cs @@ -13,6 +13,7 @@ namespace ComputerShopContracts.BusinessLogicsContracts { List? ReadList(OrderSearchModel? model); bool CreateOrder(OrderBindingModel model); + OrderViewModel? ReadElement(OrderSearchModel model); bool TakeOrderInWork(OrderBindingModel model); bool FinishOrder(OrderBindingModel model); bool DeliveryOrder(OrderBindingModel model); diff --git a/ComputerShopContracts/BusinessLogicsContracts/IWorkProcess.cs b/ComputerShopContracts/BusinessLogicsContracts/IWorkProcess.cs new file mode 100644 index 0000000..77928c1 --- /dev/null +++ b/ComputerShopContracts/BusinessLogicsContracts/IWorkProcess.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopContracts.BusinessLogicsContracts +{ + public interface IWorkProcess + { + void DoWork(IImplementerLogic implementerLogic, IOrderLogic orderLogic); + } +} diff --git a/ComputerShopContracts/SearchModels/ImplementerSearchModel.cs b/ComputerShopContracts/SearchModels/ImplementerSearchModel.cs new file mode 100644 index 0000000..564396f --- /dev/null +++ b/ComputerShopContracts/SearchModels/ImplementerSearchModel.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopContracts.SearchModels +{ + public class ImplementerSearchModel + { + public int? Id { get; set; } + + public string? ImplementerFIO { get; set; } + + public string? Password { get; set; } + } +} diff --git a/ComputerShopContracts/SearchModels/OrderSearchModel.cs b/ComputerShopContracts/SearchModels/OrderSearchModel.cs index 531d720..caa3528 100644 --- a/ComputerShopContracts/SearchModels/OrderSearchModel.cs +++ b/ComputerShopContracts/SearchModels/OrderSearchModel.cs @@ -1,4 +1,5 @@ -using System; +using ComputerShopDataModels.Enums; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -12,6 +13,8 @@ namespace ComputerShopContracts.SearchModels public DateTime? DateFrom { get; set; } public DateTime? DateTo { get; set; } public int? ClientId { get; set; } + public int? ImplementerId { get; set; } + public OrderStatus? Status { get; set; } } } diff --git a/ComputerShopContracts/StoragesContracts/IImplementerStorage.cs b/ComputerShopContracts/StoragesContracts/IImplementerStorage.cs new file mode 100644 index 0000000..8cd03ff --- /dev/null +++ b/ComputerShopContracts/StoragesContracts/IImplementerStorage.cs @@ -0,0 +1,26 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopContracts.StoragesContracts +{ + public interface IImplementerStorage + { + List GetFullList(); + + List GetFilteredList(ImplementerSearchModel model); + + ImplementerViewModel? GetElement(ImplementerSearchModel model); + + ImplementerViewModel? Insert(ImplementerBindingModel model); + + ImplementerViewModel? Update(ImplementerBindingModel model); + + ImplementerViewModel? Delete(ImplementerBindingModel model); + } +} diff --git a/ComputerShopContracts/ViewModels/ImplementerViewModel.cs b/ComputerShopContracts/ViewModels/ImplementerViewModel.cs new file mode 100644 index 0000000..a7b530e --- /dev/null +++ b/ComputerShopContracts/ViewModels/ImplementerViewModel.cs @@ -0,0 +1,27 @@ +using ComputerShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopContracts.ViewModels +{ + public class ImplementerViewModel : IImplementerModel + { + public int Id { get; set; } + + [DisplayName("ФИО")] + public string ImplementerFIO { get; set; } = string.Empty; + + [DisplayName("Пароль")] + public string Password { get; set; } = string.Empty; + + [DisplayName("Трудовой стаж")] + public int WorkExperience { get; set; } + + [DisplayName("Квалификация")] + public int Qualification { get; set; } + } +} diff --git a/ComputerShopContracts/ViewModels/OrderViewModel.cs b/ComputerShopContracts/ViewModels/OrderViewModel.cs index 63a8bcd..5b36388 100644 --- a/ComputerShopContracts/ViewModels/OrderViewModel.cs +++ b/ComputerShopContracts/ViewModels/OrderViewModel.cs @@ -22,6 +22,9 @@ namespace ComputerShopContracts.ViewModels [DisplayName("ФИО клиента")] public string ClientFIO { get; set; } = string.Empty; + public int? ImplementerId { get; set; } + [DisplayName("ФИО исполнителя")] + public string ImplementerFIO { get; set; } = string.Empty; [DisplayName("Количество")] public int Count { get; set; } diff --git a/ComputerShopDatabaseImplement/ComputerShopDatabase.cs b/ComputerShopDatabaseImplement/ComputerShopDatabase.cs index 353eb25..570a408 100644 --- a/ComputerShopDatabaseImplement/ComputerShopDatabase.cs +++ b/ComputerShopDatabaseImplement/ComputerShopDatabase.cs @@ -23,6 +23,7 @@ namespace ComputerShopDatabaseImplement public virtual DbSet ComputerComponents { set; get; } public virtual DbSet Orders { set; get; } public virtual DbSet Clients { set; get; } + public virtual DbSet Implementers { set; get; } } } diff --git a/ComputerShopDatabaseImplement/Implements/ImplementerStorage.cs b/ComputerShopDatabaseImplement/Implements/ImplementerStorage.cs new file mode 100644 index 0000000..30ac801 --- /dev/null +++ b/ComputerShopDatabaseImplement/Implements/ImplementerStorage.cs @@ -0,0 +1,83 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.StoragesContracts; +using ComputerShopContracts.ViewModels; +using ComputerShopDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopDatabaseImplement.Implements +{ + public class ImplementerStorage : IImplementerStorage + { + public ImplementerViewModel? GetElement(ImplementerSearchModel model) + { + using var context = new ComputerShopDatabase(); + if (model.Id.HasValue) return context.Implementers.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + if (model.ImplementerFIO != null && model.Password != null) return context.Implementers.FirstOrDefault(x => x.ImplementerFIO.Equals(model.ImplementerFIO) && x.Password.Equals(model.Password))?.GetViewModel; + if (model.ImplementerFIO != null) return context.Implementers.FirstOrDefault(x => x.ImplementerFIO.Equals(model.ImplementerFIO))?.GetViewModel; + return null; + } + + public List GetFilteredList(ImplementerSearchModel model) + { + if (model == null) + { + return new(); + } + if (model.ImplementerFIO != null) + { + using var context = new ComputerShopDatabase(); + return context.Implementers + .Where(x => x.ImplementerFIO.Contains(model.ImplementerFIO)) + .Select(x => x.GetViewModel) + .ToList(); + } + return new(); + } + + public List GetFullList() + { + using var context = new ComputerShopDatabase(); + return context.Implementers.Select(x => x.GetViewModel).ToList(); + } + + public ImplementerViewModel? Insert(ImplementerBindingModel model) + { + using var context = new ComputerShopDatabase(); + var res = Implementer.Create(model); + if (res != null) + { + context.Implementers.Add(res); + context.SaveChanges(); + } + return res?.GetViewModel; + } + + public ImplementerViewModel? Update(ImplementerBindingModel model) + { + using var context = new ComputerShopDatabase(); + var res = context.Implementers.FirstOrDefault(x => x.Id == model.Id); + if (res != null) + { + res.Update(model); + context.SaveChanges(); + } + return res?.GetViewModel; + } + public ImplementerViewModel? Delete(ImplementerBindingModel model) + { + using var context = new ComputerShopDatabase(); + var res = context.Implementers.FirstOrDefault(x => x.Id == model.Id); + if (res != null) + { + context.Implementers.Remove(res); + context.SaveChanges(); + } + return res?.GetViewModel; + } + } +} diff --git a/ComputerShopDatabaseImplement/Implements/OrderStorage.cs b/ComputerShopDatabaseImplement/Implements/OrderStorage.cs index e2af06d..80abc4f 100644 --- a/ComputerShopDatabaseImplement/Implements/OrderStorage.cs +++ b/ComputerShopDatabaseImplement/Implements/OrderStorage.cs @@ -21,12 +21,19 @@ namespace ComputerShopDatabaseImplement.Implements return null; } using var context = new ComputerShopDatabase(); - return context.Orders.Include(x => x.Computer).FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + return context.Orders + .Include(x => x.Computer) + .Include(x => x.Client) + .Include(x => x.Implementer) + .FirstOrDefault(x => + (model.Status == null || model.Status != null && model.Status.Equals(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 && !model.DateFrom.HasValue && !model.DateTo.HasValue && !model.ClientId.HasValue) + if (!model.Id.HasValue && !model.DateFrom.HasValue && !model.DateTo.HasValue && !model.ClientId.HasValue && model.Status == null) { return new(); } @@ -35,22 +42,35 @@ namespace ComputerShopDatabaseImplement.Implements { return context.Orders .Include(x => x.Client) + .Include(x => x.Implementer) .Where(x => x.ClientId == model.ClientId) .Select(x => x.GetViewModel) .ToList(); } + if (model.Status != null) + { + return context.Orders + .Include(x => x.Computer) + .Include(x => x.Client) + .Include(x => x.Implementer) + .Where(x => model.Status.Equals(x.Status)) + .Select(x => x.GetViewModel) + .ToList(); + } return context.Orders - .Include(x => x.Computer) - .Where(x => x.Id == model.Id || model.DateFrom <= x.DateCreate && x.DateCreate <= model.DateTo) - .Select(x => x.GetViewModel) - .ToList(); + .Include(x => x.Computer) + .Include(x => x.Client) + .Include(x => x.Implementer) + .Where(x => x.Id == model.Id || model.DateFrom <= x.DateCreate && x.DateCreate <= model.DateTo) + .Select(x => x.GetViewModel) + .ToList(); } public List GetFullList() { using var context = new ComputerShopDatabase(); - return context.Orders.Include(x => x.Computer).Select(x => x.GetViewModel).ToList(); + return context.Orders.Include(x => x.Computer).Include(x => x.Client).Include(x => x.Implementer).Select(x => x.GetViewModel).ToList(); } public OrderViewModel? Insert(OrderBindingModel model) @@ -63,7 +83,7 @@ namespace ComputerShopDatabaseImplement.Implements using var context = new ComputerShopDatabase(); context.Orders.Add(newOrder); context.SaveChanges(); - return context.Orders.Include(x => x.Computer).FirstOrDefault(x => x.Id == newOrder.Id)?.GetViewModel; + return context.Orders.Include(x => x.Computer).Include(x => x.Client).Include(x => x.Implementer).FirstOrDefault(x => x.Id == newOrder.Id)?.GetViewModel; } public OrderViewModel? Update(OrderBindingModel model) @@ -76,12 +96,16 @@ namespace ComputerShopDatabaseImplement.Implements } order.Update(model); context.SaveChanges(); - return context.Orders.Include(x => x.Computer).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + return context.Orders.Include(x => x.Computer).Include(x => x.Client).Include(x => x.Implementer).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; } public OrderViewModel? Delete(OrderBindingModel model) { using var context = new ComputerShopDatabase(); - var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + var element = context.Orders + .Include(x => x.Computer) + .Include(x => x.Client) + .Include(x => x.Implementer) + .FirstOrDefault(x => x.Id == model.Id); if (element != null) { context.Orders.Remove(element); diff --git a/ComputerShopDatabaseImplement/Migrations/20230419114414_ImplementerMig.Designer.cs b/ComputerShopDatabaseImplement/Migrations/20230419114414_ImplementerMig.Designer.cs new file mode 100644 index 0000000..aba0d53 --- /dev/null +++ b/ComputerShopDatabaseImplement/Migrations/20230419114414_ImplementerMig.Designer.cs @@ -0,0 +1,261 @@ +// +using System; +using ComputerShopDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace ComputerShopDatabaseImplement.Migrations +{ + [DbContext(typeof(ComputerShopDatabase))] + [Migration("20230419114414_ImplementerMig")] + partial class ImplementerMig + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.4") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("ComputerShopDatabaseImplement.Models.Client", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ClientFIO") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Email") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Password") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Clients"); + }); + + modelBuilder.Entity("ComputerShopDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("ComputerShopDatabaseImplement.Models.Computer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComputerName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Computers"); + }); + + modelBuilder.Entity("ComputerShopDatabaseImplement.Models.ComputerComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("ComputerId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("ComputerId"); + + b.ToTable("ComputerComponents"); + }); + + modelBuilder.Entity("ComputerShopDatabaseImplement.Models.Implementer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ImplementerFIO") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Password") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Qualification") + .HasColumnType("int"); + + b.Property("WorkExperience") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("Implementers"); + }); + + modelBuilder.Entity("ComputerShopDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ClientId") + .HasColumnType("int"); + + b.Property("ComputerId") + .HasColumnType("int"); + + b.Property("ComputerName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("ImplementerId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("ClientId"); + + b.HasIndex("ComputerId"); + + b.HasIndex("ImplementerId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("ComputerShopDatabaseImplement.Models.ComputerComponent", b => + { + b.HasOne("ComputerShopDatabaseImplement.Models.Component", "Component") + .WithMany("ComputerComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ComputerShopDatabaseImplement.Models.Computer", "Computer") + .WithMany("Components") + .HasForeignKey("ComputerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Computer"); + }); + + modelBuilder.Entity("ComputerShopDatabaseImplement.Models.Order", b => + { + b.HasOne("ComputerShopDatabaseImplement.Models.Client", "Client") + .WithMany("Orders") + .HasForeignKey("ClientId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ComputerShopDatabaseImplement.Models.Computer", "Computer") + .WithMany("Orders") + .HasForeignKey("ComputerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ComputerShopDatabaseImplement.Models.Implementer", "Implementer") + .WithMany("Orders") + .HasForeignKey("ImplementerId"); + + b.Navigation("Client"); + + b.Navigation("Computer"); + + b.Navigation("Implementer"); + }); + + modelBuilder.Entity("ComputerShopDatabaseImplement.Models.Client", b => + { + b.Navigation("Orders"); + }); + + modelBuilder.Entity("ComputerShopDatabaseImplement.Models.Component", b => + { + b.Navigation("ComputerComponents"); + }); + + modelBuilder.Entity("ComputerShopDatabaseImplement.Models.Computer", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); + + modelBuilder.Entity("ComputerShopDatabaseImplement.Models.Implementer", b => + { + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/ComputerShopDatabaseImplement/Migrations/20230419114414_ImplementerMig.cs b/ComputerShopDatabaseImplement/Migrations/20230419114414_ImplementerMig.cs new file mode 100644 index 0000000..611b0fc --- /dev/null +++ b/ComputerShopDatabaseImplement/Migrations/20230419114414_ImplementerMig.cs @@ -0,0 +1,67 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace ComputerShopDatabaseImplement.Migrations +{ + /// + public partial class ImplementerMig : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "ImplementerId", + table: "Orders", + type: "int", + nullable: true); + + migrationBuilder.CreateTable( + name: "Implementers", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ImplementerFIO = table.Column(type: "nvarchar(max)", nullable: false), + Password = table.Column(type: "nvarchar(max)", nullable: false), + WorkExperience = table.Column(type: "int", nullable: false), + Qualification = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Implementers", x => x.Id); + }); + + migrationBuilder.CreateIndex( + name: "IX_Orders_ImplementerId", + table: "Orders", + column: "ImplementerId"); + + migrationBuilder.AddForeignKey( + name: "FK_Orders_Implementers_ImplementerId", + table: "Orders", + column: "ImplementerId", + principalTable: "Implementers", + principalColumn: "Id"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Orders_Implementers_ImplementerId", + table: "Orders"); + + migrationBuilder.DropTable( + name: "Implementers"); + + migrationBuilder.DropIndex( + name: "IX_Orders_ImplementerId", + table: "Orders"); + + migrationBuilder.DropColumn( + name: "ImplementerId", + table: "Orders"); + } + } +} diff --git a/ComputerShopDatabaseImplement/Migrations/ComputerShopDatabaseModelSnapshot.cs b/ComputerShopDatabaseImplement/Migrations/ComputerShopDatabaseModelSnapshot.cs index 3de14ac..2f07b57 100644 --- a/ComputerShopDatabaseImplement/Migrations/ComputerShopDatabaseModelSnapshot.cs +++ b/ComputerShopDatabaseImplement/Migrations/ComputerShopDatabaseModelSnapshot.cs @@ -113,6 +113,33 @@ namespace ComputerShopDatabaseImplement.Migrations b.ToTable("ComputerComponents"); }); + modelBuilder.Entity("ComputerShopDatabaseImplement.Models.Implementer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ImplementerFIO") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Password") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Qualification") + .HasColumnType("int"); + + b.Property("WorkExperience") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("Implementers"); + }); + modelBuilder.Entity("ComputerShopDatabaseImplement.Models.Order", b => { b.Property("Id") @@ -140,6 +167,9 @@ namespace ComputerShopDatabaseImplement.Migrations b.Property("DateImplement") .HasColumnType("datetime2"); + b.Property("ImplementerId") + .HasColumnType("int"); + b.Property("Status") .HasColumnType("int"); @@ -152,6 +182,8 @@ namespace ComputerShopDatabaseImplement.Migrations b.HasIndex("ComputerId"); + b.HasIndex("ImplementerId"); + b.ToTable("Orders"); }); @@ -188,9 +220,15 @@ namespace ComputerShopDatabaseImplement.Migrations .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + b.HasOne("ComputerShopDatabaseImplement.Models.Implementer", "Implementer") + .WithMany("Orders") + .HasForeignKey("ImplementerId"); + b.Navigation("Client"); b.Navigation("Computer"); + + b.Navigation("Implementer"); }); modelBuilder.Entity("ComputerShopDatabaseImplement.Models.Client", b => @@ -209,6 +247,11 @@ namespace ComputerShopDatabaseImplement.Migrations b.Navigation("Orders"); }); + + modelBuilder.Entity("ComputerShopDatabaseImplement.Models.Implementer", b => + { + b.Navigation("Orders"); + }); #pragma warning restore 612, 618 } } diff --git a/ComputerShopDatabaseImplement/Models/Implementer.cs b/ComputerShopDatabaseImplement/Models/Implementer.cs new file mode 100644 index 0000000..0f9f47d --- /dev/null +++ b/ComputerShopDatabaseImplement/Models/Implementer.cs @@ -0,0 +1,65 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.ViewModels; +using ComputerShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopDatabaseImplement.Models +{ + internal class Implementer : IImplementerModel + { + public int Id { get; private set; } + + public string ImplementerFIO { get; private set; } = string.Empty; + + public string Password { get; private set; } = string.Empty; + + public int WorkExperience { get; private set; } + + public int Qualification { get; private set; } + + [ForeignKey("ImplementerId")] + public virtual List Orders { get; private set; } = new(); + + public static Implementer? Create(ImplementerBindingModel model) + { + if (model == null) + { + return null; + } + return new() + { + Id = model.Id, + ImplementerFIO = model.ImplementerFIO, + Password = model.Password, + Qualification = model.Qualification, + WorkExperience = model.WorkExperience, + }; + } + + public void Update(ImplementerBindingModel model) + { + if (model == null) + { + return; + } + ImplementerFIO = model.ImplementerFIO; + Password = model.Password; + Qualification = model.Qualification; + WorkExperience = model.WorkExperience; + } + + public ImplementerViewModel GetViewModel => new() + { + Id = Id, + ImplementerFIO = ImplementerFIO, + Password = Password, + Qualification = Qualification, + WorkExperience = WorkExperience, + }; + } +} diff --git a/ComputerShopDatabaseImplement/Models/Order.cs b/ComputerShopDatabaseImplement/Models/Order.cs index 173f41e..f138eba 100644 --- a/ComputerShopDatabaseImplement/Models/Order.cs +++ b/ComputerShopDatabaseImplement/Models/Order.cs @@ -23,6 +23,7 @@ namespace ComputerShopDatabaseImplement.Models public int ComputerId { get; private set; } [Required] public int ClientId { get; private set; } + public int? ImplementerId { get; private set; } [Required] public int Count { get; private set; } [Required] @@ -36,6 +37,7 @@ namespace ComputerShopDatabaseImplement.Models public virtual Computer Computer { get; set; } public virtual Client Client { get; set; } + public virtual Implementer? Implementer { get; set; } public static Order? Create(OrderBindingModel? model) { @@ -48,6 +50,7 @@ namespace ComputerShopDatabaseImplement.Models ComputerId = model.ComputerId, ComputerName = model.ComputerName, ClientId = model.ClientId, + ImplementerId = model.ImplementerId, Count = model.Count, Sum = model.Sum, Status = model.Status, @@ -65,6 +68,7 @@ namespace ComputerShopDatabaseImplement.Models } Status = model.Status; DateImplement = model.DateImplement; + ImplementerId = model.ImplementerId; } public OrderViewModel GetViewModel @@ -76,6 +80,7 @@ namespace ComputerShopDatabaseImplement.Models { ComputerId = ComputerId, ClientId = ClientId, + ImplementerId = ImplementerId, Count = Count, Sum = Sum, DateCreate = DateCreate, @@ -83,7 +88,8 @@ namespace ComputerShopDatabaseImplement.Models Id = Id, Status = Status, ComputerName = context.Computers.FirstOrDefault(x => x.Id == ComputerId)?.ComputerName ?? string.Empty, - ClientFIO = context.Clients.FirstOrDefault(x => x.Id == ClientId)?.ClientFIO ?? string.Empty + ClientFIO = context.Clients.FirstOrDefault(x => x.Id == ClientId)?.ClientFIO ?? string.Empty, + ImplementerFIO = Implementer?.ImplementerFIO ?? string.Empty }; } } diff --git a/ComputerShopFileImplement/Models/Order.cs b/ComputerShopFileImplement/Models/Order.cs index 8aad8b2..b8e5412 100644 --- a/ComputerShopFileImplement/Models/Order.cs +++ b/ComputerShopFileImplement/Models/Order.cs @@ -14,6 +14,8 @@ namespace ComputerShopFileImplement.Models public class Order : IOrderModel { public int ComputerId { get; private set; } + public int ClientId { get; private set; } + public int? ImplementerId { get; private set; } public string ComputerName { get; private set; } = string.Empty; diff --git a/ComputerShopListImplement/Models/Order.cs b/ComputerShopListImplement/Models/Order.cs index 346f79d..ecc0952 100644 --- a/ComputerShopListImplement/Models/Order.cs +++ b/ComputerShopListImplement/Models/Order.cs @@ -15,6 +15,8 @@ namespace ComputerShopListImplement.Models public int ComputerId { get; private set; } public string ComputerName { get; private set; } = string.Empty; + public int ClientId { get; private set; } + public int? ImplementerId { get; private set; } public int Count { get; private set; } diff --git a/ComputerShopRestApi/Controllers/ImplementerController.cs b/ComputerShopRestApi/Controllers/ImplementerController.cs new file mode 100644 index 0000000..8a5ffee --- /dev/null +++ b/ComputerShopRestApi/Controllers/ImplementerController.cs @@ -0,0 +1,100 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.BusinessLogicsContracts; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.ViewModels; +using ComputerShopDataModels.Enums; +using DocumentFormat.OpenXml.Office2010.Excel; +using Microsoft.AspNetCore.Mvc; +namespace AbstractShopRestApi.Controllers +{ + [Route("api/[controller]/[action]")] + [ApiController] + public class ImplementerController : Controller + { + private readonly ILogger _logger; + private readonly IOrderLogic _order; + private readonly IImplementerLogic _logic; + public ImplementerController(IOrderLogic order, IImplementerLogic logic, ILogger logger) + { + _logger = logger; + _order = order; + _logic = logic; + } + [HttpGet] + public ImplementerViewModel? Login(string login, string password) + { + try + { + return _logic.ReadElement(new ImplementerSearchModel + { + ImplementerFIO = login, + Password = password + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка авторизации сотрудника"); + throw; + } + } + [HttpGet] + public List? GetNewOrders() + { + try + { + return _order.ReadList(new OrderSearchModel + { + Status = OrderStatus.Принят + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения новых заказов"); + throw; + } + } + [HttpGet] + public OrderViewModel? GetImplementerOrder(int implementerId) + { + try + { + return _order.ReadElement(new OrderSearchModel + { + ImplementerId = implementerId + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения текущего заказа исполнителя"); + throw; + } + } + [HttpPost] + public void TakeOrderInWork(OrderBindingModel model) + { + try + { + _order.TakeOrderInWork(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка перевода заказа с No{Id} в работу", + model.Id); + throw; + } + } + [HttpPost] + public void FinishOrder(OrderBindingModel model) + { + try + { + _order.FinishOrder(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка отметки о готовности заказа с No{ Id}", model.Id); + throw; + } + } + } +} \ No newline at end of file diff --git a/ComputersShop/FormImplementer.Designer.cs b/ComputersShop/FormImplementer.Designer.cs new file mode 100644 index 0000000..f810932 --- /dev/null +++ b/ComputersShop/FormImplementer.Designer.cs @@ -0,0 +1,173 @@ +namespace ComputersShop +{ + partial class FormImplementer + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.buttonCancel = new System.Windows.Forms.Button(); + this.buttonSave = new System.Windows.Forms.Button(); + this.numericUpDownQualification = new System.Windows.Forms.NumericUpDown(); + this.numericUpDownWorkExp = new System.Windows.Forms.NumericUpDown(); + this.textBoxPassword = new System.Windows.Forms.TextBox(); + this.textBoxFIO = new System.Windows.Forms.TextBox(); + this.labelQualification = new System.Windows.Forms.Label(); + this.labelWorkExp = new System.Windows.Forms.Label(); + this.labelPassword = new System.Windows.Forms.Label(); + this.labelFIO = new System.Windows.Forms.Label(); + ((System.ComponentModel.ISupportInitialize)(this.numericUpDownQualification)).BeginInit(); + ((System.ComponentModel.ISupportInitialize)(this.numericUpDownWorkExp)).BeginInit(); + this.SuspendLayout(); + // + // buttonCancel + // + this.buttonCancel.Location = new System.Drawing.Point(370, 149); + this.buttonCancel.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(93, 22); + this.buttonCancel.TabIndex = 19; + this.buttonCancel.Text = "Отмена"; + this.buttonCancel.UseVisualStyleBackColor = true; + this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click); + // + // buttonSave + // + this.buttonSave.Location = new System.Drawing.Point(275, 149); + this.buttonSave.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonSave.Name = "buttonSave"; + this.buttonSave.Size = new System.Drawing.Size(90, 22); + this.buttonSave.TabIndex = 18; + this.buttonSave.Text = "Сохранить"; + this.buttonSave.UseVisualStyleBackColor = true; + this.buttonSave.Click += new System.EventHandler(this.buttonSave_Click); + // + // numericUpDownQualification + // + this.numericUpDownQualification.Location = new System.Drawing.Point(127, 105); + this.numericUpDownQualification.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.numericUpDownQualification.Name = "numericUpDownQualification"; + this.numericUpDownQualification.Size = new System.Drawing.Size(159, 23); + this.numericUpDownQualification.TabIndex = 17; + // + // numericUpDownWorkExp + // + this.numericUpDownWorkExp.Location = new System.Drawing.Point(127, 73); + this.numericUpDownWorkExp.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.numericUpDownWorkExp.Name = "numericUpDownWorkExp"; + this.numericUpDownWorkExp.Size = new System.Drawing.Size(159, 23); + this.numericUpDownWorkExp.TabIndex = 16; + // + // textBoxPassword + // + this.textBoxPassword.Location = new System.Drawing.Point(127, 38); + this.textBoxPassword.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.textBoxPassword.Name = "textBoxPassword"; + this.textBoxPassword.Size = new System.Drawing.Size(160, 23); + this.textBoxPassword.TabIndex = 15; + // + // textBoxFIO + // + this.textBoxFIO.Location = new System.Drawing.Point(127, 9); + this.textBoxFIO.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.textBoxFIO.Name = "textBoxFIO"; + this.textBoxFIO.Size = new System.Drawing.Size(325, 23); + this.textBoxFIO.TabIndex = 14; + // + // labelQualification + // + this.labelQualification.AutoSize = true; + this.labelQualification.Location = new System.Drawing.Point(12, 107); + this.labelQualification.Name = "labelQualification"; + this.labelQualification.Size = new System.Drawing.Size(91, 15); + this.labelQualification.TabIndex = 13; + this.labelQualification.Text = "Квалификация:"; + // + // labelWorkExp + // + this.labelWorkExp.AutoSize = true; + this.labelWorkExp.Location = new System.Drawing.Point(12, 74); + this.labelWorkExp.Name = "labelWorkExp"; + this.labelWorkExp.Size = new System.Drawing.Size(82, 15); + this.labelWorkExp.TabIndex = 12; + this.labelWorkExp.Text = "Стаж работы:"; + // + // labelPassword + // + this.labelPassword.AutoSize = true; + this.labelPassword.Location = new System.Drawing.Point(12, 43); + this.labelPassword.Name = "labelPassword"; + this.labelPassword.Size = new System.Drawing.Size(52, 15); + this.labelPassword.TabIndex = 11; + this.labelPassword.Text = "Пароль:"; + // + // labelFIO + // + this.labelFIO.AutoSize = true; + this.labelFIO.Location = new System.Drawing.Point(12, 9); + this.labelFIO.Name = "labelFIO"; + this.labelFIO.Size = new System.Drawing.Size(40, 15); + this.labelFIO.TabIndex = 10; + this.labelFIO.Text = "ФИО: "; + // + // FormImplementer + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(479, 190); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.buttonSave); + this.Controls.Add(this.numericUpDownQualification); + this.Controls.Add(this.numericUpDownWorkExp); + this.Controls.Add(this.textBoxPassword); + this.Controls.Add(this.textBoxFIO); + this.Controls.Add(this.labelQualification); + this.Controls.Add(this.labelWorkExp); + this.Controls.Add(this.labelPassword); + this.Controls.Add(this.labelFIO); + this.Name = "FormImplementer"; + this.Text = "FormImplementer"; + this.Load += new System.EventHandler(this.FormImplementer_Load); + ((System.ComponentModel.ISupportInitialize)(this.numericUpDownQualification)).EndInit(); + ((System.ComponentModel.ISupportInitialize)(this.numericUpDownWorkExp)).EndInit(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private Button buttonCancel; + private Button buttonSave; + private NumericUpDown numericUpDownQualification; + private NumericUpDown numericUpDownWorkExp; + private TextBox textBoxPassword; + private TextBox textBoxFIO; + private Label labelQualification; + private Label labelWorkExp; + private Label labelPassword; + private Label labelFIO; + } +} \ No newline at end of file diff --git a/ComputersShop/FormImplementer.cs b/ComputersShop/FormImplementer.cs new file mode 100644 index 0000000..a89d585 --- /dev/null +++ b/ComputersShop/FormImplementer.cs @@ -0,0 +1,101 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.BusinessLogicsContracts; +using ComputerShopContracts.SearchModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace ComputersShop +{ + public partial class FormImplementer : Form + { + private readonly ILogger _logger; + private readonly IImplementerLogic _logic; + private int? _id; + public int Id { set { _id = value; } } + public FormImplementer(ILogger logger, IImplementerLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + } + private void FormImplementer_Load(object sender, EventArgs e) + { + if (_id.HasValue) + { + try + { + _logger.LogInformation("Получение информации исполнителя"); + var view = _logic.ReadElement(new ImplementerSearchModel + { + Id = _id.Value + }); + if (view != null) + { + textBoxFIO.Text = view.ImplementerFIO; + textBoxPassword.Text = view.Password; + numericUpDownQualification.Value = view.Qualification; + numericUpDownWorkExp.Value = view.WorkExperience; + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения исполнителя"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + + private void buttonSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxPassword.Text)) + { + MessageBox.Show("Заполните пароль", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (string.IsNullOrEmpty(textBoxFIO.Text)) + { + MessageBox.Show("Заполните ФИО", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Сохранение исполнителя"); + try + { + var model = new ImplementerBindingModel + { + Id = _id ?? 0, + ImplementerFIO = textBoxFIO.Text, + Password = textBoxPassword.Text, + Qualification = (int)numericUpDownQualification.Value, + WorkExperience = (int)numericUpDownWorkExp.Value, + }; + var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); + if (!operationResult) + { + throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); + } + MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка сохранения исполнителя"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void buttonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} diff --git a/ComputersShop/FormImplementer.resx b/ComputersShop/FormImplementer.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/ComputersShop/FormImplementer.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/ComputersShop/FormImplementers.Designer.cs b/ComputersShop/FormImplementers.Designer.cs new file mode 100644 index 0000000..6eb424a --- /dev/null +++ b/ComputersShop/FormImplementers.Designer.cs @@ -0,0 +1,120 @@ +namespace ComputersShop +{ + partial class FormImplementers + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.dataGridView = new System.Windows.Forms.DataGridView(); + this.buttonRefresh = new System.Windows.Forms.Button(); + this.buttonDelete = new System.Windows.Forms.Button(); + this.buttonEdit = new System.Windows.Forms.Button(); + this.buttonCreate = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); + this.SuspendLayout(); + // + // dataGridView + // + this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView.Location = new System.Drawing.Point(12, 11); + this.dataGridView.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.dataGridView.Name = "dataGridView"; + this.dataGridView.RowHeadersWidth = 51; + this.dataGridView.RowTemplate.Height = 29; + this.dataGridView.Size = new System.Drawing.Size(568, 320); + this.dataGridView.TabIndex = 9; + // + // buttonRefresh + // + this.buttonRefresh.Location = new System.Drawing.Point(586, 90); + this.buttonRefresh.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonRefresh.Name = "buttonRefresh"; + this.buttonRefresh.Size = new System.Drawing.Size(106, 22); + this.buttonRefresh.TabIndex = 8; + this.buttonRefresh.Text = "Обновить"; + this.buttonRefresh.UseVisualStyleBackColor = true; + this.buttonRefresh.Click += new System.EventHandler(this.buttonRefresh_Click); + // + // buttonDelete + // + this.buttonDelete.Location = new System.Drawing.Point(586, 64); + this.buttonDelete.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonDelete.Name = "buttonDelete"; + this.buttonDelete.Size = new System.Drawing.Size(106, 22); + this.buttonDelete.TabIndex = 7; + this.buttonDelete.Text = "Удалить"; + this.buttonDelete.UseVisualStyleBackColor = true; + this.buttonDelete.Click += new System.EventHandler(this.buttonDelete_Click); + // + // buttonEdit + // + this.buttonEdit.Location = new System.Drawing.Point(586, 37); + this.buttonEdit.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonEdit.Name = "buttonEdit"; + this.buttonEdit.Size = new System.Drawing.Size(106, 22); + this.buttonEdit.TabIndex = 6; + this.buttonEdit.Text = "Изменить"; + this.buttonEdit.UseVisualStyleBackColor = true; + this.buttonEdit.Click += new System.EventHandler(this.buttonEdit_Click); + // + // buttonCreate + // + this.buttonCreate.Location = new System.Drawing.Point(586, 11); + this.buttonCreate.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonCreate.Name = "buttonCreate"; + this.buttonCreate.Size = new System.Drawing.Size(106, 22); + this.buttonCreate.TabIndex = 5; + this.buttonCreate.Text = "Добавить"; + this.buttonCreate.UseVisualStyleBackColor = true; + this.buttonCreate.Click += new System.EventHandler(this.buttonCreate_Click); + // + // FormImplementers + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(702, 344); + this.Controls.Add(this.dataGridView); + this.Controls.Add(this.buttonRefresh); + this.Controls.Add(this.buttonDelete); + this.Controls.Add(this.buttonEdit); + this.Controls.Add(this.buttonCreate); + this.Name = "FormImplementers"; + this.Text = "FormImplementers"; + this.Load += new System.EventHandler(this.FormImplementers_Load); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + + private DataGridView dataGridView; + private Button buttonRefresh; + private Button buttonDelete; + private Button buttonEdit; + private Button buttonCreate; + } +} \ No newline at end of file diff --git a/ComputersShop/FormImplementers.cs b/ComputersShop/FormImplementers.cs new file mode 100644 index 0000000..256852d --- /dev/null +++ b/ComputersShop/FormImplementers.cs @@ -0,0 +1,114 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace ComputersShop +{ + public partial class FormImplementers : Form + { + private readonly ILogger _logger; + private readonly IImplementerLogic _logic; + public FormImplementers(ILogger logger, IImplementerLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + } + + private void FormImplementers_Load(object sender, EventArgs e) + { + LoadData(); + } + private void LoadData() + { + try + { + var list = _logic.ReadList(null); + if (list != null) + { + dataGridView.DataSource = list; + dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["ImplementerFIO"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + } + _logger.LogInformation("Загрузка исполнителей"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки исполнителей"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + + private void buttonCreate_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormImplementer)); + if (service is FormImplementer form) + { + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + + private void buttonEdit_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + var service = Program.ServiceProvider?.GetService(typeof(FormImplementer)); + if (service is FormImplementer form) + { + form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + } + + private void buttonDelete_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + int id = + Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + _logger.LogInformation("Удаление исполнителя"); + try + { + if (!_logic.Delete(new ImplementerBindingModel + { + Id = id + })) + { + throw new Exception("Ошибка при удалении. Дополнительная информация в логах."); + } + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка удаления исполнителя"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } + + private void buttonRefresh_Click(object sender, EventArgs e) + { + LoadData(); + } + } +} diff --git a/ComputersShop/FormImplementers.resx b/ComputersShop/FormImplementers.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/ComputersShop/FormImplementers.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/ComputersShop/FormMain.Designer.cs b/ComputersShop/FormMain.Designer.cs index c9635a5..eb3088c 100644 --- a/ComputersShop/FormMain.Designer.cs +++ b/ComputersShop/FormMain.Designer.cs @@ -32,17 +32,17 @@ this.справочникиToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.компьютерыToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.компонентыToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.клиентыToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.исполнителиToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.отчетыToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.списокКомпонентовToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.компонентыПоИзделиямToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.списокЗаказовToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.запускРаботToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.dataGridView = new System.Windows.Forms.DataGridView(); this.ButtonCreateOrder = new System.Windows.Forms.Button(); - this.ButtonTakeOrderInWork = new System.Windows.Forms.Button(); - this.ButtonOrderReady = new System.Windows.Forms.Button(); this.ButtonIssuedOrder = new System.Windows.Forms.Button(); this.ButtonRef = new System.Windows.Forms.Button(); - this.клиентыToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.menuStrip1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); this.SuspendLayout(); @@ -51,7 +51,8 @@ // this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.справочникиToolStripMenuItem, - this.отчетыToolStripMenuItem}); + this.отчетыToolStripMenuItem, + this.запускРаботToolStripMenuItem}); this.menuStrip1.Location = new System.Drawing.Point(0, 0); this.menuStrip1.Name = "menuStrip1"; this.menuStrip1.Size = new System.Drawing.Size(1006, 24); @@ -63,7 +64,8 @@ this.справочникиToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.компьютерыToolStripMenuItem, this.компонентыToolStripMenuItem, - this.клиентыToolStripMenuItem}); + this.клиентыToolStripMenuItem, + this.исполнителиToolStripMenuItem}); this.справочникиToolStripMenuItem.Name = "справочникиToolStripMenuItem"; this.справочникиToolStripMenuItem.Size = new System.Drawing.Size(94, 20); this.справочникиToolStripMenuItem.Text = "Справочники"; @@ -71,17 +73,31 @@ // компьютерыToolStripMenuItem // this.компьютерыToolStripMenuItem.Name = "компьютерыToolStripMenuItem"; - this.компьютерыToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.компьютерыToolStripMenuItem.Size = new System.Drawing.Size(149, 22); this.компьютерыToolStripMenuItem.Text = "Компьютеры"; this.компьютерыToolStripMenuItem.Click += new System.EventHandler(this.КомпьютерыToolStripMenuItem_Click); // // компонентыToolStripMenuItem // this.компонентыToolStripMenuItem.Name = "компонентыToolStripMenuItem"; - this.компонентыToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.компонентыToolStripMenuItem.Size = new System.Drawing.Size(149, 22); this.компонентыToolStripMenuItem.Text = "Компоненты"; this.компонентыToolStripMenuItem.Click += new System.EventHandler(this.КомпонентыToolStripMenuItem_Click); // + // клиентыToolStripMenuItem + // + this.клиентыToolStripMenuItem.Name = "клиентыToolStripMenuItem"; + this.клиентыToolStripMenuItem.Size = new System.Drawing.Size(149, 22); + this.клиентыToolStripMenuItem.Text = "Клиенты"; + this.клиентыToolStripMenuItem.Click += new System.EventHandler(this.клиентыToolStripMenuItem_Click); + // + // исполнителиToolStripMenuItem + // + this.исполнителиToolStripMenuItem.Name = "исполнителиToolStripMenuItem"; + this.исполнителиToolStripMenuItem.Size = new System.Drawing.Size(149, 22); + this.исполнителиToolStripMenuItem.Text = "Исполнители"; + this.исполнителиToolStripMenuItem.Click += new System.EventHandler(this.исполнителиToolStripMenuItem_Click); + // // отчетыToolStripMenuItem // this.отчетыToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { @@ -113,6 +129,13 @@ this.списокЗаказовToolStripMenuItem.Text = "Список заказов"; this.списокЗаказовToolStripMenuItem.Click += new System.EventHandler(this.OrdersToolStripMenuItem_Click); // + // запускРаботToolStripMenuItem + // + this.запускРаботToolStripMenuItem.Name = "запускРаботToolStripMenuItem"; + this.запускРаботToolStripMenuItem.Size = new System.Drawing.Size(92, 20); + this.запускРаботToolStripMenuItem.Text = "Запуск работ"; + this.запускРаботToolStripMenuItem.Click += new System.EventHandler(this.запускРаботToolStripMenuItem_Click); + // // dataGridView // this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; @@ -132,29 +155,9 @@ this.ButtonCreateOrder.UseVisualStyleBackColor = true; this.ButtonCreateOrder.Click += new System.EventHandler(this.ButtonCreateOrder_Click); // - // ButtonTakeOrderInWork - // - this.ButtonTakeOrderInWork.Location = new System.Drawing.Point(786, 69); - this.ButtonTakeOrderInWork.Name = "ButtonTakeOrderInWork"; - this.ButtonTakeOrderInWork.Size = new System.Drawing.Size(192, 23); - this.ButtonTakeOrderInWork.TabIndex = 3; - this.ButtonTakeOrderInWork.Text = "Отдать заказ на выполнение"; - this.ButtonTakeOrderInWork.UseVisualStyleBackColor = true; - this.ButtonTakeOrderInWork.Click += new System.EventHandler(this.ButtonTakeOrderInWork_Click); - // - // ButtonOrderReady - // - this.ButtonOrderReady.Location = new System.Drawing.Point(786, 98); - this.ButtonOrderReady.Name = "ButtonOrderReady"; - this.ButtonOrderReady.Size = new System.Drawing.Size(192, 23); - this.ButtonOrderReady.TabIndex = 4; - this.ButtonOrderReady.Text = "Заказ готов"; - this.ButtonOrderReady.UseVisualStyleBackColor = true; - this.ButtonOrderReady.Click += new System.EventHandler(this.ButtonIssuedOrder_Click); - // // ButtonIssuedOrder // - this.ButtonIssuedOrder.Location = new System.Drawing.Point(786, 127); + this.ButtonIssuedOrder.Location = new System.Drawing.Point(786, 69); this.ButtonIssuedOrder.Name = "ButtonIssuedOrder"; this.ButtonIssuedOrder.Size = new System.Drawing.Size(192, 23); this.ButtonIssuedOrder.TabIndex = 5; @@ -172,13 +175,6 @@ this.ButtonRef.UseVisualStyleBackColor = true; this.ButtonRef.Click += new System.EventHandler(this.ButtonRef_Click); // - // клиентыToolStripMenuItem - // - this.клиентыToolStripMenuItem.Name = "клиентыToolStripMenuItem"; - this.клиентыToolStripMenuItem.Size = new System.Drawing.Size(180, 22); - this.клиентыToolStripMenuItem.Text = "Клиенты"; - this.клиентыToolStripMenuItem.Click += new System.EventHandler(this.клиентыToolStripMenuItem_Click); - // // FormMain // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); @@ -186,8 +182,6 @@ this.ClientSize = new System.Drawing.Size(1006, 323); this.Controls.Add(this.ButtonRef); this.Controls.Add(this.ButtonIssuedOrder); - this.Controls.Add(this.ButtonOrderReady); - this.Controls.Add(this.ButtonTakeOrderInWork); this.Controls.Add(this.ButtonCreateOrder); this.Controls.Add(this.dataGridView); this.Controls.Add(this.menuStrip1); @@ -211,8 +205,6 @@ private ToolStripMenuItem компонентыToolStripMenuItem; private DataGridView dataGridView; private Button ButtonCreateOrder; - private Button ButtonTakeOrderInWork; - private Button ButtonOrderReady; private Button ButtonIssuedOrder; private Button ButtonRef; private ToolStripMenuItem отчетыToolStripMenuItem; @@ -220,5 +212,7 @@ private ToolStripMenuItem компонентыПоИзделиямToolStripMenuItem; private ToolStripMenuItem списокЗаказовToolStripMenuItem; private ToolStripMenuItem клиентыToolStripMenuItem; + private ToolStripMenuItem исполнителиToolStripMenuItem; + private ToolStripMenuItem запускРаботToolStripMenuItem; } } \ No newline at end of file diff --git a/ComputersShop/FormMain.cs b/ComputersShop/FormMain.cs index ff3ee2d..ae8e3f4 100644 --- a/ComputersShop/FormMain.cs +++ b/ComputersShop/FormMain.cs @@ -19,12 +19,15 @@ namespace ComputersShop private readonly ILogger _logger; private readonly IOrderLogic _orderLogic; private readonly IReportLogic _reportLogic; - public FormMain(ILogger logger, IOrderLogic orderLogic, IReportLogic reportLogic) + private readonly IWorkProcess _workProcess; + public FormMain(ILogger logger, IOrderLogic orderLogic, IReportLogic reportLogic, IWorkProcess workProcess) { InitializeComponent(); _logger = logger; _orderLogic = orderLogic; _reportLogic = reportLogic; + _workProcess = workProcess; + } private void FormMain_Load(object sender, EventArgs e) @@ -42,6 +45,7 @@ namespace ComputersShop dataGridView.DataSource = list; dataGridView.Columns["ComputerId"].Visible = false; dataGridView.Columns["ClientId"].Visible = false; + dataGridView.Columns["ImplementerId"].Visible = false; } _logger.LogInformation("Загрузка заказов"); } @@ -68,6 +72,22 @@ namespace ComputersShop form.ShowDialog(); } } + private void клиентыToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormClients)); + if (service is FormClients form) + { + form.ShowDialog(); + } + } + private void исполнителиToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormImplementers)); + if (service is FormImplementers form) + { + form.ShowDialog(); + } + } private void ButtonCreateOrder_Click(object sender, EventArgs e) { @@ -79,37 +99,7 @@ namespace ComputersShop LoadData(); } } - private void ButtonTakeOrderInWork_Click(object sender, EventArgs e) - { - if (dataGridView.SelectedRows.Count == 1) - { - int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); - _logger.LogInformation("Заказ No{id}. Меняется статус на 'В работе'", id); - try - { - var operationResult = _orderLogic.TakeOrderInWork(new OrderBindingModel{ - Id = id, - ComputerId = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["ComputerId"].Value), - ComputerName = dataGridView.SelectedRows[0].Cells["ComputerName"].Value.ToString(), - Status = Enum.Parse(dataGridView.SelectedRows[0].Cells["Status"].Value.ToString()), - Count = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Count"].Value), - Sum = double.Parse(dataGridView.SelectedRows[0].Cells["Sum"].Value.ToString()), - DateCreate = DateTime.Parse(dataGridView.SelectedRows[0].Cells["DateCreate"].Value.ToString()) - }); - if (!operationResult) - { - throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); - } - LoadData(); - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка передачи заказа в работу"); - MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, - MessageBoxIcon.Error); - } - } - } + private void ButtonOrderReady_Click(object sender, EventArgs e) { if (dataGridView.SelectedRows.Count == 1) @@ -141,39 +131,6 @@ namespace ComputersShop } } } - private void ButtonIssuedOrder_Click(object sender, EventArgs e) - { - if (dataGridView.SelectedRows.Count == 1) - { - int id = - Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); - _logger.LogInformation("Заказ No{id}. Меняется статус на 'Выдан'", id); - try - { - var operationResult = _orderLogic.DeliveryOrder(new OrderBindingModel { - Id = id, - ComputerId = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["ComputerId"].Value), - ComputerName = dataGridView.SelectedRows[0].Cells["ComputerName"].Value.ToString(), - Status = Enum.Parse(dataGridView.SelectedRows[0].Cells["Status"].Value.ToString()), - Count = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Count"].Value), - Sum = double.Parse(dataGridView.SelectedRows[0].Cells["Sum"].Value.ToString()), - DateCreate = DateTime.Parse(dataGridView.SelectedRows[0].Cells["DateCreate"].Value.ToString()) - }); - if (!operationResult) - { - throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); - } - _logger.LogInformation("Заказ No{id} выдан", id); - LoadData(); - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка отметки о выдачи заказа"); - MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, - MessageBoxIcon.Error); - } - } - } private void ButtonRef_Click(object sender, EventArgs e) { LoadData(); @@ -206,14 +163,11 @@ namespace ComputersShop form.ShowDialog(); } } - - private void клиентыToolStripMenuItem_Click(object sender, EventArgs e) + private void запускРаботToolStripMenuItem_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormClients)); - if (service is FormClients form) - { - form.ShowDialog(); - } + _workProcess.DoWork((Program.ServiceProvider?.GetService(typeof(IImplementerLogic)) as IImplementerLogic)!, _orderLogic); + MessageBox.Show("Процесс обработки запущен", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); + } } } diff --git a/ComputersShop/Program.cs b/ComputersShop/Program.cs index ccc0d28..081b351 100644 --- a/ComputersShop/Program.cs +++ b/ComputersShop/Program.cs @@ -39,12 +39,15 @@ namespace ComputersShop services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); @@ -60,6 +63,8 @@ namespace ComputersShop services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file -- 2.25.1 From 37a2999102e1e5c3ec916811a7515b90fcb65d46 Mon Sep 17 00:00:00 2001 From: the Date: Thu, 20 Apr 2023 21:01:22 +0400 Subject: [PATCH 2/3] Fix, update file implement --- .../BusinessLogics/OrderLogic.cs | 43 ++++++--- .../DataFileSingleton.cs | 8 ++ .../Implements/ImplementerStorage.cs | 83 ++++++++++++++++++ .../Implements/OrderStorage.cs | 31 +++++-- .../Models/Implementer.cs | 87 +++++++++++++++++++ ComputerShopFileImplement/Models/Order.cs | 9 ++ 6 files changed, 245 insertions(+), 16 deletions(-) create mode 100644 ComputerShopFileImplement/Implements/ImplementerStorage.cs create mode 100644 ComputerShopFileImplement/Models/Implementer.cs diff --git a/ComputerShopBusinessLogic/BusinessLogics/OrderLogic.cs b/ComputerShopBusinessLogic/BusinessLogics/OrderLogic.cs index 3c1adc2..d8696a0 100644 --- a/ComputerShopBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/ComputerShopBusinessLogic/BusinessLogics/OrderLogic.cs @@ -44,25 +44,48 @@ namespace ComputerShopBusinessLogic.BusinessLogics public bool StatusUpdate(OrderBindingModel model, OrderStatus newStatus) { - CheckModel(model); - if (model.Status + 1 != newStatus) + var viewModel = _orderStorage.GetElement(new OrderSearchModel + { + Id = model.Id + }); + + if (viewModel == null) + { + _logger.LogWarning("Order model not found"); + return false; + } + + OrderBindingModel model1 = new OrderBindingModel + { + Id = viewModel.Id, + ComputerId = viewModel.ComputerId, + Status = viewModel.Status, + DateCreate = viewModel.DateCreate, + DateImplement = viewModel.DateImplement, + Count = viewModel.Count, + Sum = viewModel.Sum + }; + if (model.ImplementerId.HasValue) + { + model1.ImplementerId = model.ImplementerId; + } + + CheckModel(model1, false); + if (model1.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; - if (_orderStorage.Update(model) == null) + model1.Status = newStatus; + if (model1.Status == OrderStatus.Выдан) model1.DateImplement = DateTime.Now; + if (_orderStorage.Update(model1) == null) { - model.Status--; + model1.Status--; _logger.LogWarning("Update operation failed"); return false; } - if (model.ImplementerId.HasValue) - { - model.ImplementerId = model.ImplementerId; - } return true; + } public bool TakeOrderInWork(OrderBindingModel model) diff --git a/ComputerShopFileImplement/DataFileSingleton.cs b/ComputerShopFileImplement/DataFileSingleton.cs index aafad08..110e6cc 100644 --- a/ComputerShopFileImplement/DataFileSingleton.cs +++ b/ComputerShopFileImplement/DataFileSingleton.cs @@ -14,9 +14,13 @@ namespace ComputerShopFileImplement private readonly string ComponentFileName = "Component.xml"; private readonly string OrderFileName = "Order.xml"; private readonly string ComputerFileName = "Computer.xml"; + private readonly string ClientFileName = "Client.xml"; + private readonly string ImplementerFileName = "Implementer.xml"; public List Components { get; private set; } public List Orders { get; private set; } public List Computers { get; private set; } + public List Clients { get; private set; } + public List Implementers { get; private set; } public static DataFileSingleton GetInstance() { if (instance == null) @@ -30,11 +34,15 @@ namespace ComputerShopFileImplement public void SaveComputers() => SaveData(Computers, ComputerFileName, "Computers", x => x.GetXElement); public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + public void SaveClients() => SaveData(Clients, ClientFileName, "Clients", x => x.GetXElement); + public void SaveImplementers() => SaveData(Orders, ImplementerFileName, "Implementers", x => x.GetXElement); private DataFileSingleton() { Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; Computers = LoadData(ComputerFileName, "Computer", x => Computer.Create(x)!)!; Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + Clients = LoadData(ClientFileName, "Client", x => Client.Create(x)!)!; + Implementers = LoadData(ImplementerFileName, "Impleneter", x => Implementer.Create(x)!)!; } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) diff --git a/ComputerShopFileImplement/Implements/ImplementerStorage.cs b/ComputerShopFileImplement/Implements/ImplementerStorage.cs new file mode 100644 index 0000000..06df787 --- /dev/null +++ b/ComputerShopFileImplement/Implements/ImplementerStorage.cs @@ -0,0 +1,83 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.StoragesContracts; +using ComputerShopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopFileImplement.Implements +{ + public class ImplementerStorage : IImplementerStorage + { + private readonly DataFileSingleton _source; + public ImplementerStorage() + { + _source = DataFileSingleton.GetInstance(); + } + public ImplementerViewModel? GetElement(ImplementerSearchModel model) + { + if (model.Id.HasValue) return _source.Implementers.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + if (model.ImplementerFIO != null && model.Password != null) return _source.Implementers.FirstOrDefault(x => x.ImplementerFIO.Equals(model.ImplementerFIO) && x.Password.Equals(model.Password))?.GetViewModel; + if (model.ImplementerFIO != null) return _source.Implementers.FirstOrDefault(x => x.ImplementerFIO.Equals(model.ImplementerFIO))?.GetViewModel; + return null; + } + + public List GetFilteredList(ImplementerSearchModel model) + { + if (model == null) + { + return new(); + } + if (model.ImplementerFIO != null) + { + return _source.Implementers + .Where(x => x.ImplementerFIO.Contains(model.ImplementerFIO)) + .Select(x => x.GetViewModel) + .ToList(); + } + return new(); + } + + public List GetFullList() + { + return _source.Implementers.Select(x => x.GetViewModel).ToList(); + } + + public ImplementerViewModel? Insert(ImplementerBindingModel model) + { + model.Id = _source.Implementers.Count > 0 ? _source.Implementers.Max(x => x.Id) + 1 : 1; + var res = Implementer.Create(model); + if (res != null) + { + _source.Implementers.Add(res); + _source.SaveImplementers(); + } + return res?.GetViewModel; + } + + public ImplementerViewModel? Update(ImplementerBindingModel model) + { + var res = _source.Implementers.FirstOrDefault(x => x.Id == model.Id); + if (res != null) + { + res.Update(model); + _source.SaveImplementers(); + } + return res?.GetViewModel; + } + + public ImplementerViewModel? Delete(ImplementerBindingModel model) + { + var res = _source.Implementers.FirstOrDefault(x => x.Id == model.Id); + if (res != null) + { + _source.Implementers.Remove(res); + _source.SaveImplementers(); + } + return res?.GetViewModel; + } + } +} diff --git a/ComputerShopFileImplement/Implements/OrderStorage.cs b/ComputerShopFileImplement/Implements/OrderStorage.cs index 65bc162..56e358b 100644 --- a/ComputerShopFileImplement/Implements/OrderStorage.cs +++ b/ComputerShopFileImplement/Implements/OrderStorage.cs @@ -23,24 +23,43 @@ namespace ComputerShopFileImplement.Implements public OrderViewModel? GetElement(OrderSearchModel model) { + if (model.ImplementerId.HasValue && model.Status != null) return source.Orders.FirstOrDefault(x => x.ImplementerId == model.ImplementerId && model.Status.Equals(x.Status))?.GetViewModel; + if (model.ImplementerId.HasValue) + { + return source.Orders.FirstOrDefault(x => x.ImplementerId == model.ImplementerId)?.GetViewModel; + } if (!model.Id.HasValue) { return null; } - return source.Orders - .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + return source.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id)).GetViewModel; } public List GetFilteredList(OrderSearchModel model) { - if (!model.Id.HasValue) + if (!model.Id.HasValue && !model.DateFrom.HasValue && !model.DateTo.HasValue && !model.ClientId.HasValue && model.Status == null) { return new(); } + if (model.ClientId.HasValue) + { + return source.Orders + .Where(x => x.ClientId == model.ClientId) + .Select(x => x.GetViewModel) + .ToList(); + } + if (model.Status != null) + { + return source.Orders + .Where(x => model.Status.Equals(x.Status)) + .Select(x => x.GetViewModel) + .ToList(); + } return source.Orders - .Where(x => x.Id == model.Id || model.DateFrom <= x.DateCreate && x.DateCreate <= model.DateTo) - .Select(x => x.GetViewModel) - .ToList(); + .Where(x => x.Id == model.Id || model.DateFrom <= x.DateCreate && x.DateCreate <= model.DateTo) + .Select(x => x.GetViewModel) + .ToList(); + } public List GetFullList() diff --git a/ComputerShopFileImplement/Models/Implementer.cs b/ComputerShopFileImplement/Models/Implementer.cs new file mode 100644 index 0000000..d8ac2e0 --- /dev/null +++ b/ComputerShopFileImplement/Models/Implementer.cs @@ -0,0 +1,87 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.ViewModels; +using ComputerShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace ComputerShopFileImplement.Models +{ + public class Implementer : IImplementerModel + { + public int Id { get; private set; } + + public string ImplementerFIO { get; private set; } = string.Empty; + + public string Password { get; private set; } = string.Empty; + + public int WorkExperience { get; private set; } + + public int Qualification { get; private set; } + + public static Implementer? Create(XElement element) + { + if (element == null) + { + return null; + } + return new() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ImplementerFIO = element.Element("FIO")!.Value, + Password = element.Element("Password")!.Value, + Qualification = Convert.ToInt32(element.Element("Qualification")!.Value), + WorkExperience = Convert.ToInt32(element.Element("WorkExperience")!.Value), + }; + } + + public static Implementer? Create(ImplementerBindingModel model) + { + if (model == null) + { + return null; + } + return new() + { + Id = model.Id, + ImplementerFIO = model.ImplementerFIO, + Password = model.Password, + Qualification = model.Qualification, + WorkExperience = model.WorkExperience, + }; + } + + + + public void Update(ImplementerBindingModel model) + { + if (model == null) + { + return; + } + ImplementerFIO = model.ImplementerFIO; + Password = model.Password; + Qualification = model.Qualification; + WorkExperience = model.WorkExperience; + } + + public ImplementerViewModel GetViewModel => new() + { + Id = Id, + ImplementerFIO = ImplementerFIO, + Password = Password, + Qualification = Qualification, + }; + + public XElement GetXElement => new("Client", + new XAttribute("Id", Id), + new XElement("FIO", ImplementerFIO), + new XElement("Password", Password), + new XElement("Qualification", Qualification), + new XElement("WorkExperience", WorkExperience) + ); + } +} diff --git a/ComputerShopFileImplement/Models/Order.cs b/ComputerShopFileImplement/Models/Order.cs index b8e5412..178f919 100644 --- a/ComputerShopFileImplement/Models/Order.cs +++ b/ComputerShopFileImplement/Models/Order.cs @@ -41,6 +41,8 @@ namespace ComputerShopFileImplement.Models { ComputerId = model.ComputerId, ComputerName = model.ComputerName, + ImplementerId = model.ImplementerId, + ClientId = model.ClientId, Count = model.Count, Sum = model.Sum, Status = model.Status, @@ -61,6 +63,8 @@ namespace ComputerShopFileImplement.Models Id = Convert.ToInt32(element.Attribute("Id")!.Value), ComputerName = element.Element("ComputerName")!.Value, ComputerId = Convert.ToInt32(element.Element("ComputerId")!.Value), + ClientId = Convert.ToInt32(element.Element("ClientId")!.Value), + ImplementerId = Convert.ToInt32(element.Element("ImplementerId")!.Value), Sum = Convert.ToDouble(element.Element("Sum")!.Value), Count = Convert.ToInt32(element.Element("Count")!.Value), Status = (OrderStatus)Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value), @@ -83,6 +87,7 @@ namespace ComputerShopFileImplement.Models DateCreate = model.DateCreate; DateImplement = model.DateImplement; Id = model.Id; + ImplementerId = model.ImplementerId; } public OrderViewModel GetViewModel => new() @@ -95,6 +100,8 @@ namespace ComputerShopFileImplement.Models DateImplement = DateImplement, Id = Id, Status = Status, + ImplementerId = ImplementerId, + ImplementerFIO = DataFileSingleton.GetInstance().Implementers.FirstOrDefault(x => x.Id == ImplementerId)?.ImplementerFIO ?? string.Empty, }; public XElement GetXElement => new( @@ -103,6 +110,8 @@ namespace ComputerShopFileImplement.Models new XElement("ComputerName", ComputerName), new XElement("ComputerId", ComputerId.ToString()), new XElement("Count", Count.ToString()), + new XElement("ClientId", ClientId), + new XElement("ImplementerId", ImplementerId), new XElement("Sum", Sum.ToString()), new XElement("Status", Status.ToString()), new XElement("DateCreate", DateCreate.ToString()), -- 2.25.1 From 81b95867cc2e07bc7038c856c2c6f8e9b2f722d8 Mon Sep 17 00:00:00 2001 From: the Date: Thu, 20 Apr 2023 21:09:16 +0400 Subject: [PATCH 3/3] List implement --- .../Implements/ImplementerStorage.cs | 1 + ComputerShopFileImplement/Models/Client.cs | 80 +++++++++++++ .../Implements/ImplementerStorage.cs | 111 ++++++++++++++++++ .../Implements/OrderStorage.cs | 28 +++++ ComputerShopListImplement/Models/Client.cs | 57 +++++++++ .../Models/DataListSingleton.cs | 4 + .../Models/Implementer.cs | 60 ++++++++++ ComputerShopListImplement/Models/Order.cs | 5 + 8 files changed, 346 insertions(+) create mode 100644 ComputerShopFileImplement/Models/Client.cs create mode 100644 ComputerShopListImplement/Implements/ImplementerStorage.cs create mode 100644 ComputerShopListImplement/Models/Client.cs create mode 100644 ComputerShopListImplement/Models/Implementer.cs diff --git a/ComputerShopFileImplement/Implements/ImplementerStorage.cs b/ComputerShopFileImplement/Implements/ImplementerStorage.cs index 06df787..e5e116e 100644 --- a/ComputerShopFileImplement/Implements/ImplementerStorage.cs +++ b/ComputerShopFileImplement/Implements/ImplementerStorage.cs @@ -2,6 +2,7 @@ using ComputerShopContracts.SearchModels; using ComputerShopContracts.StoragesContracts; using ComputerShopContracts.ViewModels; +using ComputerShopFileImplement.Models; using System; using System.Collections.Generic; using System.Linq; diff --git a/ComputerShopFileImplement/Models/Client.cs b/ComputerShopFileImplement/Models/Client.cs new file mode 100644 index 0000000..9d6158c --- /dev/null +++ b/ComputerShopFileImplement/Models/Client.cs @@ -0,0 +1,80 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.ViewModels; +using ComputerShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace ComputerShopFileImplement.Models +{ + public class Client : IClientModel + { + public int Id { get; private set; } + + public string ClientFIO { get; private set; } = string.Empty; + + public string Email { get; private set; } = string.Empty; + + public string Password { get; private set; } = string.Empty; + + public static Client? Create(ClientBindingModel model) + { + if (model == null) + { + return null; + } + return new() + { + Id = model.Id, + ClientFIO = model.ClientFIO, + Email = model.Email, + Password = model.Password + }; + } + public static Client? Create(XElement element) + { + if (element == null) + { + return null; + } + return new() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ClientFIO = element.Element("FIO")!.Value, + Email = element.Element("Email")!.Value, + Password = element.Element("Password")!.Value, + }; + } + + + public void Update(ClientBindingModel model) + { + if (model == null) + { + return; + } + ClientFIO = model.ClientFIO; + Email = model.Email; + Password = model.Password; + } + + public ClientViewModel GetViewModel => new() + { + Id = Id, + ClientFIO = ClientFIO, + Email = Email, + Password = Password, + }; + + public XElement GetXElement => new("Client", + new XAttribute("Id", Id), + new XElement("FIO", ClientFIO), + new XElement("Email", Email), + new XElement("Password", Password) + ); + + } +} diff --git a/ComputerShopListImplement/Implements/ImplementerStorage.cs b/ComputerShopListImplement/Implements/ImplementerStorage.cs new file mode 100644 index 0000000..8f4175b --- /dev/null +++ b/ComputerShopListImplement/Implements/ImplementerStorage.cs @@ -0,0 +1,111 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.StoragesContracts; +using ComputerShopContracts.ViewModels; +using ComputerShopListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopListImplement.Implements +{ + public class ImplementerStorage : IImplementerStorage + { + private readonly DataListSingleton _source; + public ImplementerStorage() + { + _source = DataListSingleton.GetInstance(); + } + public ImplementerViewModel? GetElement(ImplementerSearchModel model) + { + foreach (var x in _source.Implementers) + { + if (model.Id.HasValue && x.Id == model.Id) + return x.GetViewModel; + if (model.ImplementerFIO != null && model.Password != null && + x.ImplementerFIO.Equals(model.ImplementerFIO) && x.Password.Equals(model.Password)) + return x.GetViewModel; + if (model.ImplementerFIO != null && x.ImplementerFIO.Equals(model.ImplementerFIO)) + return x.GetViewModel; + } + return null; + } + + public List GetFilteredList(ImplementerSearchModel model) + { + if (model == null) + { + return new(); + } + List list = new(); + if (model.ImplementerFIO != null) + { + foreach (var implementer in _source.Implementers) + { + if (implementer.ImplementerFIO.Contains(model.ImplementerFIO)) + { + list.Add(implementer.GetViewModel); + } + } + } + return list; + } + + public List GetFullList() + { + var result = new List(); + foreach (var implementer in _source.Implementers) + { + result.Add(implementer.GetViewModel); + } + return result; + } + + public ImplementerViewModel? Insert(ImplementerBindingModel model) + { + model.Id = 1; + foreach (var implementer in _source.Implementers) + { + if (model.Id <= implementer.Id) + { + model.Id = implementer.Id + 1; + } + } + var res = Implementer.Create(model); + if (res != null) + { + _source.Implementers.Add(res); + } + return res?.GetViewModel; + } + + public ImplementerViewModel? Update(ImplementerBindingModel model) + { + foreach (var implementer in _source.Implementers) + { + if (implementer.Id == model.Id) + { + implementer.Update(model); + return implementer.GetViewModel; + } + } + return null; + } + + public ImplementerViewModel? Delete(ImplementerBindingModel model) + { + for (int i = 0; i < _source.Implementers.Count; ++i) + { + if (_source.Implementers[i].Id == model.Id) + { + var element = _source.Implementers[i]; + _source.Implementers.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } + } +} diff --git a/ComputerShopListImplement/Implements/OrderStorage.cs b/ComputerShopListImplement/Implements/OrderStorage.cs index c61cd24..50f599f 100644 --- a/ComputerShopListImplement/Implements/OrderStorage.cs +++ b/ComputerShopListImplement/Implements/OrderStorage.cs @@ -32,6 +32,14 @@ namespace ComputerShopListImplement.Implements { return order.GetViewModel; } + else if (model.ImplementerId.HasValue && model.Status != null && order.ImplementerId == model.ImplementerId && model.Status.Equals(order.Status)) + { + return order.GetViewModel; + } + else if (model.ImplementerId.HasValue && model.ImplementerId == order.ImplementerId) + { + return order.GetViewModel; + } } return null; } @@ -50,6 +58,26 @@ namespace ComputerShopListImplement.Implements result.Add(order.GetViewModel); } } + if (model.ImplementerId.HasValue) + { + foreach (var order in _source.Orders) + { + if (order.ImplementerId == model.ImplementerId) + { + result.Add(order.GetViewModel); + } + } + } + if (model.Status != null) + { + foreach (var order in _source.Orders) + { + if (model.Status.Equals(order.Status)) + { + result.Add(order.GetViewModel); + } + } + } return result; } diff --git a/ComputerShopListImplement/Models/Client.cs b/ComputerShopListImplement/Models/Client.cs new file mode 100644 index 0000000..d8a6ca7 --- /dev/null +++ b/ComputerShopListImplement/Models/Client.cs @@ -0,0 +1,57 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.ViewModels; +using ComputerShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopListImplement.Models +{ + public class Client : IClientModel + { + public int Id { get; private set; } + + public string ClientFIO { get; private set; } = string.Empty; + + public string Email { get; private set; } = string.Empty; + + public string Password { get; private set; } = string.Empty; + + public static Client? Create(ClientBindingModel model) + { + if (model == null) + { + return null; + } + return new() + { + Id = model.Id, + ClientFIO = model.ClientFIO, + Email = model.Email, + Password = model.Password + }; + } + + public void Update(ClientBindingModel model) + { + if (model == null) + { + return; + } + ClientFIO = model.ClientFIO; + Email = model.Email; + Password = model.Password; + } + + public ClientViewModel GetViewModel => new() + { + Id = Id, + ClientFIO = ClientFIO, + Email = Email, + Password = Password, + }; + + } +} diff --git a/ComputerShopListImplement/Models/DataListSingleton.cs b/ComputerShopListImplement/Models/DataListSingleton.cs index e71b814..28f714e 100644 --- a/ComputerShopListImplement/Models/DataListSingleton.cs +++ b/ComputerShopListImplement/Models/DataListSingleton.cs @@ -12,11 +12,15 @@ namespace ComputerShopListImplement.Models public List Components { get; set; } public List Orders { get; set; } public List Computers { get; set; } + public List Implementers { get; set; } + public List Clients { get; set; } private DataListSingleton() { Components = new List(); Orders = new List(); Computers = new List(); + Clients = new List(); + Implementers = new List(); } public static DataListSingleton GetInstance() { diff --git a/ComputerShopListImplement/Models/Implementer.cs b/ComputerShopListImplement/Models/Implementer.cs new file mode 100644 index 0000000..35045e8 --- /dev/null +++ b/ComputerShopListImplement/Models/Implementer.cs @@ -0,0 +1,60 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.ViewModels; +using ComputerShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopListImplement.Models +{ + public class Implementer : IImplementerModel + { + public int Id { get; private set; } + + public string ImplementerFIO { get; private set; } = string.Empty; + + public string Password { get; private set; } = string.Empty; + + public int WorkExperience { get; private set; } + + public int Qualification { get; private set; } + + public static Implementer? Create(ImplementerBindingModel model) + { + if (model == null) + { + return null; + } + return new() + { + Id = model.Id, + Password = model.Password, + Qualification = model.Qualification, + ImplementerFIO = model.ImplementerFIO, + WorkExperience = model.WorkExperience, + }; + } + + public void Update(ImplementerBindingModel model) + { + if (model == null) + { + return; + } + Password = model.Password; + Qualification = model.Qualification; + ImplementerFIO = model.ImplementerFIO; + WorkExperience = model.WorkExperience; + } + + public ImplementerViewModel GetViewModel => new() + { + Id = Id, + Password = Password, + Qualification = Qualification, + ImplementerFIO = ImplementerFIO, + }; + } +} diff --git a/ComputerShopListImplement/Models/Order.cs b/ComputerShopListImplement/Models/Order.cs index ecc0952..fa716ff 100644 --- a/ComputerShopListImplement/Models/Order.cs +++ b/ComputerShopListImplement/Models/Order.cs @@ -40,6 +40,8 @@ namespace ComputerShopListImplement.Models { ComputerId = model.ComputerId, ComputerName = model.ComputerName, + ImplementerId = model.ImplementerId, + ClientId = model.ClientId, Count = model.Count, Sum = model.Sum, Status = model.Status, @@ -63,6 +65,7 @@ namespace ComputerShopListImplement.Models DateCreate = model.DateCreate; DateImplement = model.DateImplement; Id = model.Id; + ImplementerId = model.ImplementerId; } public OrderViewModel GetViewModel => new() @@ -75,6 +78,8 @@ namespace ComputerShopListImplement.Models DateImplement = DateImplement, Id = Id, Status = Status, + ImplementerId = ImplementerId, + ImplementerFIO = DataListSingleton.GetInstance().Implementers.FirstOrDefault(x => x.Id == ImplementerId)?.ImplementerFIO ?? string.Empty, }; } } -- 2.25.1