diff --git a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/ClientLogic.cs b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/ClientLogic.cs new file mode 100644 index 0000000..ebadb4d --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/ClientLogic.cs @@ -0,0 +1,160 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.BusinessLogicsContracts; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopContracts.StoragesContracts; +using BlacksmithWorkshopContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopBusinessLogic.BusinessLogic +{ + public class ClientLogic : IClientLogic + { + private readonly ILogger _logger; + + private readonly IClientStorage _clientStorage; + + public ClientLogic(ILogger logger, IClientStorage clientStorage) + { + _logger = logger; + _clientStorage = clientStorage; + } + + public List? ReadList(ClientSearchModel? model) + { + _logger.LogInformation("ReadList. ClientFIO:{ClientFIO}. Id:{Id}", model?.ClientFIO, model?.Id); + + //list хранит весь список в случае, если model пришло со значением null на вход метода + var list = model == null ? _clientStorage.GetFullList() : _clientStorage.GetFilteredList(model); + + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + + return list; + } + + public ClientViewModel? ReadElement(ClientSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + _logger.LogInformation("ReadList. ClientFIO:{ClientFIO}. Id:{Id}", model.ClientFIO, model?.Id); + + var element = _clientStorage.GetElement(model); + + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + + return null; + } + + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + + return element; + } + + public bool Create(ClientBindingModel model) + { + CheckModel(model); + + if (_clientStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + + return false; + } + + return true; + } + + public bool Update(ClientBindingModel model) + { + CheckModel(model); + + if (_clientStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + + return false; + } + + return true; + } + + public bool Delete(ClientBindingModel model) + { + CheckModel(model, false); + + _logger.LogInformation("Delete. Id:{Id}", model.Id); + + if (_clientStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + + return false; + } + + return true; + } + + //проверка входного аргумента для методов Insert, Update и Delete + private void CheckModel(ClientBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + //так как при удалении передаём как параметр false + if (!withParams) + { + return; + } + + //проверка на наличие ФИО + if (string.IsNullOrEmpty(model.ClientFIO)) + { + throw new ArgumentNullException("Отсутствие ФИО в учётной записи", nameof(model.ClientFIO)); + } + + //проверка на наличие почты + if (string.IsNullOrEmpty(model.Email)) + { + throw new ArgumentNullException("Отсутствие почты в учётной записи (логина)", nameof(model.Email)); + } + + //проверка на наличие пароля + if (string.IsNullOrEmpty(model.Password)) + { + throw new ArgumentNullException("Отсутствие пароля в учётной записи", nameof(model.Password)); + } + + _logger.LogInformation("WorkPiece. ClientFIO:{ClientFIO}. Email:{Email}. Password:{Password}. Id:{Id}", + model.ClientFIO, model.Email, model.Password, model.Id); + + //для проверка на наличие такого же аккаунта + var element = _clientStorage.GetElement(new ClientSearchModel + { + Email = model.Email, + }); + + //если элемент найден и его Id не совпадает с Id переданного объекта + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Аккаунт с таким логином уже есть"); + } + } + } +} \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/OrderLogic.cs b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/OrderLogic.cs index 6ca58be..b7e1316 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/OrderLogic.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/OrderLogic.cs @@ -122,7 +122,12 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogic throw new InvalidOperationException("Дата создания должна быть более ранней, нежели дата завершения"); } - _logger.LogInformation("Order. OrderId:{Id}. Sun:{Sum}. ManufactureId:{Id}", model.Id, model.Sum, model.ManufactureId); + if (model.ClientId < 0) + { + throw new ArgumentNullException("Некорректный идентификатор у клиента", nameof(model.ClientId)); + } + + _logger.LogInformation("Order. OrderId:{Id}. Sum:{Sum}. ClientId:{ClientId}. ManufactureId:{Id}", model.Id, model.Sum, model.ClientId, model.ManufactureId); } //обновление статуса заказа diff --git a/BlacksmithWorkshop/BlacksmithWorkshopContracts/BindingModels/ClientBindingModel.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/BindingModels/ClientBindingModel.cs new file mode 100644 index 0000000..43922fa --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/BindingModels/ClientBindingModel.cs @@ -0,0 +1,20 @@ +using BlacksmithWorkshopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopContracts.BindingModels +{ + public class ClientBindingModel : IClientModel + { + public int Id { get; set; } + + public string ClientFIO { get; set; } = string.Empty; + + public string Email { get; set; } = string.Empty; + + public string Password { get; set; } = string.Empty; + } +} \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshopContracts/BindingModels/OrderBindingModel.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/BindingModels/OrderBindingModel.cs index 5ab83dc..19b395a 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopContracts/BindingModels/OrderBindingModel.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/BindingModels/OrderBindingModel.cs @@ -13,6 +13,8 @@ namespace BlacksmithWorkshopContracts.BindingModels { public int Id { get; set; } + public int ClientId { get; set; } + public int ManufactureId { get; set; } public int Count { get; set; } @@ -25,4 +27,4 @@ namespace BlacksmithWorkshopContracts.BindingModels public DateTime? DateImplement { get; set; } } -} +} \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshopContracts/BusinessLogicsContracts/IClientLogic.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/BusinessLogicsContracts/IClientLogic.cs new file mode 100644 index 0000000..a665387 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/BusinessLogicsContracts/IClientLogic.cs @@ -0,0 +1,24 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopContracts.BusinessLogicsContracts +{ + public interface IClientLogic + { + List? ReadList(ClientSearchModel? model); + + ClientViewModel? ReadElement(ClientSearchModel model); + + bool Create(ClientBindingModel model); + + bool Update(ClientBindingModel model); + + bool Delete(ClientBindingModel model); + } +} \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshopContracts/SearchModels/ClientSearchModel.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/SearchModels/ClientSearchModel.cs new file mode 100644 index 0000000..8db09a9 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/SearchModels/ClientSearchModel.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopContracts.SearchModels +{ + public class ClientSearchModel + { + public int? Id { get; set; } + + public string? ClientFIO { get; set; } + + public string? Email { get; set; } + + public string? Password { get; set; } + } +} \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshopContracts/SearchModels/OrderSearchModel.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/SearchModels/OrderSearchModel.cs index b65f4d6..2e5ea0f 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopContracts/SearchModels/OrderSearchModel.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/SearchModels/OrderSearchModel.cs @@ -12,6 +12,9 @@ namespace BlacksmithWorkshopContracts.SearchModels //для поиска по идентификатору public int? Id { get; set; } + //для поиска по клиенту + public int? ClientId { get; set; } + //два поля для возможности производить выборку public DateTime? DateFrom { get; set; } diff --git a/BlacksmithWorkshop/BlacksmithWorkshopContracts/StoragesContracts/IClientStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/StoragesContracts/IClientStorage.cs new file mode 100644 index 0000000..8b5d2d8 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/StoragesContracts/IClientStorage.cs @@ -0,0 +1,26 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopContracts.StoragesContracts +{ + public interface IClientStorage + { + List GetFullList(); + + List GetFilteredList(ClientSearchModel model); + + ClientViewModel? GetElement(ClientSearchModel model); + + ClientViewModel? Insert(ClientBindingModel model); + + ClientViewModel? Update(ClientBindingModel model); + + ClientViewModel? Delete(ClientBindingModel model); + } +} \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshopContracts/ViewModels/ClientViewModel.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/ViewModels/ClientViewModel.cs new file mode 100644 index 0000000..7ef898a --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/ViewModels/ClientViewModel.cs @@ -0,0 +1,25 @@ +using BlacksmithWorkshopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopContracts.ViewModels +{ + public class ClientViewModel : IClientModel + { + public int Id { get; set; } + + [DisplayName("ФИО клиента")] + public string ClientFIO { get; set; } = string.Empty; + + [DisplayName("Логин (эл. почта)")] + public string Email { get; set; } = string.Empty; + + [DisplayName("Пароль")] + public string Password { get; set; } = string.Empty; + + } +} \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshopContracts/ViewModels/OrderViewModel.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/ViewModels/OrderViewModel.cs index 83174ed..9cec296 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopContracts/ViewModels/OrderViewModel.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/ViewModels/OrderViewModel.cs @@ -15,8 +15,13 @@ namespace BlacksmithWorkshopContracts.ViewModels [DisplayName("Номер")] public int Id { get; set; } + public int ClientId { get; set; } + public int ManufactureId { get; set; } + [DisplayName("ФИО клиента")] + public string ClientFIO { get; set; } = string.Empty; + [DisplayName("Изделие")] public string ManufactureName { get; set; } = string.Empty; diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDataModels/Models/IClientModel.cs b/BlacksmithWorkshop/BlacksmithWorkshopDataModels/Models/IClientModel.cs new file mode 100644 index 0000000..c45c383 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDataModels/Models/IClientModel.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopDataModels.Models +{ + public interface IClientModel : IId + { + string ClientFIO { get; } + + string Email { get; } + + string Password { get; } + } +} \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/ClientStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/ClientStorage.cs new file mode 100644 index 0000000..e398dee --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/ClientStorage.cs @@ -0,0 +1,130 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopContracts.StoragesContracts; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopDatabaseImplement.Implements +{ + public class ClientStorage : IClientStorage + { + public ClientViewModel? Delete(ClientBindingModel model) + { + using var context = new BlacksmithWorkshopDatabase(); + + var element = context.Clients + .FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + // для отображения КОРРЕКТНОЙ ViewModel-и + var deletedElement = context.Clients + .Include(x => x.Orders) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + + context.Clients.Remove(element); + context.SaveChanges(); + + return deletedElement; + } + + return null; + } + + public ClientViewModel? GetElement(ClientSearchModel model) + { + using var context = new BlacksmithWorkshopDatabase(); + + if (model.Id.HasValue) + { + return context.Clients + .Include(x => x.Orders) + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) + ?.GetViewModel; + } + else if (!string.IsNullOrEmpty(model.Email) && !string.IsNullOrEmpty(model.Password)) + { + return context.Clients + .Include(x => x.Orders) + .FirstOrDefault(x => (x.Email == model.Email && x.Password == model.Password)) + ?.GetViewModel; + } + + return new(); + } + + public List GetFilteredList(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.ClientFIO)) + { + return new(); + } + + using var context = new BlacksmithWorkshopDatabase(); + + return context.Clients + .Include(x => x.Orders) + .Where(x => x.ClientFIO.Contains(model.ClientFIO)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new BlacksmithWorkshopDatabase(); + + return context.Clients + .Include(x => x.Orders) + .Select(x => x.GetViewModel) + .ToList(); + } + + public ClientViewModel? Insert(ClientBindingModel model) + { + var newClient = Client.Create(model); + + if (newClient == null) + { + return null; + } + + using var context = new BlacksmithWorkshopDatabase(); + + context.Clients.Add(newClient); + context.SaveChanges(); + + return context.Clients + .Include(x => x.Orders) + .FirstOrDefault(x => x.Id == newClient.Id) + ?.GetViewModel; + } + + public ClientViewModel? Update(ClientBindingModel model) + { + using var context = new BlacksmithWorkshopDatabase(); + var order = context.Clients + .Include(x => x.Orders) + .FirstOrDefault(x => x.Id == model.Id); + + if (order == null) + { + return null; + } + + order.Update(model); + context.SaveChanges(); + + return context.Clients + .Include(x => x.Orders) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + } + } +} \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/OrderStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/OrderStorage.cs index c36ac62..f3f2fa2 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/OrderStorage.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/OrderStorage.cs @@ -17,15 +17,23 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements public OrderViewModel? Delete(OrderBindingModel model) { using var context = new BlacksmithWorkshopDatabase(); + var element = context.Orders .FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { + // для отображения КОРРЕКТНОЙ ViewModel-и + var deletedElement = context.Orders + .Include(x => x.Manufacture) + .Include(x => x.Client) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + context.Orders.Remove(element); context.SaveChanges(); - return element.GetViewModel; + return deletedElement; } return null; @@ -41,24 +49,45 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements using var context = new BlacksmithWorkshopDatabase(); return context.Orders + .Include(x => x.Manufacture) + .Include(x => x.Client) .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) ?.GetViewModel; } public List GetFilteredList(OrderSearchModel model) { - if (!model.Id.HasValue) - { - return new(); - } - using var context = new BlacksmithWorkshopDatabase(); - return context.Orders + if (!model.Id.HasValue && model.DateFrom.HasValue && model.DateTo.HasValue) + { + return context.Orders .Include(x => x.Manufacture) + .Include(x => x.Client) + .Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo) + .Select(x => x.GetViewModel) + .ToList(); + } + else if (model.Id.HasValue) + { + return context.Orders + .Include(x => x.Manufacture) + .Include(x => x.Client) .Where(x => x.Id == model.Id) .Select(x => x.GetViewModel) .ToList(); + } + else if (model.ClientId.HasValue) + { + return context.Orders + .Include(x => x.Manufacture) + .Include(x => x.Client) + .Where(x => x.ClientId == model.ClientId) + .Select(x => x.GetViewModel) + .ToList(); + } + + return new(); } public List GetFullList() @@ -66,9 +95,10 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements using var context = new BlacksmithWorkshopDatabase(); return context.Orders - .Include(x => x.Manufacture) - .Select(x => x.GetViewModel) - .ToList(); + .Include(x => x.Manufacture) + .Include(x => x.Client) + .Select(x => x.GetViewModel) + .ToList(); } public OrderViewModel? Insert(OrderBindingModel model) @@ -81,17 +111,24 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements } using var context = new BlacksmithWorkshopDatabase(); + context.Orders.Add(newOrder); context.SaveChanges(); - return context.Orders.Include(x => x.Manufacture) - .FirstOrDefault(x => x.Id == newOrder.Id)?.GetViewModel; + return context.Orders + .Include(x => x.Manufacture) + .Include(x => x.Client) + .FirstOrDefault(x => x.Id == newOrder.Id) + ?.GetViewModel; } public OrderViewModel? Update(OrderBindingModel model) { using var context = new BlacksmithWorkshopDatabase(); - var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + var order = context.Orders + .Include(x => x.Manufacture) + .Include(x => x.Client) + .FirstOrDefault(x => x.Id == model.Id); if (order == null) { @@ -101,7 +138,11 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements order.Update(model); context.SaveChanges(); - return order.GetViewModel; + return context.Orders + .Include(x => x.Manufacture) + .Include(x => x.Client) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; } } } \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Client.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Client.cs new file mode 100644 index 0000000..f9fcb6c --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Client.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopDatabaseImplement.Models +{ + internal class Client + { + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Order.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Order.cs index 7a8c5ad..7e2bc59 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Order.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Order.cs @@ -8,6 +8,7 @@ using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Xml.Linq; namespace BlacksmithWorkshopDatabaseImplement.Models { @@ -18,6 +19,9 @@ namespace BlacksmithWorkshopDatabaseImplement.Models [Required] public int ManufactureId { get; private set; } + [Required] + public int ClientId { get; private set; } + [Required] public int Count { get; private set; } @@ -35,6 +39,9 @@ namespace BlacksmithWorkshopDatabaseImplement.Models //для передачи названия изделия public virtual Manufacture Manufacture { get; set; } + //для передачи имени клиента + public virtual Client Client { get; set; } + public static Order? Create(OrderBindingModel model) { if (model == null) @@ -46,6 +53,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Models { Id = model.Id, ManufactureId = model.ManufactureId, + ClientId = model.ClientId, Count = model.Count, Sum = model.Sum, Status = model.Status, @@ -69,12 +77,14 @@ namespace BlacksmithWorkshopDatabaseImplement.Models { Id = Id, ManufactureId = ManufactureId, + ClientId = ClientId, Count = Count, Sum = Sum, Status = Status, DateCreate = DateCreate, DateImplement = DateImplement, - ManufactureName = Manufacture.ManufactureName + ManufactureName = Manufacture.ManufactureName, + ClientFIO = Client.ClientFIO }; } } \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/ClientStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/ClientStorage.cs new file mode 100644 index 0000000..d075cd8 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/ClientStorage.cs @@ -0,0 +1,90 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopContracts.StoragesContracts; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopFileImplement.Implements +{ + public class ClientStorage : IClientStorage + { + private readonly DataFileSingleton source; + + public ClientStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public List GetFullList() + { + return source.Clients.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(ClientSearchModel model) + { + return source.Clients.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList(); + } + + public ClientViewModel? GetElement(ClientSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + + return source.Clients.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public ClientViewModel? Insert(ClientBindingModel model) + { + model.Id = source.Clients.Count > 0 ? source.Clients.Max(x => x.Id) + 1 : 1; + + var newClient = Client.Create(model); + + if (newClient == null) + { + return null; + } + + source.Clients.Add(newClient); + source.SaveClients(); + + return newClient.GetViewModel; + } + + public ClientViewModel? Update(ClientBindingModel model) + { + var client = source.Clients.FirstOrDefault(x => x.Id == model.Id); + + if (client == null) + { + return null; + } + + client.Update(model); + source.SaveClients(); + + return client.GetViewModel; + } + + public ClientViewModel? Delete(ClientBindingModel model) + { + var element = source.Clients.FirstOrDefault(x => x.Id == model.Id); + + if (element != null) + { + source.Clients.Remove(element); + source.SaveClients(); + + return element.GetViewModel; + } + + return null; + } + } +} \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/OrderStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/OrderStorage.cs index 1dd5e07..930071d 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/OrderStorage.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/OrderStorage.cs @@ -28,12 +28,28 @@ namespace BlacksmithWorkshopFileImplement.Implements public List GetFilteredList(OrderSearchModel model) { - if (!model.Id.HasValue) + if (!model.Id.HasValue && model.DateFrom.HasValue && model.DateTo.HasValue) { - return new(); + return source.Orders.Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo) + .Select(x => GetViewModel(x)) + .ToList(); + } + else if (model.Id.HasValue) + { + return source.Orders + .Where(x => x.Id == model.Id) + .Select(x => GetViewModel(x)) + .ToList(); + } + else if (model.ClientId.HasValue) + { + return source.Orders + .Where(x => x.ClientId == model.ClientId) + .Select(x => GetViewModel(x)) + .ToList(); } - return source.Orders.Where(x => x.Id == model.Id).Select(x => GetViewModel(x)).ToList(); + return new(); } public OrderViewModel? GetElement(OrderSearchModel model) @@ -53,11 +69,18 @@ namespace BlacksmithWorkshopFileImplement.Implements var manufacture = source.Manufactures.FirstOrDefault(x => x.Id == order.ManufactureId); + var client = source.Clients.FirstOrDefault(x => x.Id == order.ClientId); + if (manufacture != null) { viewModel.ManufactureName = manufacture.ManufactureName; } + if (client != null) + { + viewModel.ClientFIO = client.ClientFIO; + } + return viewModel; } diff --git a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Client.cs b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Client.cs new file mode 100644 index 0000000..f00dc0a --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Client.cs @@ -0,0 +1,84 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopDataModels.Enums; +using BlacksmithWorkshopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace BlacksmithWorkshopFileImplement.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 Client() + { + 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 Client() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ClientFIO = element.Element("ClientFIO")!.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("Order", + new XAttribute("Id", Id), + new XElement("ClientFIO", ClientFIO), + new XElement("Email", Email), + new XElement("Password", Password)); + } +} \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Order.cs b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Order.cs index 563f1b4..ee17b60 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Order.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Order.cs @@ -17,6 +17,8 @@ namespace BlacksmithWorkshopFileImplement.Models { public int Id { get; private set; } + public int ClientId { get; private set; } + public int ManufactureId { get; private set; } public int Count { get; private set; } @@ -39,6 +41,7 @@ namespace BlacksmithWorkshopFileImplement.Models return new Order() { Id = model.Id, + ClientId = model.ClientId, ManufactureId = model.ManufactureId, Count = model.Count, Sum = model.Sum, @@ -58,6 +61,7 @@ namespace BlacksmithWorkshopFileImplement.Models return new Order() { Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ClientId = Convert.ToInt32(element.Attribute("Id")!.Value), ManufactureId = Convert.ToInt32(element.Element("ManufactureId")!.Value), Count = Convert.ToInt32(element.Element("Count")!.Value), Sum = Convert.ToDouble(element.Element("Sum")!.Value), @@ -82,6 +86,7 @@ namespace BlacksmithWorkshopFileImplement.Models public OrderViewModel GetViewModel => new() { Id = Id, + ClientId = ClientId, ManufactureId = ManufactureId, Count = Count, Sum = Sum, @@ -90,9 +95,9 @@ namespace BlacksmithWorkshopFileImplement.Models DateImplement = DateImplement }; - public XElement GetXElement => new("Order", new XAttribute("Id", Id), + new XElement("ClientId", ClientId.ToString()), new XElement("ManufactureId", ManufactureId.ToString()), new XElement("Count", Count.ToString()), new XElement("Sum", Sum.ToString()), diff --git a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/ClientStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/ClientStorage.cs new file mode 100644 index 0000000..04f0bdd --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/ClientStorage.cs @@ -0,0 +1,130 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopContracts.StoragesContracts; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopListImplement.Implements +{ + public class ClientStorage : IClientStorage + { + //поле для работы со списком изделий + private readonly DataListSingleton _source; + + public ClientStorage() + { + _source = DataListSingleton.GetInstance(); + } + + public List GetFullList() + { + var result = new List(); + + foreach (var client in _source.Clients) + { + result.Add(client.GetViewModel); + } + + return result; + } + + public List GetFilteredList(ClientSearchModel model) + { + var result = new List(); + + if (string.IsNullOrEmpty(model.Email)) + { + return result; + } + + foreach (var client in _source.Clients) + { + if (client.Email.Contains(model.Email)) + { + result.Add(client.GetViewModel); + } + } + + return result; + } + + public ClientViewModel? GetElement(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.Email) && !model.Id.HasValue) + { + return null; + } + + foreach (var client in _source.Clients) + { + if ((!string.IsNullOrEmpty(model.Email) && client.Email == model.Email) || + (model.Id.HasValue && client.Id == model.Id)) + { + return client.GetViewModel; + } + } + + return null; + } + + public ClientViewModel? Insert(ClientBindingModel model) + { + model.Id = 1; + + foreach (var client in _source.Clients) + { + if (model.Id <= client.Id) + { + model.Id = client.Id + 1; + } + } + + var newClient = Client.Create(model); + + if (newClient == null) + { + return null; + } + + _source.Clients.Add(newClient); + + return newClient.GetViewModel; + } + + public ClientViewModel? Update(ClientBindingModel model) + { + foreach (var client in _source.Clients) + { + if (client.Id == model.Id) + { + client.Update(model); + + return client.GetViewModel; + } + } + + return null; + } + + public ClientViewModel? Delete(ClientBindingModel model) + { + for (int i = 0; i < _source.Clients.Count; ++i) + { + if (_source.Clients[i].Id == model.Id) + { + var element = _source.Clients[i]; + _source.Clients.RemoveAt(i); + + return element.GetViewModel; + } + } + + return null; + } + } +} \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/OrderStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/OrderStorage.cs index a69eac6..63ccc7d 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/OrderStorage.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/OrderStorage.cs @@ -39,22 +39,46 @@ namespace BlacksmithWorkshopListImplement.Implements //получение отфильтрованного списка заказов public List GetFilteredList(OrderSearchModel model) { - var result = new List(); - - if (!model.Id.HasValue) + if (!model.Id.HasValue && model.DateFrom.HasValue && model.DateTo.HasValue) { + var result = new List(); + + foreach (var order in _source.Orders) + { + if (order.DateCreate >= model.DateFrom && order.DateCreate <= model.DateTo) + { + result.Add(GetViewModel(order)); + } + } + + return result; + } + else if (model.Id.HasValue) + { + foreach (var order in _source.Orders) + { + if (order.Id == model.Id) + { + return new() { GetViewModel(order) }; + } + } + } + else if (model.ClientId.HasValue) + { + var result = new List(); + + foreach (var order in _source.Orders) + { + if (order.ClientId == model.ClientId) + { + result.Add(GetViewModel(order)); + } + } + return result; } - foreach (var order in _source.Orders) - { - if (order.Id == model.Id) - { - result.Add(GetViewModel(order)); - } - } - - return result; + return new(); } //получение элемента из списка заказов @@ -91,6 +115,15 @@ namespace BlacksmithWorkshopListImplement.Implements } } + foreach (var client in _source.Clients) + { + if (client.Id == order.ClientId) + { + viewModel.ClientFIO = client.ClientFIO; + break; + } + } + return viewModel; } diff --git a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Client.cs b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Client.cs new file mode 100644 index 0000000..2a4e4f9 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Client.cs @@ -0,0 +1,62 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopListImplement.Models +{ + public class Client : IClientModel + { + //методы set делаем приватным, чтобы исключить неразрешённые манипуляции + 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; + + //метод для создания объекта от класса-компонента на основе класса-BindingModel + public static Client? Create(ClientBindingModel? model) + { + if (model == null) + { + return null; + } + + return new Client() + { + 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; + } + + //метод для создания объекта класса ViewModel на основе данных объекта класса-компонента + public ClientViewModel GetViewModel => new() + { + Id = Id, + ClientFIO = ClientFIO, + Email = Email, + Password = Password + }; + } +} \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Order.cs b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Order.cs index 53d4b82..2cfc89c 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Order.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Order.cs @@ -17,6 +17,8 @@ namespace BlacksmithWorkshopListImplement.Models //методы set сделали приватными, чтобы исключить неразрешённые манипуляции public int Id { get; private set; } + public int ClientId { get; private set; } + public int ManufactureId { get; private set; } public int Count { get; private set; } @@ -40,6 +42,7 @@ namespace BlacksmithWorkshopListImplement.Models { Id = model.Id, ManufactureId = model.ManufactureId, + ClientId = model.ClientId, Count = model.Count, Sum = model.Sum, Status = model.Status, @@ -55,6 +58,7 @@ namespace BlacksmithWorkshopListImplement.Models { return; } + Status = model.Status; DateImplement = model.DateImplement; } @@ -64,6 +68,7 @@ namespace BlacksmithWorkshopListImplement.Models { Id = Id, ManufactureId = ManufactureId, + ClientId = ClientId, Count = Count, Sum = Sum, Status = Status, diff --git a/BlacksmithWorkshop/BlacksmithWorkshopView/ReportOrders.rdlc b/BlacksmithWorkshop/BlacksmithWorkshopView/ReportOrders.rdlc deleted file mode 100644 index a4779fa..0000000 --- a/BlacksmithWorkshop/BlacksmithWorkshopView/ReportOrders.rdlc +++ /dev/null @@ -1,53 +0,0 @@ - - - - - - true - true - - - - - Заказы - - - - Textbox1 - 0.95137cm - 16.51cm - - - Middle - 2pt - 2pt - 2pt - 2pt - - - - 2in -