diff --git a/ConfectionaryFileImplement/Order.cs b/ConfectionaryFileImplement/Order.cs index 177a901..cc960c8 100644 --- a/ConfectionaryFileImplement/Order.cs +++ b/ConfectionaryFileImplement/Order.cs @@ -14,6 +14,7 @@ namespace ConfectioneryFileImplement.Models public int ClientId { get; set; } + public int? ImplementerId { get; set; } public int Count { get; private set; } @@ -40,7 +41,8 @@ namespace ConfectioneryFileImplement.Models Status = model.Status, DateCreate = model.DateCreate, DateImplement = model.DateImplement, - Id = model.Id, + ImplementerId = model.ImplementerId, + Id = model.Id, }; } public static Order? Create(XElement element) @@ -59,6 +61,7 @@ namespace ConfectioneryFileImplement.Models PastryId = Convert.ToInt32(element.Element("PastryId")!.Value), ClientId = Convert.ToInt32(element.Element("ClientId")!.Value), DateCreate = Convert.ToDateTime(element.Element("DateCreate")!.Value), + ImplementerId = Convert.ToInt32(element.Element("ImplementerId")!.Value), DateImplement = string.IsNullOrEmpty(dateImplement) ? null : Convert.ToDateTime(dateImplement), }; } @@ -79,15 +82,17 @@ namespace ConfectioneryFileImplement.Models } public OrderViewModel GetViewModel => new() { + ImplementerFIO = DataFileSingleton.GetInstance().Implementers.FirstOrDefault(x => x.Id == ImplementerId)?.ImplementerFIO ?? string.Empty, PastryName = DataFileSingleton.GetInstance().Pastries.FirstOrDefault(x => x.Id == PastryId)?.PastryName ?? string.Empty, ClientFIO = DataFileSingleton.GetInstance().Clients.FirstOrDefault(x => x.Id == ClientId)?.ClientFIO ?? string.Empty, - PastryId = PastryId, + PastryId = PastryId, ClientId = ClientId, Count = Count, Sum = Sum, Status = Status, DateCreate = DateCreate, DateImplement = DateImplement, + ImplementerId = ImplementerId, Id = Id, }; public XElement GetXElement => new("Order", @@ -98,7 +103,8 @@ namespace ConfectioneryFileImplement.Models new XElement("Sum", Sum.ToString()), new XElement("Status", (int)Status), new XElement("DateCreate", DateCreate), - new XElement("DateImplement", DateImplement) + new XElement("DateImplement", DateImplement), + new XElement("ImplementerId", ImplementerId) ); } } diff --git a/ConfectionaryFileImplement/OrderStorage.cs b/ConfectionaryFileImplement/OrderStorage.cs index 266422d..ecd3a9b 100644 --- a/ConfectionaryFileImplement/OrderStorage.cs +++ b/ConfectionaryFileImplement/OrderStorage.cs @@ -28,11 +28,15 @@ namespace ConfectioneryFileImplement public OrderViewModel? GetElement(OrderSearchModel model) { - if (!model.Id.HasValue) - { - return null; - } - return _source.Orders.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.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; } public List GetFilteredList(OrderSearchModel model) @@ -51,7 +55,14 @@ namespace ConfectioneryFileImplement .Select(x => x.GetViewModel) .ToList(); } - var result = GetElement(model); + if (!model.Id.HasValue && model.Status.HasValue) + { + return _source.Orders + .Where(x => x.Status == model.Status) + .Select(x => x.GetViewModel) + .ToList(); + } + var result = GetElement(model); return result != null ? new() { result } : new(); } diff --git a/ConfectionaryListImplement/Order.cs b/ConfectionaryListImplement/Order.cs index 9954ae8..85a9423 100644 --- a/ConfectionaryListImplement/Order.cs +++ b/ConfectionaryListImplement/Order.cs @@ -13,6 +13,8 @@ namespace ConfectioneryListImplement.Models public int ClientId { get; private set; } + public int? ImplementerId { get; private set; } + public int Count { get; private set; } public double Sum { get; private set; } @@ -38,6 +40,7 @@ namespace ConfectioneryListImplement.Models Status = model.Status, DateCreate = model.DateCreate, DateImplement = model.DateImplement, + ImplementerId = model.ImplementerId, Id = model.Id, }; } @@ -60,7 +63,9 @@ namespace ConfectioneryListImplement.Models PastryId = PastryId, ClientFIO = DataListSingleton.GetInstance().Clients.FirstOrDefault(x => x.Id == ClientId)?.ClientFIO ?? string.Empty, ClientId = ClientId, - Count = Count, + ImplementerId = ImplementerId, + ImplementerFIO = DataListSingleton.GetInstance().Implementers.FirstOrDefault(x => x.Id == ImplementerId)?.ImplementerFIO ?? string.Empty, + Count = Count, Sum = Sum, Status = Status, DateCreate = DateCreate, diff --git a/ConfectionaryListImplement/OrderStorage.cs b/ConfectionaryListImplement/OrderStorage.cs index 8b0558a..7159bf2 100644 --- a/ConfectionaryListImplement/OrderStorage.cs +++ b/ConfectionaryListImplement/OrderStorage.cs @@ -30,6 +30,10 @@ namespace ConfectioneryListImplement public OrderViewModel? GetElement(OrderSearchModel model) { + if (model.ImplementerId.HasValue) + { + return _source.Orders.FirstOrDefault(x => x.ImplementerId == model.ImplementerId)?.GetViewModel; + } if (!model.Id.HasValue) { return null; @@ -61,6 +65,18 @@ namespace ConfectioneryListImplement .Select(x => x.GetViewModel) .ToList(); } + if (!model.Id.HasValue && model.Status.HasValue) + { + return _source.Orders + .Where(x => x.Status == model.Status) + .Select(x => x.GetViewModel) + .ToList(); + } + if (model.ImplementerId.HasValue) + { + var res = _source.Orders.FirstOrDefault(x => x.ImplementerId == model.ImplementerId); + return res != null ? new() { res.GetViewModel } : new(); + } foreach (var order in _source.Orders) { if (order.Id == model.Id) diff --git a/ConfectioneryContracts/BindingModels/OrderBindingModel.cs b/ConfectioneryContracts/BindingModels/OrderBindingModel.cs index 62eec79..1961d7d 100644 --- a/ConfectioneryContracts/BindingModels/OrderBindingModel.cs +++ b/ConfectioneryContracts/BindingModels/OrderBindingModel.cs @@ -8,6 +8,7 @@ namespace ConfectioneryContracts.BindingModels public int Id { get; set; } public int PastryId { get; set; } public int ClientId { get; set; } + public int? ImplementerId { get; set; } public int Count { get; set; } public double Sum { get; set; } public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; diff --git a/ConfectioneryContracts/SearchModels/OrderSearchModel.cs b/ConfectioneryContracts/SearchModels/OrderSearchModel.cs index 2aaa9a2..606260e 100644 --- a/ConfectioneryContracts/SearchModels/OrderSearchModel.cs +++ b/ConfectioneryContracts/SearchModels/OrderSearchModel.cs @@ -1,4 +1,6 @@ -namespace ConfectioneryContracts.SearchModels +using ConfectioneryDataModels.Enums; + +namespace ConfectioneryContracts.SearchModels { public class OrderSearchModel { @@ -9,5 +11,9 @@ public DateTime? DateTo { get; set; } public int? ClientId { get; set; } + + public int? ImplementerId { get; set; } + + public OrderStatus? Status { get; set; } } } diff --git a/ConfectioneryContracts/ViewModels/OrderViewModel.cs b/ConfectioneryContracts/ViewModels/OrderViewModel.cs index 32a6d71..49e9198 100644 --- a/ConfectioneryContracts/ViewModels/OrderViewModel.cs +++ b/ConfectioneryContracts/ViewModels/OrderViewModel.cs @@ -17,10 +17,15 @@ namespace ConfectioneryContracts.ViewModels public int ClientId { get; set; } - [DisplayName("Фамилия клиента")] + public int? ImplementerId { get; set; } + + [DisplayName("Фамилия клиента")] public string ClientFIO { get; set; } = string.Empty; - [DisplayName("Изделие")] + [DisplayName("Фамилия исполнителя")] + public string ImplementerFIO { get; set; } = string.Empty; + + [DisplayName("Изделие")] public string PastryName { get; set; } = string.Empty; [DisplayName("Количество")] diff --git a/ConfectioneryDataModels/IOrderModel.cs b/ConfectioneryDataModels/IOrderModel.cs index c168d51..ddfc3e7 100644 --- a/ConfectioneryDataModels/IOrderModel.cs +++ b/ConfectioneryDataModels/IOrderModel.cs @@ -6,6 +6,7 @@ namespace ConfectioneryDataModels.Models { int PastryId { get; } int ClientId { get; } + int? ImplementerId { get; } int Count { get; } double Sum { get; } OrderStatus Status { get; } diff --git a/ConfectioneryDatabaseImplement/Implementer.cs b/ConfectioneryDatabaseImplement/Implementer.cs index 7eca337..6de89d0 100644 --- a/ConfectioneryDatabaseImplement/Implementer.cs +++ b/ConfectioneryDatabaseImplement/Implementer.cs @@ -1,8 +1,9 @@ using ConfectioneryContracts.BindingModels; using ConfectioneryContracts.ViewModels; using ConfectioneryDataModels; +using System.ComponentModel.DataAnnotations.Schema; -namespace ConfectioneryDatabaseImplement +namespace ConfectioneryDatabaseImplement.Models { public class Implementer : IImplementerModel { @@ -16,6 +17,9 @@ namespace ConfectioneryDatabaseImplement 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) diff --git a/ConfectioneryDatabaseImplement/ImplementerStorage.cs b/ConfectioneryDatabaseImplement/ImplementerStorage.cs index 99ae586..6562763 100644 --- a/ConfectioneryDatabaseImplement/ImplementerStorage.cs +++ b/ConfectioneryDatabaseImplement/ImplementerStorage.cs @@ -3,37 +3,33 @@ using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.StoragesContract; using ConfectioneryContracts.ViewModels; -namespace ConfectioneryListImplement +namespace ConfectioneryDatabaseImplement { public class ImplementerStorage : IImplementerStorage { - private readonly DataListSingleton _source; - public ImplementerStorage() - { - _source = DataListSingleton.GetInstance(); - } - public ImplementerViewModel? Delete(ImplementerBindingModel model) { - var res = _source.Implementers.FirstOrDefault(x => x.Id == model.Id); + using var context = new ConfectioneryDatabase(); + var res = context.Implementers.FirstOrDefault(x => x.Id == model.Id); if (res != null) { - _source.Implementers.Remove(res); + context.Implementers.Remove(res); } return res?.GetViewModel; } public ImplementerViewModel? GetElement(ImplementerSearchModel model) { + using var context = new ConfectioneryDatabase(); if (model.Id.HasValue) - return _source.Implementers.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + return context.Implementers.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; if (model.ImplementerFIO != null && model.Password != null) - return _source.Implementers + return context.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 context.Implementers.FirstOrDefault(x => x.ImplementerFIO.Equals(model.ImplementerFIO))?.GetViewModel; return null; } @@ -50,7 +46,8 @@ namespace ConfectioneryListImplement } if (model.ImplementerFIO != null) // На случай если фио не будет уникальным (по заданию оно уникально) { - return _source.Implementers + using var context = new ConfectioneryDatabase(); + return context.Implementers .Where(x => x.ImplementerFIO.Equals(model.ImplementerFIO)) .Select(x => x.GetViewModel) .ToList(); @@ -60,23 +57,25 @@ namespace ConfectioneryListImplement public List GetFullList() { - return _source.Implementers.Select(x => x.GetViewModel).ToList(); + using var context = new ConfectioneryDatabase(); + return context.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; + using var context = new ConfectioneryDatabase(); var res = Implementer.Create(model); if (res != null) { - _source.Implementers.Add(res); + context.Implementers.Add(res); } return res?.GetViewModel; } public ImplementerViewModel? Update(ImplementerBindingModel model) { - var res = _source.Implementers.FirstOrDefault(x => x.Id == model.Id); + using var context = new ConfectioneryDatabase(); + var res = context.Implementers.FirstOrDefault(x => x.Id == model.Id); res?.Update(model); return res?.GetViewModel; } diff --git a/ConfectioneryDatabaseImplement/Order.cs b/ConfectioneryDatabaseImplement/Order.cs index 9859bee..1370bd8 100644 --- a/ConfectioneryDatabaseImplement/Order.cs +++ b/ConfectioneryDatabaseImplement/Order.cs @@ -24,7 +24,9 @@ namespace ConfectioneryDatabaseImplement.Models [Required] public int ClientId { get; private set; } - [Required] + public int? ImplementerId { get; private set; } + + [Required] public int Count { get; private set; } [Required] @@ -40,9 +42,10 @@ namespace ConfectioneryDatabaseImplement.Models public Pastry Pastry { get; private set; } - public Client Client { get; private set; } + public Implementer? Implementer { get; private set; } + public static Order? Create(OrderBindingModel? model) { if (model == null) @@ -57,6 +60,7 @@ namespace ConfectioneryDatabaseImplement.Models Sum = model.Sum, Status = model.Status, DateCreate = model.DateCreate, + ImplementerId = model.ImplementerId, DateImplement = model.DateImplement, Id = model.Id, }; @@ -86,12 +90,15 @@ namespace ConfectioneryDatabaseImplement.Models PastryName = context.Pastries.FirstOrDefault(x => x.Id == PastryId)?.PastryName ?? string.Empty, // ?????? ClientFIO = Client?.ClientFIO ?? context.Clients.FirstOrDefault(x => x.Id == ClientId)?.ClientFIO ?? string.Empty, + ImplementerFIO = Implementer?.ImplementerFIO ?? + context.Implementers.FirstOrDefault(x => x.Id == ImplementerId)?.ImplementerFIO ?? string.Empty, PastryId = PastryId, Count = Count, Sum = Sum, Status = Status, DateCreate = DateCreate, DateImplement = DateImplement, + ImplementerId = ImplementerId, Id = Id, }; } diff --git a/ConfectioneryDatabaseImplement/OrderStorage.cs b/ConfectioneryDatabaseImplement/OrderStorage.cs index e962a50..8466ddc 100644 --- a/ConfectioneryDatabaseImplement/OrderStorage.cs +++ b/ConfectioneryDatabaseImplement/OrderStorage.cs @@ -26,7 +26,11 @@ namespace ConfectioneryDatabaseImplement.Implements public OrderViewModel? GetElement(OrderSearchModel model) { using var context = new ConfectioneryDatabase(); - if (!model.Id.HasValue) + if (model.ImplementerId.HasValue) + { + return context.Orders.FirstOrDefault(x => x.ImplementerId == model.ImplementerId)?.GetViewModel; + } + if (!model.Id.HasValue) { return null; } @@ -52,7 +56,14 @@ namespace ConfectioneryDatabaseImplement.Implements .Select(x => x.GetViewModel) .ToList(); } - if (model.ClientId.HasValue) + if (model.Status.HasValue) + { + return context.Orders + .Where(x => x.Status == model.Status) + .Select(x => x.GetViewModel) + .ToList(); + } + if (model.ClientId.HasValue) { return context.Orders .Where(x => x.Client.Id == model.ClientId)