diff --git a/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogic/ClientLogic.cs b/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogic/ClientLogic.cs index 5dab6ae..1433f02 100644 --- a/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogic/ClientLogic.cs +++ b/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogic/ClientLogic.cs @@ -18,10 +18,10 @@ namespace IceCreamShopBusinessLogic.BusinessLogic private readonly ILogger _logger; private readonly IClientStorage _clientStorage; public ClientLogic(ILogger logger, IClientStorage - componentStorage) + clientStorage) { _logger = logger; - _clientStorage = componentStorage; + _clientStorage = clientStorage; } public List? ReadList(ClientSearchModel? model) { diff --git a/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogic/ImplementerLogic.cs b/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogic/ImplementerLogic.cs new file mode 100644 index 0000000..a9d4d15 --- /dev/null +++ b/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogic/ImplementerLogic.cs @@ -0,0 +1,128 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.BusinessLogicsContracts; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.StoragesContracts; +using IceCreamShopContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopBusinessLogic.BusinessLogic +{ + public class ImplementerLogic : IImplementerLogic + { + private readonly ILogger _logger; + private readonly IImplementerStorage _implementerStorage; + public ImplementerLogic(ILogger logger, IImplementerStorage + implementerStorage) + { + _logger = logger; + _implementerStorage = implementerStorage; + } + public List? ReadList(ImplementerSearchModel? model) + { + _logger.LogInformation("ReadList. ImplementerFIO:{ClientFIO}. 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 ImplementerViewModel? ReadElement(ImplementerSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. ImplementerFIO:{ImplementerFIO}.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 bool Create(ImplementerBindingModel model) + { + CheckModel(model); + if (_implementerStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + public bool Update(ImplementerBindingModel model) + { + CheckModel(model); + if (_implementerStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(ImplementerBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_implementerStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + private void CheckModel(ImplementerBindingModel model, bool withParams = + true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.ImplementerFIO)) + { + throw new ArgumentNullException("Нет ФИО исполнителя", + nameof(model.ImplementerFIO)); + } + if (string.IsNullOrEmpty(model.Password)) + { + throw new ArgumentNullException("Нет пароля клиента", + nameof(model.Password)); + } + if (model.WorkExperience < 0) + { + throw new ArgumentNullException("Стаж меньше 0", + nameof(model.Password)); + } + if (model.Qualification < 0) + { + throw new ArgumentException("Квалификация меньше 0", nameof(model.Qualification)); + } + _logger.LogInformation("Implementer. ImplementerFIO:{ImplementerFIO}." + + "Password:{ Password}. WorkExperience:{ WorkExperience}. Qualification:{ Qualification}. Id: { Id} ", + model.ImplementerFIO, model.Password, model.WorkExperience, model.Qualification, model.Id); + var element = _implementerStorage.GetElement(new ImplementerSearchModel + { + ImplementerFIO = model.ImplementerFIO, + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Исполнитель с таким ФИО уже есть"); + } + } + } +} diff --git a/IceCreamShop/IceCreamShopContracts/BindingModels/ImplementerBindingModel.cs b/IceCreamShop/IceCreamShopContracts/BindingModels/ImplementerBindingModel.cs new file mode 100644 index 0000000..46e9e0e --- /dev/null +++ b/IceCreamShop/IceCreamShopContracts/BindingModels/ImplementerBindingModel.cs @@ -0,0 +1,18 @@ +using IceCreamShopDataModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopContracts.BindingModels +{ + public class ImplementerBindingModel : IImplementerModel + { + public int Id { get; set; } + public string ImplementerFIO { get; set; } = string.Empty; + public int WorkExperience { get; set; } = 0; + public int Qualification { get; set; } = 0; + public string Password { get; set; } = string.Empty; + } +} diff --git a/IceCreamShop/IceCreamShopContracts/BindingModels/OrderBindingModel.cs b/IceCreamShop/IceCreamShopContracts/BindingModels/OrderBindingModel.cs index 7c104ac..b301f05 100644 --- a/IceCreamShop/IceCreamShopContracts/BindingModels/OrderBindingModel.cs +++ b/IceCreamShop/IceCreamShopContracts/BindingModels/OrderBindingModel.cs @@ -13,7 +13,8 @@ namespace IceCreamShopContracts.BindingModels { public int Id { get; set; } public int IceCreamId { get; set; } - public int ClientId { get; set; } + public int ImplementerId { get; set; } + public int ClientId { get; set; } public int Count { get; set; } public double Sum { get; set; } public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; diff --git a/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IImplementerLogic.cs b/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IImplementerLogic.cs new file mode 100644 index 0000000..3a2bed9 --- /dev/null +++ b/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IImplementerLogic.cs @@ -0,0 +1,20 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopContracts.BusinessLogicsContracts +{ + public interface IImplementerLogic + { + List? ReadList(ImplementerSearchModel? model); + ImplementerViewModel? ReadElement(ImplementerSearchModel model); + bool Create(ImplementerBindingModel model); + bool Update(ImplementerBindingModel model); + bool Delete(ImplementerBindingModel model); + } +} diff --git a/IceCreamShop/IceCreamShopContracts/SearchModels/ImplementerSearchModel.cs b/IceCreamShop/IceCreamShopContracts/SearchModels/ImplementerSearchModel.cs new file mode 100644 index 0000000..8eec0fd --- /dev/null +++ b/IceCreamShop/IceCreamShopContracts/SearchModels/ImplementerSearchModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopContracts.SearchModels +{ + public class ImplementerSearchModel + { + public int? Id { get; set; } + public string? ImplementerFIO { get; set; } = string.Empty; + public string? Password { get; set; } = string.Empty; + } +} diff --git a/IceCreamShop/IceCreamShopContracts/StoragesContracts/IImplementerStorage.cs b/IceCreamShop/IceCreamShopContracts/StoragesContracts/IImplementerStorage.cs new file mode 100644 index 0000000..1517b5b --- /dev/null +++ b/IceCreamShop/IceCreamShopContracts/StoragesContracts/IImplementerStorage.cs @@ -0,0 +1,21 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopContracts.StoragesContracts +{ + public interface IImplementerStorage + { + List GetFullList(); + List GetFilteredList(ImplementerSearchModel model); + ImplementerViewModel? GetElement(ImplementerSearchModel model); + ImplementerViewModel? Insert(ImplementerBindingModel model); + ImplementerViewModel? Update(ImplementerBindingModel model); + ImplementerViewModel? Delete(ImplementerBindingModel model); + } +} diff --git a/IceCreamShop/IceCreamShopContracts/ViewModels/ImplementerViewModel.cs b/IceCreamShop/IceCreamShopContracts/ViewModels/ImplementerViewModel.cs new file mode 100644 index 0000000..4511e23 --- /dev/null +++ b/IceCreamShop/IceCreamShopContracts/ViewModels/ImplementerViewModel.cs @@ -0,0 +1,23 @@ +using IceCreamShopDataModels; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopContracts.ViewModels +{ + public class ImplementerViewModel : IImplementerModel + { + public int Id { get; set; } + [DisplayName("ФИО исполнителя")] + public string ImplementerFIO { get; set; } = string.Empty; + [DisplayName("Стаж работы")] + public int WorkExperience { get; set; } = 0; + [DisplayName("Квалификация")] + public int Qualification { get; set; } = 0; + [DisplayName("Пароль")] + public string Password { get; set; } = string.Empty; + } +} diff --git a/IceCreamShop/IceCreamShopContracts/ViewModels/OrderViewModel.cs b/IceCreamShop/IceCreamShopContracts/ViewModels/OrderViewModel.cs index 8f34065..e38f633 100644 --- a/IceCreamShop/IceCreamShopContracts/ViewModels/OrderViewModel.cs +++ b/IceCreamShop/IceCreamShopContracts/ViewModels/OrderViewModel.cs @@ -16,11 +16,14 @@ namespace IceCreamShopContracts.ViewModels public int Id { get; set; } public int IceCreamId { get; set; } public int ClientId { get; set; } + public int ImplementerId { get; set; } [DisplayName("Клиент")] public string ClientFIO { get; set; } = string.Empty; [DisplayName("Мороженное")] public string IceCreamName { get; set; } = string.Empty; - [DisplayName("Количество")] + [DisplayName("Исполнитель")] + public string ImplementerFIO { get; set; } = string.Empty; + [DisplayName("Количество")] public int Count { get; set; } [DisplayName("Сумма")] public double Sum { get; set; } diff --git a/IceCreamShop/IceCreamShopDataModels/IImplementerModel.cs b/IceCreamShop/IceCreamShopDataModels/IImplementerModel.cs new file mode 100644 index 0000000..8e94a01 --- /dev/null +++ b/IceCreamShop/IceCreamShopDataModels/IImplementerModel.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopDataModels +{ + public interface IImplementerModel + { + string ImplementerFIO { get; } + string Password { get; } + int WorkExperience { get; } + int Qualification { get; } + + } +} diff --git a/IceCreamShop/IceCreamShopDataModels/IOrderModel.cs b/IceCreamShop/IceCreamShopDataModels/IOrderModel.cs index b5d338e..862ec94 100644 --- a/IceCreamShop/IceCreamShopDataModels/IOrderModel.cs +++ b/IceCreamShop/IceCreamShopDataModels/IOrderModel.cs @@ -11,6 +11,7 @@ namespace IceCreamShopDataModels.Models { int IceCreamId { get; } int ClientId { get; } + int ImplementerId { get; } int Count { get; } double Sum { get; } OrderStatus Status { get; } diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDataBase.cs b/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDataBase.cs index 8cc2867..574d990 100644 --- a/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDataBase.cs +++ b/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDataBase.cs @@ -25,6 +25,7 @@ optionsBuilder) public virtual DbSet IceCreamComponents { set; get; } public virtual DbSet Orders { set; get; } public virtual DbSet Clients { set; get; } + public virtual DbSet Implementers { set; get; } - } + } } diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Implements/ImplementerStorage.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Implements/ImplementerStorage.cs new file mode 100644 index 0000000..6159573 --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Implements/ImplementerStorage.cs @@ -0,0 +1,87 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.StoragesContracts; +using IceCreamShopContracts.ViewModels; +using IceCreamShopDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopDatabaseImplement.Implements +{ + public class ImplementerStorage : IImplementerStorage + { + public ImplementerViewModel? Delete(ImplementerBindingModel model) + { + using var context = new IceCreamShopDataBase(); + 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) + { + if (string.IsNullOrEmpty(model.ImplementerFIO) && string.IsNullOrEmpty(model.Password) && + !model.Id.HasValue) + { + return null; + } + using var context = new IceCreamShopDataBase(); + return context.Implementers + .FirstOrDefault(x => (string.IsNullOrEmpty(model.ImplementerFIO) || x.ImplementerFIO == model.ImplementerFIO) && + (!model.Id.HasValue || x.Id == model.Id) && + (string.IsNullOrEmpty(model.Password) || x.Password == model.Password)) + ?.GetViewModel; + } + + public List GetFilteredList(ImplementerSearchModel model) + { + if (model == null) + { + return new(); + } + if (model.Id.HasValue) + { + var res = GetElement(model); + return res != null ? new() { res } : new(); + } + return new(); + } + + public List GetFullList() + { + using var context = new IceCreamShopDataBase(); + return context.Implementers.Select(x => x.GetViewModel).ToList(); + } + + public ImplementerViewModel? Insert(ImplementerBindingModel model) + { + using var context = new IceCreamShopDataBase(); + 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 IceCreamShopDataBase(); + var res = context.Implementers.FirstOrDefault(x => x.Id == model.Id); + if (res != null) + { + res.Update(model); + context.SaveChanges(); + } + return res?.GetViewModel; + } + } +} diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Models/Implementer.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Models/Implementer.cs new file mode 100644 index 0000000..4bebe27 --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Models/Implementer.cs @@ -0,0 +1,65 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.ViewModels; +using IceCreamShopDataModels; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopDatabaseImplement.Models +{ + public class Implementer : IImplementerModel + { + public int Id { get; private set; } + [Required] + public string ImplementerFIO { get; private set; } = string.Empty; + [Required] + public string Password { get; set; } = string.Empty; + [Required] + public int Qualification { get; set; } = 0; + [Required] + public int WorkExperience { get; set; } = 0; + [ForeignKey("ImplementerId")] + public virtual List Orders { get; 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, + }; + } +} diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Models/Order.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Models/Order.cs index 836bcb9..dab371c 100644 --- a/IceCreamShop/IceCreamShopDatabaseImplement/Models/Order.cs +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Models/Order.cs @@ -29,7 +29,10 @@ namespace IceCreamShopDatabaseImplement.Models [Required] public int ClientId { get; private set; } public virtual Client Client { get; private set; } - public static Order? Create(IceCreamShopDataBase context, OrderBindingModel model) + [Required] + public int ImplementerId { get; private set; } + public virtual Implementer Implementer { get; private set; } + public static Order? Create(IceCreamShopDataBase context, OrderBindingModel model) { if (model == null) { @@ -47,6 +50,8 @@ namespace IceCreamShopDatabaseImplement.Models IceCream = context.IceCreams.FirstOrDefault(x => x.Id == model.IceCreamId), ClientId = model.ClientId, Client = context.Clients.FirstOrDefault(x => x.Id == model.ClientId), + ImplementerId = model.ImplementerId, + Implementer = context.Implementers.FirstOrDefault(x => x.Id == model.ImplementerId), }; } @@ -64,6 +69,7 @@ namespace IceCreamShopDatabaseImplement.Models { IceCreamId = IceCreamId, ClientId = ClientId, + ImplementerId = ImplementerId, Count = Count, Sum = Sum, Status = Status, @@ -71,7 +77,8 @@ namespace IceCreamShopDatabaseImplement.Models DateImplement = DateImplement, IceCreamName = IceCream.IceCreamName, Id = Id, - ClientFIO = Client.ClientFIO + ClientFIO = Client.ClientFIO, + ImplementerFIO = Implementer.ImplementerFIO }; } diff --git a/IceCreamShop/IceCreamShopFileImplement/DataFileSingleton.cs b/IceCreamShop/IceCreamShopFileImplement/DataFileSingleton.cs index 79d9ba2..ca2cb52 100644 --- a/IceCreamShop/IceCreamShopFileImplement/DataFileSingleton.cs +++ b/IceCreamShop/IceCreamShopFileImplement/DataFileSingleton.cs @@ -15,12 +15,14 @@ namespace IceCreamShopFileImplement private readonly string OrderFileName = "Order.xml"; private readonly string IceCreamFileName = "IceCream.xml"; private readonly string ClientFileName = "Client.xml"; - public List Components { get; private set; } + private readonly string ImplementerFileName = "Implementer.xml"; + public List Components { get; private set; } public List Orders { get; private set; } public List IceCreams { get; private set; } public List Clients { get; private set; } + public List Implementers { get; private set; } - public static DataFileSingleton GetInstance() + public static DataFileSingleton GetInstance() { if (instance == null) { @@ -37,7 +39,9 @@ namespace IceCreamShopFileImplement "Orders", x => x.GetXElement); public void SaveClients() => SaveData(Clients, ClientFileName, "Clients", x => x.GetXElement); - private DataFileSingleton() + public void SaveImplementers() => SaveData(Implementers, ImplementerFileName, +"Implementers", x => x.GetXElement); + private DataFileSingleton() { Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; @@ -47,7 +51,9 @@ namespace IceCreamShopFileImplement Order.Create(x)!)!; 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/IceCreamShop/IceCreamShopFileImplement/Implements/ImplementerStorage.cs b/IceCreamShop/IceCreamShopFileImplement/Implements/ImplementerStorage.cs new file mode 100644 index 0000000..7478124 --- /dev/null +++ b/IceCreamShop/IceCreamShopFileImplement/Implements/ImplementerStorage.cs @@ -0,0 +1,83 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.StoragesContracts; +using IceCreamShopContracts.ViewModels; +using IceCreamShopFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopFileImplement.Implements +{ + public class ImplementerStorage : IImplementerStorage + { + private readonly DataFileSingleton source; + public ImplementerStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Implementers + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ImplementerSearchModel + model) + { + if (string.IsNullOrEmpty(model.ImplementerFIO) && string.IsNullOrEmpty(model.Password)) + { + return new(); + } + return source.Implementers + .Where(x => (string.IsNullOrEmpty(model.ImplementerFIO) || x.ImplementerFIO.Contains(model.ImplementerFIO)) && + (string.IsNullOrEmpty(model.Password) || x.Password.Contains(model.Password))) + .Select(x => x.GetViewModel) + .ToList(); + } + public ImplementerViewModel? GetElement(ImplementerSearchModel model) + { + return source.Implementers + .FirstOrDefault(x => (string.IsNullOrEmpty(model.ImplementerFIO) || x.ImplementerFIO == model.ImplementerFIO) && + (!model.Id.HasValue || x.Id == model.Id) && (string.IsNullOrEmpty(model.Password) || x.Password == model.Password)) + ?.GetViewModel; + } + public ImplementerViewModel? Insert(ImplementerBindingModel model) + { + model.Id = source.Implementers.Count > 0 ? source.Implementers.Max(x => + x.Id) + 1 : 1; + var newImplementer = Implementer.Create(model); + if (newImplementer == null) + { + return null; + } + source.Implementers.Add(newImplementer); + source.SaveImplementers(); + return newImplementer.GetViewModel; + } + public ImplementerViewModel? Update(ImplementerBindingModel model) + { + var implementer = source.Implementers.FirstOrDefault(x => x.Id == model.Id); + if (implementer == null) + { + return null; + } + implementer.Update(model); + source.SaveImplementers(); + return implementer.GetViewModel; + } + public ImplementerViewModel? Delete(ImplementerBindingModel model) + { + var element = source.Implementers.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + source.Implementers.Remove(element); + source.SaveImplementers(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/IceCreamShop/IceCreamShopFileImplement/Models/Implementer.cs b/IceCreamShop/IceCreamShopFileImplement/Models/Implementer.cs new file mode 100644 index 0000000..7c787b1 --- /dev/null +++ b/IceCreamShop/IceCreamShopFileImplement/Models/Implementer.cs @@ -0,0 +1,78 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.ViewModels; +using IceCreamShopDataModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace IceCreamShopFileImplement.Models +{ + public class Implementer : IImplementerModel + { + public int Id { get; private set; } + public string ImplementerFIO { get; private set; } = string.Empty; + public string Password { get; set; } = string.Empty; + public int Qualification { get; set; } = 0; + public int WorkExperience { get; set; } = 0; + public static Implementer? Create(ImplementerBindingModel model) + { + if (model == null) + { + return null; + } + return new Implementer() + { + Id = model.Id, + ImplementerFIO = model.ImplementerFIO, + Password = model.Password, + WorkExperience = model.WorkExperience, + Qualification = model.Qualification + }; + } + public void Update(ImplementerBindingModel model) + { + if (model == null) + { + return; + } + ImplementerFIO = model.ImplementerFIO; + Password = model.Password; + WorkExperience = model.WorkExperience; + Qualification = model.Qualification; + } + public ImplementerViewModel GetViewModel => new() + { + Id = Id, + ImplementerFIO = ImplementerFIO, + Password = Password, + WorkExperience = WorkExperience, + Qualification = Qualification + }; + public static Implementer? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Implementer() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ImplementerFIO = element.Element("ImplementerFIO")!.Value, + Password = element.Element("Password")!.Value, + Qualification = Convert.ToInt32(element.Element("Qualification")!.Value), + WorkExperience = Convert.ToInt32(element.Element("WorkExperience")!.Value) + }; + } + + public XElement GetXElement => new("Implementer", + new XAttribute("Id", Id), + new XElement("ImplementerFIO", ImplementerFIO), + new XElement("Password", Password), + new XElement("Qualification", Qualification.ToString()), + new XElement("WorkExperience", WorkExperience.ToString()) + ); + } +} diff --git a/IceCreamShop/IceCreamShopFileImplement/Models/Order.cs b/IceCreamShop/IceCreamShopFileImplement/Models/Order.cs index 799fcc1..1edf86c 100644 --- a/IceCreamShop/IceCreamShopFileImplement/Models/Order.cs +++ b/IceCreamShop/IceCreamShopFileImplement/Models/Order.cs @@ -15,7 +15,8 @@ namespace IceCreamShopFileImplement.Models { public int Id { get; private set; } public int IceCreamId { get; private set; } - public int ClientId { get; private set; } + public int ImplementerId { get; private set; } + public int ClientId { get; private set; } public int Count { get; private set; } public double Sum { get; private set; } public OrderStatus Status { get; private set; } @@ -39,6 +40,7 @@ namespace IceCreamShopFileImplement.Models Status = model.Status, DateCreate = model.DateCreate, DateImplement = model.DateImplement, + ImplementerId = model.ImplementerId, }; } public static Order? Create(XElement element) @@ -52,7 +54,8 @@ namespace IceCreamShopFileImplement.Models Id = Convert.ToInt32(element.Attribute("Id")!.Value), IceCreamId = Convert.ToInt32(element.Element("IceCreamId")!.Value), ClientId = Convert.ToInt32(element.Element("ClientId")!.Value), - Count = Convert.ToInt32(element.Element("Count")!.Value), + ImplementerId = Convert.ToInt32(element.Element("ImplementerId")!.Value), + Count = Convert.ToInt32(element.Element("Count")!.Value), Sum = Convert.ToDouble(element.Element("Sum")!.Value), Status = (OrderStatus)Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value.ToString()), DateCreate = Convert.ToDateTime(element.Element("DateCreate")!.Value), @@ -73,6 +76,7 @@ namespace IceCreamShopFileImplement.Models Id = Id, IceCreamId = IceCreamId, ClientId = ClientId, + ImplementerId = ImplementerId, Count = Count, Sum = Sum, Status = Status, diff --git a/IceCreamShop/IceCreamShopListImplement/DataListSingleton.cs b/IceCreamShop/IceCreamShopListImplement/DataListSingleton.cs index e7cb474..01b529f 100644 --- a/IceCreamShop/IceCreamShopListImplement/DataListSingleton.cs +++ b/IceCreamShop/IceCreamShopListImplement/DataListSingleton.cs @@ -14,13 +14,15 @@ namespace IceCreamShopListImplement public List Orders { get; set; } public List IceCreams { get; set; } public List Clients { get; set; } - private DataListSingleton() + public List Implementers { get; set; } + private DataListSingleton() { Components = new List(); Orders = new List(); IceCreams = new List(); Clients = new List(); - } + Implementers = new List(); + } public static DataListSingleton GetInstance() { if (_instance == null) diff --git a/IceCreamShop/IceCreamShopListImplement/Implements/ImplementerStorage.cs b/IceCreamShop/IceCreamShopListImplement/Implements/ImplementerStorage.cs new file mode 100644 index 0000000..755a799 --- /dev/null +++ b/IceCreamShop/IceCreamShopListImplement/Implements/ImplementerStorage.cs @@ -0,0 +1,103 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.StoragesContracts; +using IceCreamShopContracts.ViewModels; +using IceCreamShopListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopListImplement.Implements +{ + public class ImplementerStorage : IImplementerStorage + { + private readonly DataListSingleton _source; + public ImplementerStorage() + { + _source = DataListSingleton.GetInstance(); + } + public List GetFullList() + { + var result = new List(); + foreach (var implementer in _source.Implementers) + { + result.Add(implementer.GetViewModel); + } + return result; + } + public List GetFilteredList(ImplementerSearchModel + model) + { + var result = new List(); + if (string.IsNullOrEmpty(model.ImplementerFIO) && string.IsNullOrEmpty(model.Password)) + { + return result; + } + foreach (var implementer in _source.Implementers) + { + if (implementer.ImplementerFIO.Contains(model.ImplementerFIO)) + { + result.Add(implementer.GetViewModel); + } + } + return result; + } + public ImplementerViewModel? GetElement(ImplementerSearchModel model) + { + foreach (var implementer in _source.Implementers) + { + if ((string.IsNullOrEmpty(model.ImplementerFIO) || implementer.ImplementerFIO == model.ImplementerFIO) && + (!model.Id.HasValue || implementer.Id == model.Id) && (string.IsNullOrEmpty(model.Password) || implementer.Password == model.Password)) + { + return implementer.GetViewModel; + } + } + return null; + } + 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 newImplementer = Implementer.Create(model); + if (newImplementer == null) + { + return null; + } + _source.Implementers.Add(newImplementer); + return newImplementer.GetViewModel; + } + public ImplementerViewModel? Update(ImplementerBindingModel model) + { + foreach (var implementer in _source.Implementers) + { + if (model.Id == implementer.Id) + { + implementer.Update(model); + return implementer.GetViewModel; + } + } + return null; + } + public ImplementerViewModel? Delete(ImplementerBindingModel model) + { + for (int i = 0; i < _source.Implementers.Count; ++i) + { + if (_source.Implementers[i].Id == model.Id) + { + var element = _source.Implementers[i]; + _source.Implementers.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } + } +} diff --git a/IceCreamShop/IceCreamShopListImplement/Models/Implementer.cs b/IceCreamShop/IceCreamShopListImplement/Models/Implementer.cs new file mode 100644 index 0000000..d0de7bd --- /dev/null +++ b/IceCreamShop/IceCreamShopListImplement/Models/Implementer.cs @@ -0,0 +1,54 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.ViewModels; +using IceCreamShopDataModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopListImplement.Models +{ + public class Implementer : IImplementerModel + { + public int Id { get; private set; } + public string ImplementerFIO { get; private set; } = string.Empty; + public string Password { get; set; } = string.Empty; + public int WorkExperience { get; set; } = 0; + public int Qualification { get; set; } = 0; + public static Implementer? Create(ImplementerBindingModel model) + { + if (model == null) + { + return null; + } + return new Implementer() + { + Id = model.Id, + ImplementerFIO = model.ImplementerFIO, + Password = model.Password, + WorkExperience = model.WorkExperience, + Qualification = model.Qualification + }; + } + public void Update(ImplementerBindingModel model) + { + if (model == null) + { + return; + } + ImplementerFIO = model.ImplementerFIO; + Password = model.Password; + WorkExperience = model.WorkExperience; + Qualification = model.Qualification; + } + public ImplementerViewModel GetViewModel => new() + { + Id = Id, + ImplementerFIO = ImplementerFIO, + Password = Password, + WorkExperience = WorkExperience, + Qualification = Qualification + }; + } +} diff --git a/IceCreamShop/IceCreamShopListImplement/Models/Order.cs b/IceCreamShop/IceCreamShopListImplement/Models/Order.cs index 55b2134..73b77e8 100644 --- a/IceCreamShop/IceCreamShopListImplement/Models/Order.cs +++ b/IceCreamShop/IceCreamShopListImplement/Models/Order.cs @@ -18,7 +18,8 @@ namespace IceCreamShopListImplement.Models public int Id { get; private set; } public int IceCreamId { 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; } public DateTime DateCreate { get; private set; } @@ -40,6 +41,7 @@ namespace IceCreamShopListImplement.Models Status = model.Status, DateCreate =model.DateCreate, DateImplement = model.DateImplement, + ImplementerId = model.ImplementerId }; } public void Update(OrderBindingModel? model) @@ -56,12 +58,14 @@ namespace IceCreamShopListImplement.Models Status = model.Status; DateCreate = model.DateCreate; DateImplement = model.DateImplement; + ImplementerId = model.ImplementerId; } public OrderViewModel GetViewModel => new() { Id = Id, IceCreamId = IceCreamId, ClientId = ClientId, + ImplementerId = ImplementerId, Count = Count, Sum = Sum, Status = Status,