diff --git a/LawFirm/LawFirmBusinessLogic/BusinessLogics/ImplementerLogic.cs b/LawFirm/LawFirmBusinessLogic/BusinessLogics/ImplementerLogic.cs new file mode 100644 index 0000000..a3285f8 --- /dev/null +++ b/LawFirm/LawFirmBusinessLogic/BusinessLogics/ImplementerLogic.cs @@ -0,0 +1,123 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.BusinessLogicsContracts; +using LawFirmContracts.SearchModels; +using LawFirmContracts.StoragesContracts; +using LawFirmContracts.ViewModels; +using LawFirmDataModel.Models; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmBusinessLogic.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 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; + } + public bool Update(ImplementerBindingModel model) + { + CheckModel(model); + if (_implementerStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + 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("Исполнитель с таким фио уже есть"); + } + } + } +} \ No newline at end of file diff --git a/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs b/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs index 29cabc1..fd884b1 100644 --- a/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs @@ -5,6 +5,8 @@ using LawFirmContracts.StoragesContracts; using LawFirmContracts.ViewModels; using LawFirmDataModels.Enums; using Microsoft.Extensions.Logging; +using DocumentFormat.OpenXml.EMMA; +using LawFirmContracts.BindingModels; namespace LawFirmBusinessLogic.BusinessLogics { @@ -47,12 +49,18 @@ namespace LawFirmBusinessLogic.BusinessLogics return false; } model.Status = newStatus; + model.DateCreate = viewModel.DateCreate; if (model.Status == OrderStatus.Готов) model.DateImplement = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); else { model.DateImplement = viewModel.DateImplement; } - CheckModel(model, false); + if (viewModel.ImplementerId.HasValue) + model.ImplementerId = viewModel.ImplementerId; + model.DocumentId = viewModel.DocumentId; + model.Sum = viewModel.Sum; + model.Count = viewModel.Count; + CheckModel(model); if (_orderStorage.Update(model) == null) { model.Status--; @@ -86,6 +94,22 @@ namespace LawFirmBusinessLogic.BusinessLogics _logger.LogInformation("ReadList. Count:{Count}", list.Count); 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/LawFirm/LawFirmBusinessLogic/BusinessLogics/WorkModeling.cs b/LawFirm/LawFirmBusinessLogic/BusinessLogics/WorkModeling.cs new file mode 100644 index 0000000..8c38a03 --- /dev/null +++ b/LawFirm/LawFirmBusinessLogic/BusinessLogics/WorkModeling.cs @@ -0,0 +1,146 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.BusinessLogicsContracts; +using LawFirmContracts.SearchModels; +using LawFirmContracts.ViewModels; +using LawFirmDataModels.Enums; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmBusinessLogic.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 { OrderStatus = 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); + await Task.Run(() => + { + foreach (var order in orders) + { + try + { + _logger.LogDebug("DoWork. Worker {Id} try get order {Order}", implementer.Id, order.Id); + // пытаемся назначить заказ на исполнителя + _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.FinishOrder(new OrderBindingModel + { + Id = order.Id + }); + } + // кто-то мог уже перехватить заказ, игнорируем ошибку + catch (InvalidOperationException ex) + { + _logger.LogWarning(ex, "Error try get work"); + } + // заканчиваем выполнение имитации в случае иной ошибки + catch (Exception ex) + { + _logger.LogError(ex, "Error while do work"); + throw; + } + // отдыхаем + Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100)); + } + }); + } + /// + /// Ищем заказ, которые уже в работе (вдруг исполнителя прервали) + /// + /// + /// + private async Task RunOrderInWork(ImplementerViewModel implementer) + { + if (_orderLogic == null || implementer == null) + { + return; + } + try + { + var runOrder = await Task.Run(() => _orderLogic.ReadElement(new OrderSearchModel + { + ImplementerId = implementer.Id, + OrderStatus = 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; + } + } + } +} \ No newline at end of file diff --git a/LawFirm/LawFirmContracts/BindingModels/ImplementerBindingModel.cs b/LawFirm/LawFirmContracts/BindingModels/ImplementerBindingModel.cs new file mode 100644 index 0000000..8e13ace --- /dev/null +++ b/LawFirm/LawFirmContracts/BindingModels/ImplementerBindingModel.cs @@ -0,0 +1,19 @@ +using LawFirmDataModel.Models; +using LawFirmDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmContracts.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; } + } +} \ No newline at end of file diff --git a/LawFirm/LawFirmContracts/BindingModels/OrderBindingModel.cs b/LawFirm/LawFirmContracts/BindingModels/OrderBindingModel.cs index f1d5fb4..9097117 100644 --- a/LawFirm/LawFirmContracts/BindingModels/OrderBindingModel.cs +++ b/LawFirm/LawFirmContracts/BindingModels/OrderBindingModel.cs @@ -8,7 +8,8 @@ namespace LawFirmContracts.BindingModels public int Id { get; set; } public int DocumentId { get; set; } public int ClientId { get; set; } - public int Count { get; set; } + public int? ImplementerId { get; set; } + public int Count { get; set; } public double Sum { get; set; } public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; public DateTime DateCreate { get; set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); diff --git a/LawFirm/LawFirmContracts/BusinessLogicsContracts/IImplementerLogic.cs b/LawFirm/LawFirmContracts/BusinessLogicsContracts/IImplementerLogic.cs new file mode 100644 index 0000000..16a4f7e --- /dev/null +++ b/LawFirm/LawFirmContracts/BusinessLogicsContracts/IImplementerLogic.cs @@ -0,0 +1,20 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.SearchModels; +using LawFirmContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmContracts.BusinessLogicsContracts +{ + public interface IImplementerLogic + { + List? ReadList(ImplementerSearchModel? model); + ImplementerViewModel? ReadElement(ImplementerSearchModel model); + bool Create(ImplementerBindingModel model); + bool Update(ImplementerBindingModel model); + bool Delete(ImplementerBindingModel model); + } +} \ No newline at end of file diff --git a/LawFirm/LawFirmContracts/BusinessLogicsContracts/IOrderLogic.cs b/LawFirm/LawFirmContracts/BusinessLogicsContracts/IOrderLogic.cs index 850fa8d..f126a23 100644 --- a/LawFirm/LawFirmContracts/BusinessLogicsContracts/IOrderLogic.cs +++ b/LawFirm/LawFirmContracts/BusinessLogicsContracts/IOrderLogic.cs @@ -7,6 +7,7 @@ namespace LawFirmContracts.BusinessLogicsContracts public interface IOrderLogic { List? ReadList(OrderSearchModel? model); + OrderViewModel? ReadElement(OrderSearchModel model); bool CreateOrder(OrderBindingModel model); bool TakeOrderInWork(OrderBindingModel model); bool FinishOrder(OrderBindingModel model); diff --git a/LawFirm/LawFirmContracts/BusinessLogicsContracts/IWorkProcess.cs b/LawFirm/LawFirmContracts/BusinessLogicsContracts/IWorkProcess.cs new file mode 100644 index 0000000..aa1b99f --- /dev/null +++ b/LawFirm/LawFirmContracts/BusinessLogicsContracts/IWorkProcess.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmContracts.BusinessLogicsContracts +{ + public interface IWorkProcess + { + /// + /// Запуск работ + /// + void DoWork(IImplementerLogic implementerLogic, IOrderLogic orderLogic); + } +} \ No newline at end of file diff --git a/LawFirm/LawFirmContracts/SearchModels/ImplementerSearchModel.cs b/LawFirm/LawFirmContracts/SearchModels/ImplementerSearchModel.cs new file mode 100644 index 0000000..c915a1e --- /dev/null +++ b/LawFirm/LawFirmContracts/SearchModels/ImplementerSearchModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmContracts.SearchModels +{ + public class ImplementerSearchModel + { + public int? Id { get; set; } + public string? ImplementerFIO { get; set; } + public string? Password { get; set; } + } +} \ No newline at end of file diff --git a/LawFirm/LawFirmContracts/SearchModels/OrderSearchModel.cs b/LawFirm/LawFirmContracts/SearchModels/OrderSearchModel.cs index 04de8e5..8b35d17 100644 --- a/LawFirm/LawFirmContracts/SearchModels/OrderSearchModel.cs +++ b/LawFirm/LawFirmContracts/SearchModels/OrderSearchModel.cs @@ -1,4 +1,6 @@ -namespace LawFirmContracts.SearchModels +using LawFirmDataModels.Enums; + +namespace LawFirmContracts.SearchModels { public class OrderSearchModel { @@ -6,5 +8,7 @@ public DateTime? DateFrom { get; set; } public DateTime? DateTo { get; set; } public int? ClientId { get; set; } - } + public int? ImplementerId { get; set; } + public OrderStatus? OrderStatus { get; set; } + } } \ No newline at end of file diff --git a/LawFirm/LawFirmContracts/StoragesContracts/IImplementerStorage.cs b/LawFirm/LawFirmContracts/StoragesContracts/IImplementerStorage.cs new file mode 100644 index 0000000..b0124bc --- /dev/null +++ b/LawFirm/LawFirmContracts/StoragesContracts/IImplementerStorage.cs @@ -0,0 +1,21 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.SearchModels; +using LawFirmContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmContracts.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); + } +} \ No newline at end of file diff --git a/LawFirm/LawFirmContracts/ViewModels/ImplementerViewModel.cs b/LawFirm/LawFirmContracts/ViewModels/ImplementerViewModel.cs new file mode 100644 index 0000000..64bd61e --- /dev/null +++ b/LawFirm/LawFirmContracts/ViewModels/ImplementerViewModel.cs @@ -0,0 +1,27 @@ +using LawFirmDataModel.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmContracts.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; } + } +} \ No newline at end of file diff --git a/LawFirm/LawFirmContracts/ViewModels/OrderViewModel.cs b/LawFirm/LawFirmContracts/ViewModels/OrderViewModel.cs index e5b076e..7d2b51d 100644 --- a/LawFirm/LawFirmContracts/ViewModels/OrderViewModel.cs +++ b/LawFirm/LawFirmContracts/ViewModels/OrderViewModel.cs @@ -9,11 +9,14 @@ namespace LawFirmContracts.ViewModels [DisplayName("Номер")] public int Id { get; set; } public int DocumentId { get; set; } + public int? ImplementerId { get; set; } [DisplayName("Документ")] public int ClientId { get; set; } [DisplayName("Данные клиента")] public string ClientFIO { get; set; } = string.Empty; - [DisplayName("Документ")] + [DisplayName("Фамилия исполнителя")] + public string ImplementerFIO { get; set; } = string.Empty; + [DisplayName("Документ")] public string DocumentName { get; set; } = string.Empty; [DisplayName("Количество")] public int Count { get; set; } diff --git a/LawFirm/LawFirmDataModels/Models/IImplementerModel.cs b/LawFirm/LawFirmDataModels/Models/IImplementerModel.cs new file mode 100644 index 0000000..a0d0914 --- /dev/null +++ b/LawFirm/LawFirmDataModels/Models/IImplementerModel.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmDataModel.Models +{ + public interface IImplementerModel + { + string ImplementerFIO { get; } + string Password { get; } + int WorkExperience { get; } + int Qualification { get; } + } +} \ No newline at end of file diff --git a/LawFirm/LawFirmDataModels/Models/IOrderModel.cs b/LawFirm/LawFirmDataModels/Models/IOrderModel.cs index 6dc919b..f7e1665 100644 --- a/LawFirm/LawFirmDataModels/Models/IOrderModel.cs +++ b/LawFirm/LawFirmDataModels/Models/IOrderModel.cs @@ -11,5 +11,6 @@ namespace LawFirmDataModels.Models OrderStatus Status { get; } DateTime DateCreate { get; } DateTime? DateImplement { get; } + int? ImplementerId { get; } } } diff --git a/LawFirm/LawFirmDatabaseImplement/Implements/ImplementerStorage.cs b/LawFirm/LawFirmDatabaseImplement/Implements/ImplementerStorage.cs new file mode 100644 index 0000000..09dc1f7 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Implements/ImplementerStorage.cs @@ -0,0 +1,90 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.SearchModels; +using LawFirmContracts.StoragesContracts; +using LawFirmContracts.ViewModels; +using LawFirmDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmDatabaseImplement.Implements +{ + public class ImplementerStorage : IImplementerStorage + { + public ImplementerViewModel? Delete(ImplementerBindingModel model) + { + using var context = new LawFirmDatabase(); + var res = context.Implementers.FirstOrDefault(x => x.Id == model.Id); + if (res != null) + { + context.Implementers.Remove(res); + context.SaveChanges(); + } + return res?.GetViewModel; + } + public ImplementerViewModel? GetElement(ImplementerSearchModel model) + { + using var context = new LawFirmDatabase(); + 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.Id.HasValue) + { + var res = GetElement(model); + return res != null ? new() { res } : new(); + } + if (model.ImplementerFIO != null) // На случай если фио не будет уникальным (по заданию оно уникально) + { + using var context = new LawFirmDatabase(); + return context.Implementers + .Where(x => x.ImplementerFIO.Equals(model.ImplementerFIO)) + .Select(x => x.GetViewModel) + .ToList(); + } + return new(); + } + public List GetFullList() + { + using var context = new LawFirmDatabase(); + return context.Implementers.Select(x => x.GetViewModel).ToList(); + } + public ImplementerViewModel? Insert(ImplementerBindingModel model) + { + using var context = new LawFirmDatabase(); + 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 LawFirmDatabase(); + var res = context.Implementers.FirstOrDefault(x => x.Id == model.Id); + if (res != null) + { + res.Update(model); + context.SaveChanges(); + } + return res?.GetViewModel; + } + } +} \ No newline at end of file diff --git a/LawFirm/LawFirmDatabaseImplement/Implements/OrderStorage.cs b/LawFirm/LawFirmDatabaseImplement/Implements/OrderStorage.cs index 5f34ceb..0711445 100644 --- a/LawFirm/LawFirmDatabaseImplement/Implements/OrderStorage.cs +++ b/LawFirm/LawFirmDatabaseImplement/Implements/OrderStorage.cs @@ -11,12 +11,26 @@ namespace LawFirmDatabaseImplement.Implements { public OrderViewModel? GetElement(OrderSearchModel model) { - if (!model.Id.HasValue) - { - return null; - } using var context = new LawFirmDatabase(); - return context.Orders.Include(x => x.Document).Include(x => x.Client).FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + if (model.Id.HasValue) + { + return context.Orders + .Include(x => x.Document) + .Include(x => x.Client) + .Include(x => x.Implementer) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + } + if (model.ImplementerId.HasValue && model.OrderStatus.HasValue) + { + return context.Orders + .Include(x => x.Document) + .Include(x => x.Client) + .Include(x => x.Implementer) + .FirstOrDefault(x => x.ImplementerId == model.ImplementerId && x.Status == model.OrderStatus) + ?.GetViewModel; + } + return null; } public List GetFilteredList(OrderSearchModel model) { @@ -48,7 +62,17 @@ namespace LawFirmDatabaseImplement.Implements .Select(x => x.GetViewModel) .ToList(); } - else + else if (model.OrderStatus.HasValue) + { + return context.Orders + .Include(x => x.Document) + .Include(x => x.Client) + .Include(x => x.Implementer) + .Where(x => x.Status == model.OrderStatus) + .Select(x => x.GetViewModel) + .ToList(); + } + else { return new(); } @@ -56,7 +80,7 @@ namespace LawFirmDatabaseImplement.Implements public List GetFullList() { using var context = new LawFirmDatabase(); - return context.Orders.Include(x => x.Document).Include(x => x.Client).Select(x => x.GetViewModel).ToList(); + return context.Orders.Include(x => x.Document).Include(x => x.Client).Include(x => x.Implementer).Select(x => x.GetViewModel).ToList(); } public OrderViewModel? Insert(OrderBindingModel model) { @@ -73,19 +97,19 @@ namespace LawFirmDatabaseImplement.Implements public OrderViewModel? Update(OrderBindingModel model) { using var context = new LawFirmDatabase(); - var order = context.Orders.Include(x => x.Client).FirstOrDefault(x => x.Id == model.Id); + var order = context.Orders.Include(x => x.Client).Include(x => x.Implementer).Include(x => x.Document).FirstOrDefault(x => x.Id == model.Id); if (order == null) { return null; } order.Update(model); context.SaveChanges(); - return context.Orders.Include(x => x.Document).Include(x => x.Client).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + return order.GetViewModel; } public OrderViewModel? Delete(OrderBindingModel model) { using var context = new LawFirmDatabase(); - var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + var element = context.Orders.Include(x => x.Client).Include(x => x.Implementer).Include(x => x.Document).FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { var deletedElement = context.Orders @@ -94,8 +118,8 @@ namespace LawFirmDatabaseImplement.Implements ?.GetViewModel; context.Orders.Remove(element); context.SaveChanges(); - return deletedElement; - } + return element.GetViewModel; + } return null; } } diff --git a/LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs b/LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs index 6ed8bc9..c80d97a 100644 --- a/LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs +++ b/LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs @@ -9,7 +9,7 @@ namespace LawFirmDatabaseImplement { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS; Initial Catalog=LawFirmDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS; Initial Catalog=LawFirm6;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); } @@ -18,5 +18,6 @@ namespace LawFirmDatabaseImplement public virtual DbSet DocumentBlanks { set; get; } public virtual DbSet Orders { set; get; } public virtual DbSet Clients { set; get; } - } + public virtual DbSet Implementers { set; get; } + } } \ No newline at end of file diff --git a/LawFirm/LawFirmDatabaseImplement/Migrations/20240517194322_InitialCreate.Designer.cs b/LawFirm/LawFirmDatabaseImplement/Migrations/20240524144938_r.Designer.cs similarity index 82% rename from LawFirm/LawFirmDatabaseImplement/Migrations/20240517194322_InitialCreate.Designer.cs rename to LawFirm/LawFirmDatabaseImplement/Migrations/20240524144938_r.Designer.cs index dc05c0a..ecaae31 100644 --- a/LawFirm/LawFirmDatabaseImplement/Migrations/20240517194322_InitialCreate.Designer.cs +++ b/LawFirm/LawFirmDatabaseImplement/Migrations/20240524144938_r.Designer.cs @@ -12,8 +12,8 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace LawFirmDatabaseImplement.Migrations { [DbContext(typeof(LawFirmDatabase))] - [Migration("20240517194322_InitialCreate")] - partial class InitialCreate + [Migration("20240524144938_r")] + partial class r { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -116,6 +116,33 @@ namespace LawFirmDatabaseImplement.Migrations b.ToTable("DocumentBlanks"); }); + modelBuilder.Entity("LawFirmDatabaseImplement.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("LawFirmDatabaseImplement.Models.Order", b => { b.Property("Id") @@ -139,6 +166,9 @@ namespace LawFirmDatabaseImplement.Migrations b.Property("DocumentId") .HasColumnType("int"); + b.Property("ImplementerId") + .HasColumnType("int"); + b.Property("Status") .HasColumnType("int"); @@ -151,6 +181,8 @@ namespace LawFirmDatabaseImplement.Migrations b.HasIndex("DocumentId"); + b.HasIndex("ImplementerId"); + b.ToTable("Orders"); }); @@ -187,9 +219,15 @@ namespace LawFirmDatabaseImplement.Migrations .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + b.HasOne("LawFirmDatabaseImplement.Models.Implementer", "Implementer") + .WithMany("Orders") + .HasForeignKey("ImplementerId"); + b.Navigation("Client"); b.Navigation("Document"); + + b.Navigation("Implementer"); }); modelBuilder.Entity("LawFirmDatabaseImplement.Models.Blank", b => @@ -208,6 +246,11 @@ namespace LawFirmDatabaseImplement.Migrations b.Navigation("Orders"); }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Implementer", b => + { + b.Navigation("Orders"); + }); #pragma warning restore 612, 618 } } diff --git a/LawFirm/LawFirmDatabaseImplement/Migrations/20240517194322_InitialCreate.cs b/LawFirm/LawFirmDatabaseImplement/Migrations/20240524144938_r.cs similarity index 81% rename from LawFirm/LawFirmDatabaseImplement/Migrations/20240517194322_InitialCreate.cs rename to LawFirm/LawFirmDatabaseImplement/Migrations/20240524144938_r.cs index e844e84..b87a7d2 100644 --- a/LawFirm/LawFirmDatabaseImplement/Migrations/20240517194322_InitialCreate.cs +++ b/LawFirm/LawFirmDatabaseImplement/Migrations/20240524144938_r.cs @@ -6,7 +6,7 @@ using Microsoft.EntityFrameworkCore.Migrations; namespace LawFirmDatabaseImplement.Migrations { /// - public partial class InitialCreate : Migration + public partial class r : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) @@ -54,6 +54,22 @@ namespace LawFirmDatabaseImplement.Migrations table.PrimaryKey("PK_Documents", x => x.Id); }); + 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.CreateTable( name: "DocumentBlanks", columns: table => new @@ -93,6 +109,7 @@ namespace LawFirmDatabaseImplement.Migrations Sum = table.Column(type: "float", nullable: false), Status = table.Column(type: "int", nullable: false), DateCreate = table.Column(type: "datetime2", nullable: false), + ImplementerId = table.Column(type: "int", nullable: true), DateImplement = table.Column(type: "datetime2", nullable: true) }, constraints: table => @@ -110,6 +127,11 @@ namespace LawFirmDatabaseImplement.Migrations principalTable: "Documents", principalColumn: "Id", onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_Orders_Implementers_ImplementerId", + column: x => x.ImplementerId, + principalTable: "Implementers", + principalColumn: "Id"); }); migrationBuilder.CreateIndex( @@ -131,6 +153,11 @@ namespace LawFirmDatabaseImplement.Migrations name: "IX_Orders_DocumentId", table: "Orders", column: "DocumentId"); + + migrationBuilder.CreateIndex( + name: "IX_Orders_ImplementerId", + table: "Orders", + column: "ImplementerId"); } /// @@ -150,6 +177,9 @@ namespace LawFirmDatabaseImplement.Migrations migrationBuilder.DropTable( name: "Documents"); + + migrationBuilder.DropTable( + name: "Implementers"); } } } diff --git a/LawFirm/LawFirmDatabaseImplement/Migrations/LawFirmDatabaseModelSnapshot.cs b/LawFirm/LawFirmDatabaseImplement/Migrations/LawFirmDatabaseModelSnapshot.cs index 9ae3d18..8ccde39 100644 --- a/LawFirm/LawFirmDatabaseImplement/Migrations/LawFirmDatabaseModelSnapshot.cs +++ b/LawFirm/LawFirmDatabaseImplement/Migrations/LawFirmDatabaseModelSnapshot.cs @@ -113,6 +113,33 @@ namespace LawFirmDatabaseImplement.Migrations b.ToTable("DocumentBlanks"); }); + modelBuilder.Entity("LawFirmDatabaseImplement.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("LawFirmDatabaseImplement.Models.Order", b => { b.Property("Id") @@ -136,6 +163,9 @@ namespace LawFirmDatabaseImplement.Migrations b.Property("DocumentId") .HasColumnType("int"); + b.Property("ImplementerId") + .HasColumnType("int"); + b.Property("Status") .HasColumnType("int"); @@ -148,6 +178,8 @@ namespace LawFirmDatabaseImplement.Migrations b.HasIndex("DocumentId"); + b.HasIndex("ImplementerId"); + b.ToTable("Orders"); }); @@ -184,9 +216,15 @@ namespace LawFirmDatabaseImplement.Migrations .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + b.HasOne("LawFirmDatabaseImplement.Models.Implementer", "Implementer") + .WithMany("Orders") + .HasForeignKey("ImplementerId"); + b.Navigation("Client"); b.Navigation("Document"); + + b.Navigation("Implementer"); }); modelBuilder.Entity("LawFirmDatabaseImplement.Models.Blank", b => @@ -205,6 +243,11 @@ namespace LawFirmDatabaseImplement.Migrations b.Navigation("Orders"); }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Implementer", b => + { + b.Navigation("Orders"); + }); #pragma warning restore 612, 618 } } diff --git a/LawFirm/LawFirmDatabaseImplement/Models/Implementer.cs b/LawFirm/LawFirmDatabaseImplement/Models/Implementer.cs new file mode 100644 index 0000000..120f867 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Models/Implementer.cs @@ -0,0 +1,57 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.ViewModels; +using LawFirmDataModel.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmDatabaseImplement.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; } + [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, + 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, + WorkExperience = WorkExperience, + }; + } +} \ No newline at end of file diff --git a/LawFirm/LawFirmDatabaseImplement/Models/Order.cs b/LawFirm/LawFirmDatabaseImplement/Models/Order.cs index 959ea2b..8370033 100644 --- a/LawFirm/LawFirmDatabaseImplement/Models/Order.cs +++ b/LawFirm/LawFirmDatabaseImplement/Models/Order.cs @@ -21,10 +21,12 @@ namespace LawFirmDatabaseImplement.Models public OrderStatus Status { get; set; } [Required] public DateTime DateCreate { get; set; } + public int? ImplementerId { get; private set; } public DateTime? DateImplement { get; set; } public virtual Document Document { get; set; } public virtual Client Client { get; set; } - public int Id { get; set; } + public virtual Implementer? Implementer { get; private set; } + public int Id { get; set; } public static Order? Create(OrderBindingModel? model) { if (model == null) @@ -40,7 +42,8 @@ namespace LawFirmDatabaseImplement.Models Sum = model.Sum, Status = model.Status, DateCreate = model.DateCreate, - DateImplement = model.DateImplement + DateImplement = model.DateImplement, + ImplementerId = model.ImplementerId, }; } public void Update(OrderBindingModel? model) @@ -49,8 +52,14 @@ namespace LawFirmDatabaseImplement.Models { return; } + DocumentId = model.DocumentId; + Count = model.Count; + Sum = model.Sum; Status = model.Status; + DateCreate = model.DateCreate; DateImplement = model.DateImplement; + ImplementerId = model.ImplementerId; + Id = model.Id; } public OrderViewModel GetViewModel => new() { @@ -58,12 +67,14 @@ namespace LawFirmDatabaseImplement.Models DocumentId = DocumentId, ClientId = ClientId, ClientFIO = Client.ClientFIO, - Count = Count, + ImplementerFIO = Implementer?.ImplementerFIO ?? string.Empty, + Count = Count, Sum = Sum, Status = Status, DateCreate = DateCreate, DateImplement = DateImplement, - DocumentName = Document.DocumentName + DocumentName = Document.DocumentName, + ImplementerId = ImplementerId, }; } } \ No newline at end of file diff --git a/LawFirm/LawFirmFileImplement/DataFileSingleton.cs b/LawFirm/LawFirmFileImplement/DataFileSingleton.cs index ca048a7..6b6c656 100644 --- a/LawFirm/LawFirmFileImplement/DataFileSingleton.cs +++ b/LawFirm/LawFirmFileImplement/DataFileSingleton.cs @@ -10,11 +10,13 @@ namespace LawFirmFileImplement private readonly string OrderFileName = "Order.xml"; private readonly string DocumentFileName = "Document.xml"; private readonly string ClientFileName = "Client.xml"; - public List Blanks { get; private set; } + private readonly string ImplementerFileName = "Implementer.xml"; + public List Blanks { get; private set; } public List Orders { get; private set; } public List Documents { get; private set; } public List Clients { get; private set; } - public static DataFileSingleton GetInstance() + public List Implementers { get; private set; } + public static DataFileSingleton GetInstance() { if (instance == null) { @@ -26,13 +28,15 @@ namespace LawFirmFileImplement public void SaveDocuments() => SaveData(Documents, DocumentFileName, "Documents", x => x.GetXElement); public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); public void SaveClients() => SaveData(Clients, OrderFileName, "Clients", x => x.GetXElement); - private DataFileSingleton() + public void SaveImplementers() => SaveData(Orders, ImplementerFileName, "Implementers", x => x.GetXElement); + private DataFileSingleton() { Blanks = LoadData(BlankFileName, "Blank", x => Blank.Create(x)!)!; Documents = LoadData(DocumentFileName, "Document", x => Document.Create(x)!)!; - Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; - Clients = LoadData(ClientFileName, "Client", x => Client.Create(x)!)!; - } + Orders = new List(); + Clients = LoadData(ClientFileName, "Client", x => Client.Create(x)!)!; + Implementers = LoadData(ImplementerFileName, "Implementer", x => Implementer.Create(x)!)!; + } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) { diff --git a/LawFirm/LawFirmFileImplement/Implements/ImplementerStorage.cs b/LawFirm/LawFirmFileImplement/Implements/ImplementerStorage.cs new file mode 100644 index 0000000..7658d99 --- /dev/null +++ b/LawFirm/LawFirmFileImplement/Implements/ImplementerStorage.cs @@ -0,0 +1,90 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.SearchModels; +using LawFirmContracts.StoragesContracts; +using LawFirmContracts.ViewModels; +using LawFirmFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmFileImplement.Implements +{ + public class ImplementerStorage : IImplementerStorage + { + private readonly DataFileSingleton _source; + public ImplementerStorage() + { + _source = DataFileSingleton.GetInstance(); + } + 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; + } + 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.Id.HasValue) + { + var res = GetElement(model); + return res != null ? new() { res } : new(); + } + if (model.ImplementerFIO != null) // На случай если фио не будет уникальным (по заданию оно уникально) + { + return _source.Implementers + .Where(x => x.ImplementerFIO.Equals(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; + } + } +} \ No newline at end of file diff --git a/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs b/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs index 2c6a06e..adde7fd 100644 --- a/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs +++ b/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs @@ -33,8 +33,15 @@ namespace LawFirmFileImplement.Implements .Select(x => x.GetViewModel) .ToList(); } - var result = GetElement(model); - return result != null ? new() { result } : new(); + if (!model.Id.HasValue && model.OrderStatus.HasValue) + { + return source.Orders + .Where(x => model.OrderStatus == x.Status) + .Select(x => x.GetViewModel) + .ToList(); + } + var result = GetElement(model); + return result != null ? new() { result } : new(); } public OrderViewModel? GetElement(OrderSearchModel model) { @@ -42,6 +49,19 @@ namespace LawFirmFileImplement.Implements { return null; } + if (model.ImplementerId.HasValue && model.OrderStatus.HasValue) + { + return source.Orders + .FirstOrDefault(x => x.ImplementerId == model.ImplementerId && + model.OrderStatus == x.Status) + ?.GetViewModel; + } + if (model.ImplementerId.HasValue) + { + return source.Orders + .FirstOrDefault(x => x.ImplementerId == model.ImplementerId) + ?.GetViewModel; + } return source.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; } diff --git a/LawFirm/LawFirmFileImplement/Models/Implementer.cs b/LawFirm/LawFirmFileImplement/Models/Implementer.cs new file mode 100644 index 0000000..2e2da30 --- /dev/null +++ b/LawFirm/LawFirmFileImplement/Models/Implementer.cs @@ -0,0 +1,76 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.ViewModels; +using LawFirmDataModel.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace LawFirmFileImplement.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() + { + ImplementerFIO = element.Element("FIO")!.Value, + Password = element.Element("Password")!.Value, + Id = Convert.ToInt32(element.Attribute("Id")!.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, + 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, + }; + public XElement GetXElement => new("Client", + new XAttribute("Id", Id), + new XElement("Password", Password), + new XElement("FIO", ImplementerFIO), + new XElement("Qualification", Qualification), + new XElement("WorkExperience", WorkExperience) + ); + } +} \ No newline at end of file diff --git a/LawFirm/LawFirmFileImplement/Models/Order.cs b/LawFirm/LawFirmFileImplement/Models/Order.cs index b2b8273..3d71639 100644 --- a/LawFirm/LawFirmFileImplement/Models/Order.cs +++ b/LawFirm/LawFirmFileImplement/Models/Order.cs @@ -11,7 +11,8 @@ namespace LawFirmFileImplement.Models public int Id { get; private set; } public int DocumentId { get; private set; } public int ClientId { get; set; } - public int Count { get; private set; } + public int? ImplementerId { get; set; } + public int Count { get; private set; } public double Sum { get; private set; } public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; public DateTime DateCreate { get; private set; } = DateTime.Now; @@ -32,6 +33,7 @@ namespace LawFirmFileImplement.Models Status = model.Status, DateCreate = model.DateCreate, DateImplement = model.DateImplement, + ImplementerId = model.ImplementerId }; } public static Order? Create(XElement element) @@ -40,12 +42,18 @@ namespace LawFirmFileImplement.Models { return null; } + var dateImplement = element.Element("DateImplement")!.Value; var order = new Order() { Id = Convert.ToInt32(element.Attribute("Id")!.Value), DocumentId = Convert.ToInt32(element.Element("DocumentId")!.Value), Count = Convert.ToInt32(element.Element("Count")!.Value), Sum = Convert.ToDouble(element.Element("Sum")!.Value), + Status = (OrderStatus)Convert.ToInt32(element.Element("Status")!.Value), + ClientId = Convert.ToInt32(element.Element("ClientId")!.Value), + DateCreate = Convert.ToDateTime(element.Element("DateCreate")!.Value), + DateImplement = string.IsNullOrEmpty(dateImplement) ? null : Convert.ToDateTime(dateImplement), + ImplementerId = Convert.ToInt32(element.Element("ImplementerId")!.Value), }; DateTime.TryParse(element.Element("DateImplement")!.Value, out DateTime dateImpl); order.DateImplement = dateImpl; @@ -63,8 +71,14 @@ namespace LawFirmFileImplement.Models { return; } + DocumentId = model.DocumentId; + Count = model.Count; + Sum = model.Sum; Status = model.Status; + DateCreate = model.DateCreate; + ImplementerId = model.ImplementerId; DateImplement = model.DateImplement; + Id = model.Id; } public OrderViewModel GetViewModel => new() { @@ -76,6 +90,7 @@ namespace LawFirmFileImplement.Models DateImplement = DateImplement, Id = Id, Status = Status, + ImplementerId = ImplementerId }; public XElement GetXElement => new("Order", new XAttribute("Id", Id), @@ -84,6 +99,7 @@ namespace LawFirmFileImplement.Models new XElement("Sum", Sum.ToString()), new XElement("Status", Status.ToString()), new XElement("DateCreate", DateCreate.ToString()), - new XElement("DateImplement", DateImplement.ToString())); + new XElement("DateImplement", DateImplement.ToString()), + new XElement("ImplementerId", ImplementerId)); } } \ No newline at end of file diff --git a/LawFirm/LawFirmListImplement/DataListSingleton.cs b/LawFirm/LawFirmListImplement/DataListSingleton.cs index 50f82b5..19cedfa 100644 --- a/LawFirm/LawFirmListImplement/DataListSingleton.cs +++ b/LawFirm/LawFirmListImplement/DataListSingleton.cs @@ -9,13 +9,15 @@ namespace LawFirmListImplement public List Orders { get; set; } public List Documents { get; set; } public List Clients { get; set; } - private DataListSingleton() + public List Implementers { get; set; } + private DataListSingleton() { Blanks = new List(); Orders = new List(); Documents = new List(); Clients = new List(); - } + Implementers = new List(); + } public static DataListSingleton GetInstance() { if (_instance == null) diff --git a/LawFirm/LawFirmListImplement/Implements/ImplementerStorage.cs b/LawFirm/LawFirmListImplement/Implements/ImplementerStorage.cs new file mode 100644 index 0000000..244d79d --- /dev/null +++ b/LawFirm/LawFirmListImplement/Implements/ImplementerStorage.cs @@ -0,0 +1,113 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.SearchModels; +using LawFirmContracts.StoragesContracts; +using LawFirmContracts.ViewModels; +using LawFirmListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmListImplement.Implements +{ + public class ImplementerStorage : IImplementerStorage + { + private readonly DataListSingleton _source; + public ImplementerStorage() + { + _source = DataListSingleton.GetInstance(); + } + 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; + } + 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(); + } + if (model.Id.HasValue) + { + var res = GetElement(model); + return res != null ? new() { res } : new(); + } + // На случай если при расширении проекта фио не будет уникальным + // (по заданию оно уникально) + List result = new(); + if (model.ImplementerFIO != null) + { + foreach (var implementer in _source.Implementers) + { + if (implementer.ImplementerFIO.Equals(model.ImplementerFIO)) + { + result.Add(implementer.GetViewModel); + } + } + } + return result; + } + 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; + } + } +} \ No newline at end of file diff --git a/LawFirm/LawFirmListImplement/Implements/OrderStorage.cs b/LawFirm/LawFirmListImplement/Implements/OrderStorage.cs index 220c8ba..8c446b3 100644 --- a/LawFirm/LawFirmListImplement/Implements/OrderStorage.cs +++ b/LawFirm/LawFirmListImplement/Implements/OrderStorage.cs @@ -32,10 +32,10 @@ namespace LawFirmListImplement.Implements foreach (var order in _source.Orders) { if (order.Id == model.Id || - model.DateFrom <= order.DateCreate && - order.DateCreate <= model.DateTo || - model.ClientId.HasValue && - order.ClientId == model.ClientId) + model.DateFrom <= order.DateCreate && order.DateCreate <= model.DateTo || + model.ClientId.HasValue && order.ClientId == model.ClientId || + model.ImplementerId.HasValue && order.ImplementerId == model.ImplementerId || + model.OrderStatus.HasValue && model.OrderStatus == order.Status) { result.Add(GetViewModel(order)); } @@ -50,7 +50,16 @@ namespace LawFirmListImplement.Implements } foreach (var order in _source.Orders) { - if (model.Id.HasValue && order.Id == model.Id) + if (order.Id == model.Id) + { + return GetViewModel(order); + } + else if (model.ImplementerId.HasValue && model.OrderStatus.HasValue && + order.ImplementerId == model.ImplementerId && model.OrderStatus == order.Status) + { + return GetViewModel(order); + } + else if (model.ImplementerId.HasValue && model.ImplementerId == order.ImplementerId) { return GetViewModel(order); } diff --git a/LawFirm/LawFirmListImplement/Models/Implementer.cs b/LawFirm/LawFirmListImplement/Models/Implementer.cs new file mode 100644 index 0000000..ef82993 --- /dev/null +++ b/LawFirm/LawFirmListImplement/Models/Implementer.cs @@ -0,0 +1,53 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.ViewModels; +using LawFirmDataModel.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmListImplement.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, + }; + } +} \ No newline at end of file diff --git a/LawFirm/LawFirmListImplement/Models/Order.cs b/LawFirm/LawFirmListImplement/Models/Order.cs index b6bdf51..d1c31db 100644 --- a/LawFirm/LawFirmListImplement/Models/Order.cs +++ b/LawFirm/LawFirmListImplement/Models/Order.cs @@ -10,7 +10,8 @@ namespace LawFirmListImplement.Models public int Id { get; private set; } public int DocumentId { get; private set; } public int ClientId { get; private set; } - public int Count { get; private set; } + public int? ImplementerId { get; private set; } + public int Count { get; private set; } public double Sum { get; private set; } public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; public DateTime DateCreate { get; private set; } = DateTime.Now; @@ -30,7 +31,8 @@ namespace LawFirmListImplement.Models Sum = model.Sum, Status = model.Status, DateCreate = model.DateCreate, - DateImplement = model.DateImplement + DateImplement = model.DateImplement, + ImplementerId = model.ImplementerId, }; } public void Update(OrderBindingModel? model) @@ -39,8 +41,14 @@ namespace LawFirmListImplement.Models { return; } + DocumentId = model.DocumentId; + Count = model.Count; + Sum = model.Sum; Status = model.Status; + DateCreate = model.DateCreate; DateImplement = model.DateImplement; + ImplementerId = model.ImplementerId; + Id = model.Id; } public OrderViewModel GetViewModel => new() { @@ -51,7 +59,8 @@ namespace LawFirmListImplement.Models Sum = Sum, Status = Status, DateCreate = DateCreate, - DateImplement = DateImplement + DateImplement = DateImplement, + ImplementerId = ImplementerId }; } } \ No newline at end of file