diff --git a/TravelCompany/TravelCompanyContracts/BindingModels/ImplementerBindingModel.cs b/TravelCompany/TravelCompanyContracts/BindingModels/ImplementerBindingModel.cs new file mode 100644 index 0000000..b40ab94 --- /dev/null +++ b/TravelCompany/TravelCompanyContracts/BindingModels/ImplementerBindingModel.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TravelCompanyDataModels.Models; + +namespace TravelCompanyContracts.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/TravelCompany/TravelCompanyContracts/BusinessLogicsContracts/IImplementerLogic.cs b/TravelCompany/TravelCompanyContracts/BusinessLogicsContracts/IImplementerLogic.cs new file mode 100644 index 0000000..484a955 --- /dev/null +++ b/TravelCompany/TravelCompanyContracts/BusinessLogicsContracts/IImplementerLogic.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TravelCompanyContracts.BindingModels; +using TravelCompanyContracts.SearchModels; +using TravelCompanyContracts.ViewModels; + +namespace TravelCompanyContracts.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/TravelCompany/TravelCompanyContracts/SearchModels/ImplementerSearchModel.cs b/TravelCompany/TravelCompanyContracts/SearchModels/ImplementerSearchModel.cs new file mode 100644 index 0000000..5d523c9 --- /dev/null +++ b/TravelCompany/TravelCompanyContracts/SearchModels/ImplementerSearchModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TravelCompanyContracts.SearchModels +{ + public class ImplementerSearchModel + { + public int? Id { get; set; } + public string? ImplementerFIO { get; set; } + public string? Password { get; set; } + } +} diff --git a/TravelCompany/TravelCompanyContracts/StoragesContracts/IImplementerStorage.cs b/TravelCompany/TravelCompanyContracts/StoragesContracts/IImplementerStorage.cs new file mode 100644 index 0000000..62f7c36 --- /dev/null +++ b/TravelCompany/TravelCompanyContracts/StoragesContracts/IImplementerStorage.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TravelCompanyContracts.BindingModels; +using TravelCompanyContracts.SearchModels; +using TravelCompanyContracts.ViewModels; + +namespace TravelCompanyContracts.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/TravelCompany/TravelCompanyContracts/ViewModels/ImplementerViewModel.cs b/TravelCompany/TravelCompanyContracts/ViewModels/ImplementerViewModel.cs new file mode 100644 index 0000000..1c11a25 --- /dev/null +++ b/TravelCompany/TravelCompanyContracts/ViewModels/ImplementerViewModel.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TravelCompanyDataModels.Models; + +namespace TravelCompanyContracts.ViewModels +{ + public class ImplementerViewModel : IImplementerModel + { + public int Id { get; set; } + [DisplayName("ФИО исполнителя")] + public string ImplementerFIO { get; set; } = string.Empty; + [DisplayName("Пароль")] + public string Password { get; set; } = string.Empty; + [DisplayName("Стаж работы")] + public int WorkExperience { get; set; } + [DisplayName("Квалификация")] + public int Qualification { get; set; } + } +} diff --git a/TravelCompany/TravelCompanyDataModels/Models/IImplementerModel.cs b/TravelCompany/TravelCompanyDataModels/Models/IImplementerModel.cs new file mode 100644 index 0000000..70e6039 --- /dev/null +++ b/TravelCompany/TravelCompanyDataModels/Models/IImplementerModel.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TravelCompanyDataModels.Models +{ + public interface IImplementerModel : IId + { + string ImplementerFIO { get; } + string Password { get; } + int WorkExperience { get; } + int Qualification { get; } + } +} diff --git a/TravelCompany/TravelCompanyDatabaseImplement/Implements/ImplementerStorage.cs b/TravelCompany/TravelCompanyDatabaseImplement/Implements/ImplementerStorage.cs new file mode 100644 index 0000000..b6a72e1 --- /dev/null +++ b/TravelCompany/TravelCompanyDatabaseImplement/Implements/ImplementerStorage.cs @@ -0,0 +1,104 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TravelCompanyContracts.BindingModels; +using TravelCompanyContracts.SearchModels; +using TravelCompanyContracts.StoragesContracts; +using TravelCompanyContracts.ViewModels; +using TravelCompanyDatabaseImplement.Models; + +namespace TravelCompanyDatabaseImplement.Implements +{ + public class ImplementerStorage : IImplementerStorage + { + public ImplementerViewModel? Delete(ImplementerBindingModel model) + { + using var context = new TravelCompanyDatabase(); + var element = context.Implementers.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Implementers.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public ImplementerViewModel? GetElement(ImplementerSearchModel model) + { + if (string.IsNullOrEmpty(model.ImplementerFIO) && + !model.Id.HasValue && string.IsNullOrEmpty(model.Password)) + { + return null; + } + using var context = new TravelCompanyDatabase(); + if (model.Id.HasValue) + { + return context.Implementers + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel; + } + if (model.ImplementerFIO != null && model.Password != null) + { + return context.Implementers + .FirstOrDefault(x => + x.ImplementerFIO.Equals(model.ImplementerFIO) && + x.Password.Equals(model.Password)) + ?.GetViewModel; + } + return context.Implementers + .FirstOrDefault(x => + x.ImplementerFIO.Equals(model.ImplementerFIO)) + ?.GetViewModel; + } + + public List GetFilteredList(ImplementerSearchModel model) + { + if (string.IsNullOrEmpty(model.ImplementerFIO)) + { + return new(); + } + using var context = new TravelCompanyDatabase(); + return context.Implementers + .Where(x => x.ImplementerFIO.Equals(model.ImplementerFIO)) + .Select(x => x.GetViewModel) + .ToList(); + + } + + public List GetFullList() + { + using var context = new TravelCompanyDatabase(); + return context.Implementers + .Select(x => x.GetViewModel) + .ToList(); + } + + public ImplementerViewModel? Insert(ImplementerBindingModel model) + { + var newImplementer = Implementer.Create(model); + if (newImplementer == null) + { + return null; + } + using var context = new TravelCompanyDatabase(); + context.Implementers.Add(newImplementer); + context.SaveChanges(); + return newImplementer.GetViewModel; + } + + public ImplementerViewModel? Update(ImplementerBindingModel model) + { + using var context = new TravelCompanyDatabase(); + var implementer = context.Implementers.FirstOrDefault(x => x.Id == model.Id); + if (implementer == null) + { + return null; + } + implementer.Update(model); + context.SaveChanges(); + return implementer.GetViewModel; + } + } +} diff --git a/TravelCompany/TravelCompanyDatabaseImplement/Models/Implementer.cs b/TravelCompany/TravelCompanyDatabaseImplement/Models/Implementer.cs new file mode 100644 index 0000000..1e7536f --- /dev/null +++ b/TravelCompany/TravelCompanyDatabaseImplement/Models/Implementer.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TravelCompanyContracts.BindingModels; +using TravelCompanyContracts.ViewModels; +using TravelCompanyDataModels.Models; + +namespace TravelCompanyDatabaseImplement.Models +{ + public class Implementer : IImplementerModel + { + [Required] + public string ImplementerFIO { get; private set; } = string.Empty; + [Required] + public string Password { get; private set; } = string.Empty; + [Required] + public int WorkExperience { get; private set; } + [Required] + public int Qualification { get; private set; } + public int Id { get; private set; } + + 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/TravelCompany/TravelCompanyDatabaseImplement/TravelCompanyDatabase.cs b/TravelCompany/TravelCompanyDatabaseImplement/TravelCompanyDatabase.cs index ac59a7d..b788f2a 100644 --- a/TravelCompany/TravelCompanyDatabaseImplement/TravelCompanyDatabase.cs +++ b/TravelCompany/TravelCompanyDatabaseImplement/TravelCompanyDatabase.cs @@ -24,5 +24,6 @@ namespace TravelCompanyDatabaseImplement public virtual DbSet TravelConditions { set; get; } public virtual DbSet Orders { set; get; } public virtual DbSet Clients { set; get; } + public virtual DbSet Implementers { set; get; } } } diff --git a/TravelCompany/TravelCompanyFileImplement/DataFileSingleton.cs b/TravelCompany/TravelCompanyFileImplement/DataFileSingleton.cs index 84d49ad..c01f4d3 100644 --- a/TravelCompany/TravelCompanyFileImplement/DataFileSingleton.cs +++ b/TravelCompany/TravelCompanyFileImplement/DataFileSingleton.cs @@ -16,11 +16,13 @@ namespace TravelCompanyFileImplement private readonly string OrderFileName = "Order.xml"; private readonly string ProductFileName = "Product.xml"; private readonly string ClientFileName = "Client.xml"; + private readonly string ImplementerFileName = "Implementer.xml"; public List Conditions { get; private set; } public List Orders { get; private set; } public List Travels { get; private set; } public List Clients { get; private set; } - public static DataFileSingleton GetInstance() + public List Implementers { get; private set; } + public static DataFileSingleton GetInstance() { if (instance == null) { @@ -32,12 +34,14 @@ namespace TravelCompanyFileImplement public void SaveTravels() => SaveData(Travels, ProductFileName, "Travels", 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, "Implementers", x => x.GetXElement); private DataFileSingleton() { Conditions = LoadData(ConditionFileName, "Condition", x => Condition.Create(x)!)!; Travels = LoadData(ProductFileName, "Travel", x => Travel.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/TravelCompany/TravelCompanyFileImplement/Implements/ImplementerStorage.cs b/TravelCompany/TravelCompanyFileImplement/Implements/ImplementerStorage.cs new file mode 100644 index 0000000..0ae758a --- /dev/null +++ b/TravelCompany/TravelCompanyFileImplement/Implements/ImplementerStorage.cs @@ -0,0 +1,103 @@ +using DocumentFormat.OpenXml.Bibliography; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TravelCompanyContracts.BindingModels; +using TravelCompanyContracts.SearchModels; +using TravelCompanyContracts.StoragesContracts; +using TravelCompanyContracts.ViewModels; +using TravelCompanyListImplement.Models; + +namespace TravelCompanyFileImplement.Implements +{ + public class ImplementerStorage : IImplementerStorage + { + private readonly DataFileSingleton _source; + public ImplementerStorage() + { + _source = DataFileSingleton.GetInstance(); + } + public ImplementerViewModel? Delete(ImplementerBindingModel model) + { + var element = _source.Implementers.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + _source.Implementers.Remove(element); + _source.SaveConditions(); + return element.GetViewModel; + } + return null; + } + + public ImplementerViewModel? GetElement(ImplementerSearchModel model) + { + if (string.IsNullOrEmpty(model.ImplementerFIO) && !model.Id.HasValue && + string.IsNullOrEmpty(model.Password)) + { + return null; + } + if (model.ImplementerFIO != null && model.Password != null) + { + return _source.Implementers + .FirstOrDefault(x => + x.ImplementerFIO.Equals(model.ImplementerFIO) && x.Password.Equals(model.Password)) + ?.GetViewModel; + } + if (model.Id.HasValue) + { + return _source.Implementers + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel; + } + return _source.Implementers + .FirstOrDefault(x => + x.ImplementerFIO.Equals(model.ImplementerFIO)) + ?.GetViewModel; + } + + public List GetFilteredList(ImplementerSearchModel model) + { + if (string.IsNullOrEmpty(model.ImplementerFIO)) + { + return new(); + } + return _source.Implementers + .Where(x => x.ImplementerFIO.Equals(model.ImplementerFIO)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + return _source.Implementers + .Select(x => x.GetViewModel) + .ToList(); + } + + public ImplementerViewModel? Insert(ImplementerBindingModel model) + { + model.Id = _source.Implementers.Count > 0 ? _source.Implementers.Max(x => x.Id) + 1 : 1; + var newImpl = Implementer.Create(model); + if (newImpl == null) + { + return null; + } + _source.Implementers.Add(newImpl); + _source.SaveConditions(); + return newImpl.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.SaveConditions(); + return implementer.GetViewModel; + } + } +} diff --git a/TravelCompany/TravelCompanyFileImplement/Models/Implementer.cs b/TravelCompany/TravelCompanyFileImplement/Models/Implementer.cs new file mode 100644 index 0000000..9ff6a92 --- /dev/null +++ b/TravelCompany/TravelCompanyFileImplement/Models/Implementer.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; +using TravelCompanyContracts.BindingModels; +using TravelCompanyContracts.ViewModels; +using TravelCompanyDataModels.Models; + +namespace TravelCompanyFileImplement.Models +{ + public class Implementer : IImplementerModel + { + 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 int Id { get; private set; } + + 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 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, + WorkExperience = Convert.ToInt32(element.Element("WorkExperience")!.Value), + Qualification = Convert.ToInt32(element.Element("Qualification")!.Value) + }; + } + 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 XElement GetXElement => new("Implementer", + new XAttribute("Id", Id), + new XElement("ImplementerFIO", ImplementerFIO), + new XElement("Password", Password), + new XElement("WorkExperience", WorkExperience.ToString()), + new XElement("Qualification", Qualification.ToString())); + } +} diff --git a/TravelCompany/TravelCompanyListImplement/DataListSingleton.cs b/TravelCompany/TravelCompanyListImplement/DataListSingleton.cs index 625d0fd..fbb882b 100644 --- a/TravelCompany/TravelCompanyListImplement/DataListSingleton.cs +++ b/TravelCompany/TravelCompanyListImplement/DataListSingleton.cs @@ -15,12 +15,14 @@ namespace TravelCompanyListImplement public List Orders { get; set; } public List Travels { get; set; } public List Clients { get; set; } + public List Implementers { get; set; } private DataListSingleton() { Conditions = new List(); Orders = new List(); Travels = new List(); Clients = new List(); + Implementers = new List(); } public static DataListSingleton GetInstance() { diff --git a/TravelCompany/TravelCompanyListImplement/Implements/ImplementerStorage.cs b/TravelCompany/TravelCompanyListImplement/Implements/ImplementerStorage.cs new file mode 100644 index 0000000..d970a45 --- /dev/null +++ b/TravelCompany/TravelCompanyListImplement/Implements/ImplementerStorage.cs @@ -0,0 +1,124 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TravelCompanyContracts.BindingModels; +using TravelCompanyContracts.SearchModels; +using TravelCompanyContracts.StoragesContracts; +using TravelCompanyContracts.ViewModels; + +namespace TravelCompanyListImplement.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) + { + if (string.IsNullOrEmpty(model.ImplementerFIO) && !model.Id.HasValue + && string.IsNullOrEmpty(model.Password)) + { + return null; + } + + foreach (var implementer in _source.Implementers) + { + if (model.Id.HasValue && implementer.Id == model.Id) + { + return implementer.GetViewModel; + } + + if (model.ImplementerFIO != null && model.Password != null + && implementer.ImplementerFIO.Equals(model.ImplementerFIO) && + implementer.Password.Equals(model.Password)) + { + return implementer.GetViewModel; + } + + if (model.ImplementerFIO != null && implementer.ImplementerFIO.Equals(model.ImplementerFIO)) + { + return implementer.GetViewModel; + } + } + return null; + } + + public List GetFilteredList(ImplementerSearchModel model) + { + var result = new List(); + if (string.IsNullOrEmpty(model.ImplementerFIO)) + { + return result; + } + foreach (var implementer in _source.Implementers) + { + if (implementer.ImplementerFIO.Contains(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 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 (implementer.Id == model.Id) + { + implementer.Update(model); + return implementer.GetViewModel; + } + } + return null; + } + } +} diff --git a/TravelCompany/TravelCompanyListImplement/Models/Implementer.cs b/TravelCompany/TravelCompanyListImplement/Models/Implementer.cs new file mode 100644 index 0000000..9c248d6 --- /dev/null +++ b/TravelCompany/TravelCompanyListImplement/Models/Implementer.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TravelCompanyContracts.BindingModels; +using TravelCompanyContracts.ViewModels; +using TravelCompanyDataModels.Models; + +namespace TravelCompanyListImplement.Models +{ + public class Implementer : IImplementerModel + { + 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 int Id { get; private set; } + + 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 + }; + } +}