diff --git a/FishFactoryBusinessLogic/BusinessLogic/ImplementerLogic.cs b/FishFactoryBusinessLogic/BusinessLogic/ImplementerLogic.cs new file mode 100644 index 0000000..618d327 --- /dev/null +++ b/FishFactoryBusinessLogic/BusinessLogic/ImplementerLogic.cs @@ -0,0 +1,115 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.BusinessLogicsContracts; +using FishFactoryContracts.SearchModels; +using FishFactoryContracts.StoragesContracts; +using FishFactoryContracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace FishFactoryBusinessLogic.BusinessLogic +{ + public class ImplementerLogic : IImplementerBusinessLogic + { + 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:{ImplementerFIO}. 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.WorkExperience)); + } + 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/FishFactoryContracts/BindingModels/ImplementerBindingModel.cs b/FishFactoryContracts/BindingModels/ImplementerBindingModel.cs new file mode 100644 index 0000000..9f5155f --- /dev/null +++ b/FishFactoryContracts/BindingModels/ImplementerBindingModel.cs @@ -0,0 +1,16 @@ +using FishFactoryDataModel.Models; + +namespace FishFactoryContracts.BindingModels +{ + public class ImplementerBindingModel : IImplementerModel + { + public int Id { get; set; } + public string ImplementerFIO { get; set; } = string.Empty; + + public string Password { get; set; } = string.Empty; + + public int WorkExperience { get; set; } + + public int Qualification { get; set; } +} +} diff --git a/FishFactoryContracts/BindingModels/OrderBindingModel.cs b/FishFactoryContracts/BindingModels/OrderBindingModel.cs index 773c201..f74e8d3 100644 --- a/FishFactoryContracts/BindingModels/OrderBindingModel.cs +++ b/FishFactoryContracts/BindingModels/OrderBindingModel.cs @@ -8,6 +8,7 @@ namespace FishFactoryContracts.BindingModels public int Id { get; set; } public int CannedId { get; set; } public int ClientId { get; set; } + public int ImplementerId { get; } public int Count { get; set; } public double Sum { get; set; } public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; diff --git a/FishFactoryContracts/BusinessLogicsContracts/IImplementerBusinessLogic.cs b/FishFactoryContracts/BusinessLogicsContracts/IImplementerBusinessLogic.cs new file mode 100644 index 0000000..ccb10eb --- /dev/null +++ b/FishFactoryContracts/BusinessLogicsContracts/IImplementerBusinessLogic.cs @@ -0,0 +1,15 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.SearchModels; +using FishFactoryContracts.ViewModels; + +namespace FishFactoryContracts.BusinessLogicsContracts +{ + public interface IImplementerBusinessLogic + { + List? ReadList(ImplementerSearchModel? model); + ImplementerViewModel? ReadElement(ImplementerSearchModel model); + bool Create(ImplementerBindingModel model); + bool Update(ImplementerBindingModel model); + bool Delete(ImplementerBindingModel model); + } +} diff --git a/FishFactoryContracts/SearchModels/ImplementerSearchModel.cs b/FishFactoryContracts/SearchModels/ImplementerSearchModel.cs new file mode 100644 index 0000000..be9c97f --- /dev/null +++ b/FishFactoryContracts/SearchModels/ImplementerSearchModel.cs @@ -0,0 +1,10 @@ + +namespace FishFactoryContracts.SearchModels +{ + public class ImplementerSearchModel + { + public int? Id { get; set; } + public string? ImplementerFIO { get; set; } + public string? Password { get; set; } + } +} diff --git a/FishFactoryContracts/StoragesContracts/IImplementerStorage.cs b/FishFactoryContracts/StoragesContracts/IImplementerStorage.cs new file mode 100644 index 0000000..1e9579b --- /dev/null +++ b/FishFactoryContracts/StoragesContracts/IImplementerStorage.cs @@ -0,0 +1,17 @@ + +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.SearchModels; +using FishFactoryContracts.ViewModels; + +namespace FishFactoryContracts.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/FishFactoryContracts/ViewModels/ImplementerViewModel.cs b/FishFactoryContracts/ViewModels/ImplementerViewModel.cs new file mode 100644 index 0000000..ceaae40 --- /dev/null +++ b/FishFactoryContracts/ViewModels/ImplementerViewModel.cs @@ -0,0 +1,18 @@ + +using System.ComponentModel; + +namespace FishFactoryContracts.ViewModels +{ + public class ImplementerViewModel + { + public int Id { get; set; } + [DisplayName("ФИО исполнителя")] + public string ImplementerFIO { get; set; } + [DisplayName("Пароль")] + public string Password { get; set; } + [DisplayName("Опыт работы")] + public int WorkExperience { get; set; } + [DisplayName("Квалификация")] + public int Qualification { get; set; } + } +} diff --git a/FishFactoryContracts/ViewModels/OrderViewModel.cs b/FishFactoryContracts/ViewModels/OrderViewModel.cs index 6594c26..03c9099 100644 --- a/FishFactoryContracts/ViewModels/OrderViewModel.cs +++ b/FishFactoryContracts/ViewModels/OrderViewModel.cs @@ -15,7 +15,10 @@ namespace FishFactoryContracts.ViewModels public int ClientId { get; set; } [DisplayName("Клиент")] public string ClientFIO { get; set; } = string.Empty; - public int CannedId { get; set; } + public int ImplementerId { get; set; } + [DisplayName("Исполнитель")] + public string ImplementerFIO { get; set; } = string.Empty; + public int CannedId { get; set; } [DisplayName("Изделие")] public string CannedName { get; set; } = string.Empty; [DisplayName("Количество")] diff --git a/FishFactoryDataModels/Models/IImplementerModel.cs b/FishFactoryDataModels/Models/IImplementerModel.cs new file mode 100644 index 0000000..396886f --- /dev/null +++ b/FishFactoryDataModels/Models/IImplementerModel.cs @@ -0,0 +1,10 @@ +namespace FishFactoryDataModel.Models +{ + public interface IImplementerModel + { + string ImplementerFIO { get; } + string Password { get; } + int WorkExperience { get; } + int Qualification { get; } + } +} diff --git a/FishFactoryDataModels/Models/IOrderModel.cs b/FishFactoryDataModels/Models/IOrderModel.cs index 2d545bf..d95bdff 100644 --- a/FishFactoryDataModels/Models/IOrderModel.cs +++ b/FishFactoryDataModels/Models/IOrderModel.cs @@ -6,6 +6,7 @@ namespace FishFactoryDataModel.Models { int CannedId { get; } int ClientId { get; } + int ImplementerId { get; } int Count { get; } double Sum { get; } OrderStatus Status { get; } diff --git a/FishFactoryDatabaseImplement/FishFactoryDatabase.cs b/FishFactoryDatabaseImplement/FishFactoryDatabase.cs index c75f78e..359e212 100644 --- a/FishFactoryDatabaseImplement/FishFactoryDatabase.cs +++ b/FishFactoryDatabaseImplement/FishFactoryDatabase.cs @@ -20,5 +20,6 @@ namespace FishFactoryDatabaseImplement public virtual DbSet CannedComponents { set; get; } public virtual DbSet Clients { set; get; } public virtual DbSet Orders { set; get; } + public virtual DbSet Implementers { set; get; } } } diff --git a/FishFactoryDatabaseImplement/Implements/ImplementerStorage.cs b/FishFactoryDatabaseImplement/Implements/ImplementerStorage.cs new file mode 100644 index 0000000..285dac7 --- /dev/null +++ b/FishFactoryDatabaseImplement/Implements/ImplementerStorage.cs @@ -0,0 +1,79 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.SearchModels; +using FishFactoryContracts.StoragesContracts; +using FishFactoryContracts.ViewModels; +using FishFactoryDatabaseImplement.Models; + +namespace FishFactoryDatabaseImplement.Implements +{ + public class ImplementerStorage : IImplementerStorage + { + public List GetFullList() + { + using var context = new FishFactoryDatabase(); + return context.Implementers.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(ImplementerSearchModel model) + { + if (string.IsNullOrEmpty(model.ImplementerFIO)) + { + return new(); + } + using var context = new FishFactoryDatabase(); + return context.Implementers.Where(x => x.ImplementerFIO.Contains(model.ImplementerFIO)).Select(x => x.GetViewModel).ToList(); + } + + public ImplementerViewModel? GetElement(ImplementerSearchModel model) + { + if (string.IsNullOrEmpty(model.ImplementerFIO) && !model.Id.HasValue) + { + return null; + } + using var context = new FishFactoryDatabase(); + return context.Implementers.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.ImplementerFIO) && x.ImplementerFIO == model.ImplementerFIO && (!string.IsNullOrEmpty(model.Password) ? x.Password == model.Password : true)) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public ImplementerViewModel? Insert(ImplementerBindingModel model) + { + var newImplementer = Implementer.Create(model); + if (newImplementer == null) + { + return null; + } + using var context = new FishFactoryDatabase(); + context.Implementers.Add(newImplementer); + context.SaveChanges(); + return newImplementer.GetViewModel; + } + + public ImplementerViewModel? Update(ImplementerBindingModel model) + { + using var context = new FishFactoryDatabase(); + var implementer = context.Implementers.FirstOrDefault(x => x.Id == model.Id); + if (implementer == null) + { + return null; + } + implementer.Update(model); + context.SaveChanges(); + return implementer.GetViewModel; + } + + public ImplementerViewModel? Delete(ImplementerBindingModel model) + { + using var context = new FishFactoryDatabase(); + var implementer = context.Implementers.FirstOrDefault(rec => rec.Id == model.Id); + if (implementer != null) + { + context.Implementers.Remove(implementer); + context.SaveChanges(); + return implementer.GetViewModel; + } + return null; + } + } +} diff --git a/FishFactoryDatabaseImplement/Implements/OrderStorage.cs b/FishFactoryDatabaseImplement/Implements/OrderStorage.cs index bcefbfc..dec4a6a 100644 --- a/FishFactoryDatabaseImplement/Implements/OrderStorage.cs +++ b/FishFactoryDatabaseImplement/Implements/OrderStorage.cs @@ -15,6 +15,7 @@ namespace FishFactoryDatabaseImplement.Implements return context.Orders .Include(x => x.Canned) .Include(x => x.Client) + .Include(x => x.Implementer) .Select(x => x.GetViewModel) .ToList(); } @@ -24,6 +25,7 @@ namespace FishFactoryDatabaseImplement.Implements return context.Orders .Include(x => x.Canned) .Include(x => x.Client) + .Include(x => x.Implementer) .Where(x => (!model.Id.HasValue || x.Id == model.Id) && (!model.DateFrom.HasValue || x.DateCreate >= model.DateFrom) && @@ -42,6 +44,7 @@ namespace FishFactoryDatabaseImplement.Implements return context.Orders .Include(x => x.Canned) .Include(x => x.Client) + .Include(x => x.Implementer) .FirstOrDefault(x => x.Id == model.Id) ?.GetViewModel; } @@ -68,6 +71,7 @@ namespace FishFactoryDatabaseImplement.Implements var order = context.Orders .Include(o => o.Canned) .Include(o => o.Client) + .Include(x => x.Implementer) .FirstOrDefault(o => o.Id == model.Id); if (order == null) { @@ -90,6 +94,7 @@ namespace FishFactoryDatabaseImplement.Implements var element = context.Orders .Include(o => o.Canned) .Include(o => o.Client) + .Include(x => x.Implementer) .FirstOrDefault(o => o.Id == model.Id); if (element != null) { diff --git a/FishFactoryDatabaseImplement/Models/Implementer.cs b/FishFactoryDatabaseImplement/Models/Implementer.cs new file mode 100644 index 0000000..55dde5e --- /dev/null +++ b/FishFactoryDatabaseImplement/Models/Implementer.cs @@ -0,0 +1,59 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.ViewModels; +using FishFactoryDataModel.Models; +using System.ComponentModel.DataAnnotations.Schema; + +namespace FishFactoryDatabaseImplement.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 Order { 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, + ImplementerFIO = ImplementerFIO, + Password = Password, + Qualification = Qualification, + ImplementerFIO = ImplementerFIO, + }; + } +} diff --git a/FishFactoryDatabaseImplement/Models/Order.cs b/FishFactoryDatabaseImplement/Models/Order.cs index 234fd27..495aefe 100644 --- a/FishFactoryDatabaseImplement/Models/Order.cs +++ b/FishFactoryDatabaseImplement/Models/Order.cs @@ -18,7 +18,11 @@ namespace FishFactoryDatabaseImplement.Models public int ClientId { get; private set; } public virtual Client Client { get; set; } = new(); - [Required] + [Required] + public int ImplementerId { get; private set; } + public virtual Implementer Implementer { get; set; } = new(); + + [Required] public int Count { get; private set; } [Required] public double Sum { get; private set; } @@ -36,7 +40,9 @@ namespace FishFactoryDatabaseImplement.Models Canned = context.Canneds.First(x => x.Id == model.CannedId), ClientId = model.ClientId, Client = context.Clients.First(x => x.Id == model.ClientId), - Count = model.Count, + ImplementerId = model.ImplementerId, + Implementer = context.Implementers.First(x => x.Id == model.ImplementerId), + Count = model.Count, Sum = model.Sum, Status = model.Status, DateCreate = model.DateCreate, @@ -51,6 +57,8 @@ namespace FishFactoryDatabaseImplement.Models } Id = model.Id; CannedId = model.CannedId; + ClientId = model.ClientId; + ImplementerId = model.ImplementerId; Sum = model.Sum; Status = model.Status; DateCreate = model.DateCreate; @@ -63,6 +71,8 @@ namespace FishFactoryDatabaseImplement.Models CannedName = Canned.CannedName, ClientId = ClientId, ClientFIO = Client.ClientFIO, + ImplementerId = ImplementerId, + ImplementerFIO = Implementer.ImplementerFIO, Count = Count, Sum = Sum, Status = Status, diff --git a/FishFactoryFileImplement/DataFileSingleton.cs b/FishFactoryFileImplement/DataFileSingleton.cs index 94e30ec..0afb482 100644 --- a/FishFactoryFileImplement/DataFileSingleton.cs +++ b/FishFactoryFileImplement/DataFileSingleton.cs @@ -11,10 +11,12 @@ namespace FishFactoryFileImplement private readonly string OrderFileName = "Order.xml"; private readonly string CannedFileName = "Canned.xml"; private readonly string ClientFileName = "Client.xml"; + private readonly string ImplementerFileName = "Implementer.xml"; public List Components { get; private set; } public List Orders { get; private set; } public List Canneds { get; private set; } public List Clients { get; private set; } + public List Implementers { get; private set; } public static DataFileSingleton GetInstance() { if (instance == null) @@ -27,12 +29,14 @@ namespace FishFactoryFileImplement public void SaveCanneds() => SaveData(Canneds, CannedFileName, "Canneds", x => x.GetXElement); public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); public void SaveClients() => SaveData(Clients, ClientFileName, "Clients", x => x.GetXElement); + public void SaveImplementers() => SaveData(Implementers, ImplementerFileName, "Implementer", x => x.GetXElement); private DataFileSingleton() { Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; Canneds = LoadData(CannedFileName, "Canned", x => Canned.Create(x)!)!; Orders = LoadData(OrderFileName, "Order", x => 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/FishFactoryFileImplement/FishFactoryFileImplement.csproj b/FishFactoryFileImplement/FishFactoryFileImplement.csproj index 2fba834..1cbc85b 100644 --- a/FishFactoryFileImplement/FishFactoryFileImplement.csproj +++ b/FishFactoryFileImplement/FishFactoryFileImplement.csproj @@ -11,4 +11,4 @@ - + \ No newline at end of file diff --git a/FishFactoryFileImplement/Implements/ImplementerStorage.cs b/FishFactoryFileImplement/Implements/ImplementerStorage.cs new file mode 100644 index 0000000..3352213 --- /dev/null +++ b/FishFactoryFileImplement/Implements/ImplementerStorage.cs @@ -0,0 +1,118 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.SearchModels; +using FishFactoryContracts.StoragesContracts; +using FishFactoryContracts.ViewModels; +using FishFactoryFileImplement.Models; + +namespace FishFactoryFileImplement.Implements +{ + public class ImplementerStorage : IImplementerStorage + { + private readonly DataFileSingleton _source; + public ImplementerStorage() + { + _source = DataFileSingleton.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) + { + 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 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 && x.ImplementerFIO.Equals(model.ImplementerFIO) && + model.Password != null && x.Password.Equals(model.Password)) + { + return x.GetViewModel; + } + if (model.ImplementerFIO != null && x.ImplementerFIO.Equals(model.ImplementerFIO)) + { + return x.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; + } + + 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; + } + } +} diff --git a/FishFactoryFileImplement/Models/Implementer.cs b/FishFactoryFileImplement/Models/Implementer.cs new file mode 100644 index 0000000..9469378 --- /dev/null +++ b/FishFactoryFileImplement/Models/Implementer.cs @@ -0,0 +1,65 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.ViewModels; +using FishFactoryDataModel.Models; +using System.Xml.Linq; + +namespace FishFactoryFileImplement.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, + ImplementerFIO = ImplementerFIO, + Qualification = Qualification, + ImplementerFIO = ImplementerFIO, + }; + + public XElement GetXElement => new("Implementer", + new XAttribute("Id", Id), + new XElement("Password", Password), + new XElement("ImplementerFIO", ImplementerFIO), + new XElement("Qualification", Qualification), + new XElement("WorkExperience", WorkExperience) + ); + } +} diff --git a/FishFactoryFileImplement/Models/Order.cs b/FishFactoryFileImplement/Models/Order.cs index 0eb4367..e7aa048 100644 --- a/FishFactoryFileImplement/Models/Order.cs +++ b/FishFactoryFileImplement/Models/Order.cs @@ -11,7 +11,8 @@ namespace FishFactoryFileImplement.Models public int Id { get; private set; } public int CannedId { get; private set; } public int ClientId { get; private 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; } public DateTime DateCreate { get; private set; } @@ -25,8 +26,9 @@ namespace FishFactoryFileImplement.Models return new Order() { Id = Convert.ToInt32(element.Attribute("Id")!.Value), - ClientId = Convert.ToInt32(element.Element("ClientId")!.Value), - CannedId = Convert.ToInt32(element.Element("CannedId")!.Value), + CannedId = Convert.ToInt32(element.Element("CannedId")!.Value), + ClientId = Convert.ToInt32(element.Element("ClientId")!.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), @@ -43,8 +45,9 @@ namespace FishFactoryFileImplement.Models return new Order() { Id = model.Id, - ClientId = model.ClientId, - CannedId = model.CannedId, + CannedId = model.CannedId, + ClientId = model.ClientId, + ImplementerId = model.ImplementerId, Count = model.Count, Sum = model.Sum, Status = model.Status, @@ -59,7 +62,8 @@ namespace FishFactoryFileImplement.Models return; } CannedId = model.CannedId; - ClientId = ClientId; + ClientId = model.ClientId; + ImplementerId = model.ImplementerId; Count = model.Count; Sum = model.Sum; Status = model.Status; @@ -71,7 +75,8 @@ namespace FishFactoryFileImplement.Models Id = Id, CannedId = CannedId, ClientId = ClientId, - Count = Count, + ImplementerId = ImplementerId, + Count = Count, Sum = Sum, Status = Status, DateCreate = DateCreate, @@ -79,8 +84,9 @@ namespace FishFactoryFileImplement.Models }; public XElement GetXElement => new("Order", new XAttribute("Id", Id), + new XElement("CannedId", CannedId.ToString()), new XElement("ClientId", ClientId.ToString()), - new XElement("CannedId", CannedId), + new XElement("ImplementerId", ImplementerId.ToString()), new XElement("Count", Count.ToString()), new XElement("Sum", Sum.ToString()), new XElement("Status", Status.ToString()), diff --git a/FishFactoryListImplement/DataListSingleton.cs b/FishFactoryListImplement/DataListSingleton.cs index 7595546..1f883f4 100644 --- a/FishFactoryListImplement/DataListSingleton.cs +++ b/FishFactoryListImplement/DataListSingleton.cs @@ -9,13 +9,15 @@ namespace FishFactoryListImplement public List Orders { get; set; } public List Canneds { get; set; } public List Clients { get; set; } - private DataListSingleton() + public List Implementers { get; set; } + private DataListSingleton() { Components = new List(); Orders = new List(); Canneds = new List(); Clients = new List(); - } + Implementers = new List(); + } public static DataListSingleton GetInstance() { if (_instance == null) diff --git a/FishFactoryListImplement/Implements/ImplementerStorage.cs b/FishFactoryListImplement/Implements/ImplementerStorage.cs new file mode 100644 index 0000000..f93448e --- /dev/null +++ b/FishFactoryListImplement/Implements/ImplementerStorage.cs @@ -0,0 +1,119 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.SearchModels; +using FishFactoryContracts.StoragesContracts; +using FishFactoryContracts.ViewModels; +using FishFactoryListImplement.Models; + +namespace FishFactoryListImplement.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 && x.ImplementerFIO.Equals(model.ImplementerFIO) && + model.Password != null && 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; + } + } +} diff --git a/FishFactoryListImplement/Models/Implementer.cs b/FishFactoryListImplement/Models/Implementer.cs new file mode 100644 index 0000000..4530cce --- /dev/null +++ b/FishFactoryListImplement/Models/Implementer.cs @@ -0,0 +1,56 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.ViewModels; +using FishFactoryDataModel.Models; + +namespace FishFactoryListImplement.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, + ImplementerFIO = ImplementerFIO, + Qualification = Qualification, + ImplementerFIO = ImplementerFIO, + }; + } +} diff --git a/FishFactoryListImplement/Models/Order.cs b/FishFactoryListImplement/Models/Order.cs index ade6f6b..6b31f26 100644 --- a/FishFactoryListImplement/Models/Order.cs +++ b/FishFactoryListImplement/Models/Order.cs @@ -2,12 +2,7 @@ using FishFactoryContracts.ViewModels; using FishFactoryDataModel.Enums; using FishFactoryDataModel.Models; -using System; -using System.Collections.Generic; -using System.Linq; using System.Reflection; -using System.Text; -using System.Threading.Tasks; namespace FishFactoryListImplement.Models { @@ -16,7 +11,8 @@ namespace FishFactoryListImplement.Models public int Id { get; private set; } public int CannedId { 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; } @@ -32,7 +28,8 @@ namespace FishFactoryListImplement.Models Id = model.Id, CannedId = model.CannedId, ClientId = model.ClientId, - Count = model.Count, + ImplementerId = model.ImplementerId, + Count = model.Count, Sum = model.Sum, Status = model.Status, DateCreate = model.DateCreate, @@ -58,7 +55,8 @@ namespace FishFactoryListImplement.Models Id = Id, CannedId = CannedId, ClientId = ClientId, - Count = Count, + ImplementerId = model.ImplementerId, + Count = Count, Sum = Sum, Status = Status, DateCreate = DateCreate,