diff --git a/TravelCompany/TravelCompany.sln b/TravelCompany/TravelCompany.sln index b8d7350..817e619 100644 --- a/TravelCompany/TravelCompany.sln +++ b/TravelCompany/TravelCompany.sln @@ -11,7 +11,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TravelCompanyContracts", "T EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TravelCompanyBusinessLogic", "TravelCompanyBusinessLogic\TravelCompanyBusinessLogic.csproj", "{A636CF3E-87F6-4C8B-99C6-8EA066516C0A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TravelCompanyListImplement", "TravelCompanyListImplement\TravelCompanyListImplement.csproj", "{0FF615FA-A053-4DF6-99E6-CFC50A1D88C9}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TravelCompanyListImplement", "TravelCompanyListImplement\TravelCompanyListImplement.csproj", "{0FF615FA-A053-4DF6-99E6-CFC50A1D88C9}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TravelCompanyFileImplement", "TravelCompanyFileImplement\TravelCompanyFileImplement.csproj", "{388A7F25-BA80-436B-85C2-479FFD9CD6F2}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -39,6 +41,10 @@ Global {0FF615FA-A053-4DF6-99E6-CFC50A1D88C9}.Debug|Any CPU.Build.0 = Debug|Any CPU {0FF615FA-A053-4DF6-99E6-CFC50A1D88C9}.Release|Any CPU.ActiveCfg = Release|Any CPU {0FF615FA-A053-4DF6-99E6-CFC50A1D88C9}.Release|Any CPU.Build.0 = Release|Any CPU + {388A7F25-BA80-436B-85C2-479FFD9CD6F2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {388A7F25-BA80-436B-85C2-479FFD9CD6F2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {388A7F25-BA80-436B-85C2-479FFD9CD6F2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {388A7F25-BA80-436B-85C2-479FFD9CD6F2}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/TravelCompany/TravelCompany/Program.cs b/TravelCompany/TravelCompany/Program.cs index 6e49f02..a355230 100644 --- a/TravelCompany/TravelCompany/Program.cs +++ b/TravelCompany/TravelCompany/Program.cs @@ -3,7 +3,7 @@ using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; using TravelCompanyContracts.BusinessLogicsContracts; using TravelCompanyContracts.StoragesContracts; -using TravelCompanyListImplement.Implements; +using TravelCompanyFileImplement.Implements; using TravelCompanyBusinessLogic.BusinessLogic; namespace TravelCompany diff --git a/TravelCompany/TravelCompany/TravelCompany.csproj b/TravelCompany/TravelCompany/TravelCompany.csproj index 2cfe538..1674708 100644 --- a/TravelCompany/TravelCompany/TravelCompany.csproj +++ b/TravelCompany/TravelCompany/TravelCompany.csproj @@ -29,6 +29,7 @@ + diff --git a/TravelCompany/TravelCompanyFileImplement/DataFileSingleton.cs b/TravelCompany/TravelCompanyFileImplement/DataFileSingleton.cs new file mode 100644 index 0000000..ee723c0 --- /dev/null +++ b/TravelCompany/TravelCompanyFileImplement/DataFileSingleton.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TravelCompanyFileImplement.Models; +using System.Xml.Linq; +using System.ComponentModel; + +namespace TravelCompanyFileImplement +{ + public class DataFileSingleton + { + private static DataFileSingleton? instance; + private readonly string ConditionFileName = "Condition.xml"; + private readonly string OrderFileName = "Order.xml"; + private readonly string ProductFileName = "Product.xml"; + public List Conditions { get; private set; } + public List Orders { get; private set; } + public List Travels { get; private set; } + public static DataFileSingleton GetInstance() + { + if (instance == null) + { + instance = new DataFileSingleton(); + } + return instance; + } + public void SaveConditions() => SaveData(Conditions, ConditionFileName,"Conditions", x => x.GetXElement); + public void SaveTravels() => SaveData(Travels, ProductFileName, "Travels", x => x.GetXElement); + public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", 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)!)!; + } + private static List? LoadData(string filename, string xmlNodeName, + Func selectFunction) + { + if (File.Exists(filename)) + { + return + XDocument.Load(filename)?.Root?.Elements(xmlNodeName)?.Select(selectFunction)?.ToList(); + } + return new List(); + } + private static void SaveData(List data, string filename, string xmlNodeName, Func selectFunction) + { + if (data != null) + { + new XDocument(new XElement(xmlNodeName, + data.Select(selectFunction).ToArray())).Save(filename); + } + } + } +} diff --git a/TravelCompany/TravelCompanyFileImplement/Implements/ConditionStorage.cs b/TravelCompany/TravelCompanyFileImplement/Implements/ConditionStorage.cs new file mode 100644 index 0000000..c132aab --- /dev/null +++ b/TravelCompany/TravelCompanyFileImplement/Implements/ConditionStorage.cs @@ -0,0 +1,85 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TravelCompanyContracts.StoragesContracts; +using TravelCompanyContracts.BindingModels; +using TravelCompanyContracts.SearchModels; +using TravelCompanyContracts.ViewModels; +using TravelCompanyFileImplement.Models; + + +namespace TravelCompanyFileImplement.Implements +{ + public class ConditionStorage : IConditionStorage + { + private readonly DataFileSingleton source; + public ConditionStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Conditions + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ConditionSearchModel model) + { + if (string.IsNullOrEmpty(model.ConditionName)) + { + return new(); + } + return source.Conditions + .Where(x => x.ConditionName.Contains(model.ConditionName)) + .Select(x => x.GetViewModel) + .ToList(); + } + public ConditionViewModel? GetElement(ConditionSearchModel model) + { + if (string.IsNullOrEmpty(model.ConditionName) && !model.Id.HasValue) + { + return null; + } + return source.Conditions.FirstOrDefault(x =>(!string.IsNullOrEmpty(model.ConditionName) && x.ConditionName ==model.ConditionName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + public ConditionViewModel? Insert(ConditionBindingModel model) + { + model.Id = source.Conditions.Count > 0 ? source.Conditions.Max(x =>x.Id) + 1 : 1; + var newCondition = Condition.Create(model); + if (newCondition == null) + { + return null; + } + source.Conditions.Add(newCondition); + source.SaveConditions(); + return newCondition.GetViewModel; + } + public ConditionViewModel? Update(ConditionBindingModel model) + { + var condition = source.Conditions.FirstOrDefault(x => x.Id ==model.Id); + if (condition == null) + { + return null; + } + condition.Update(model); + source.SaveConditions(); + return condition.GetViewModel; + } + public ConditionViewModel? Delete(ConditionBindingModel model) + { + var element = source.Conditions.FirstOrDefault(x => x.Id ==model.Id); + if (element != null) + { + source.Conditions.Remove(element); + source.SaveConditions(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/TravelCompany/TravelCompanyFileImplement/Implements/OrderStorage.cs b/TravelCompany/TravelCompanyFileImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..691eb0d --- /dev/null +++ b/TravelCompany/TravelCompanyFileImplement/Implements/OrderStorage.cs @@ -0,0 +1,93 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +using TravelCompanyContracts.BindingModels; +using TravelCompanyContracts.SearchModels; +using TravelCompanyContracts.StoragesContracts; +using TravelCompanyContracts.ViewModels; +using TravelCompanyFileImplement.Models; + + +namespace TravelCompanyFileImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + private readonly DataFileSingleton _source; + public OrderStorage() + { + _source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return _source.Orders + .Select(x => InsertTravelName(x.GetViewModel)) + .ToList(); + } + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + return _source.Orders + .Where(x => x.Id == model.Id) + .Select(x => InsertTravelName(x.GetViewModel)) + .ToList(); + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + return InsertTravelName(_source.Orders + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) + ?.GetViewModel); + } + public OrderViewModel? Insert(OrderBindingModel model) + { + model.Id = _source.Orders.Count > 0 ? _source.Orders.Max(x => x.Id) + 1 : 1; + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + _source.Orders.Add(newOrder); + _source.SaveOrders(); + return InsertTravelName(newOrder.GetViewModel); + } + public OrderViewModel? Update(OrderBindingModel model) + { + var order = _source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + _source.SaveOrders(); + return InsertTravelName(order.GetViewModel); + } + public OrderViewModel? Delete(OrderBindingModel model) + { + var element = _source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + _source.Orders.Remove(element); + _source.SaveOrders(); + return InsertTravelName(element.GetViewModel); + } + return null; + } + + public OrderViewModel InsertTravelName(OrderViewModel fullModel) + { + string? travelName = _source.Travels.FirstOrDefault(x => x.Id == fullModel?.TravelId)?.TravelName; + if (travelName != null) fullModel.TravelName = travelName; + return fullModel; + } + } +} diff --git a/TravelCompany/TravelCompanyFileImplement/Implements/TravelStorage.cs b/TravelCompany/TravelCompanyFileImplement/Implements/TravelStorage.cs new file mode 100644 index 0000000..b00f88e --- /dev/null +++ b/TravelCompany/TravelCompanyFileImplement/Implements/TravelStorage.cs @@ -0,0 +1,85 @@ +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 TravelCompanyFileImplement.Models; + +namespace TravelCompanyFileImplement.Implements +{ + public class TravelStorage : ITravelStorage + { + private readonly DataFileSingleton _source; + public TravelStorage() + { + _source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return _source.Travels + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(TravelSearchModel model) + { + if (string.IsNullOrEmpty(model.TravelName)) + { + return new(); + } + return _source.Travels + .Where(x => x.TravelName.Contains(model.TravelName)) + .Select(x => x.GetViewModel) + .ToList(); + } + public TravelViewModel? GetElement(TravelSearchModel model) + { + if (string.IsNullOrEmpty(model.TravelName) && !model.Id.HasValue) + { + return null; + } + return _source.Travels + .FirstOrDefault(x => (x.TravelName == model.TravelName && !string.IsNullOrEmpty(model.TravelName) ) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + + } + public TravelViewModel? Insert(TravelBindingModel model) + { + model.Id = _source.Travels.Count > 0 ? _source.Travels.Max(x => x.Id) + 1 : 1; + var newTravel = Travel.Create(model); + if (newTravel == null) + { + return null; + } + _source.Travels.Add(newTravel); + _source.SaveTravels(); + return newTravel.GetViewModel; + } + public TravelViewModel? Update(TravelBindingModel model) + { + var travel = _source.Travels.FirstOrDefault(x => x.Id == model.Id); + if (travel == null) + { + return null; + } + travel.Update(model); + _source.SaveTravels(); + return travel.GetViewModel; + } + public TravelViewModel? Delete(TravelBindingModel model) + { + var element = _source.Travels.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + _source.Travels.Remove(element); + _source.SaveTravels(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/TravelCompany/TravelCompanyFileImplement/Models/Condition.cs b/TravelCompany/TravelCompanyFileImplement/Models/Condition.cs new file mode 100644 index 0000000..c5944a1 --- /dev/null +++ b/TravelCompany/TravelCompanyFileImplement/Models/Condition.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TravelCompanyDataModels.Models; +using TravelCompanyContracts.BindingModels; +using TravelCompanyContracts.ViewModels; +using System.Xml.Linq; + + +namespace TravelCompanyFileImplement.Models +{ + public class Condition : IConditionModel + { + public int Id { get; private set; } + public string ConditionName { get; private set; } = string.Empty; + public double Cost { get; set; } + public static Condition? Create(ConditionBindingModel model) + { + if (model == null) + { + return null; + } + return new Condition() + { + Id = model.Id, + ConditionName = model.ConditionName, + Cost = model.Cost + }; + } + public static Condition? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Condition() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ConditionName = element.Element("ConditionName")!.Value, + Cost = Convert.ToDouble(element.Element("Cost")!.Value) + }; + } + public void Update(ConditionBindingModel model) + { + if (model == null) + { + return; + } + ConditionName = model.ConditionName; + Cost = model.Cost; + } + public ConditionViewModel GetViewModel => new() + { + Id = Id, + ConditionName = ConditionName, + Cost = Cost + }; + public XElement GetXElement => new("Condition", + new XAttribute("Id", Id), + new XElement("ConditionName", ConditionName), + new XElement("Cost", Cost.ToString())); + } +} + diff --git a/TravelCompany/TravelCompanyFileImplement/Models/Order.cs b/TravelCompany/TravelCompanyFileImplement/Models/Order.cs new file mode 100644 index 0000000..90a50ca --- /dev/null +++ b/TravelCompany/TravelCompanyFileImplement/Models/Order.cs @@ -0,0 +1,87 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; +using TravelCompanyContracts.BindingModels; +using TravelCompanyContracts.ViewModels; +using TravelCompanyDataModels.Enums; +using TravelCompanyDataModels.Models; + +namespace TravelCompanyFileImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + public int TravelId { get; set; } + public int Count { get; set; } + public double Sum { get; set; } + public OrderStatus Status { get; set; } + public DateTime DateCreate { get; set; } + public DateTime? DateImplement { get; set; } + public static Order? Create(OrderBindingModel model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + TravelId = model.TravelId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, + }; + } + public static Order? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Order() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + TravelId = Convert.ToInt32(element.Element("TravelId")!.Value), + Count = Convert.ToInt32(element.Element("Count")!.Value), + Sum = Convert.ToDouble(element.Element("Sum")!.Value), + Status = (OrderStatus) Convert.ToInt32(element.Element("Status")!.Value), + DateCreate = Convert.ToDateTime(element.Element("DateCreate")!.Value), + DateImplement = string.IsNullOrEmpty(element.Element("DateImplement")!.Value) ? null : Convert.ToDateTime(element.Element("DateImplement")!.Value) + }; + } + public void Update(OrderBindingModel model) + { + if (model == null) + { + return; + } + Status = model.Status; + if (model.DateImplement.HasValue) { DateImplement = model.DateImplement; } + } + public OrderViewModel GetViewModel => new() + { + Id = Id, + TravelId = TravelId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + }; + public XElement GetXElement => new("Order", + new XAttribute("Id", Id), + new XElement("TravelId", TravelId), + new XElement("Count", Count.ToString()), + new XElement("Sum", Sum.ToString()), + new XElement("Status", ((int)Status).ToString()), + new XElement("DateCreate", DateCreate.ToString()), + new XElement("DateImplement", DateImplement.ToString())); + } +} + diff --git a/TravelCompany/TravelCompanyFileImplement/Models/Travel.cs b/TravelCompany/TravelCompanyFileImplement/Models/Travel.cs new file mode 100644 index 0000000..4e1f059 --- /dev/null +++ b/TravelCompany/TravelCompanyFileImplement/Models/Travel.cs @@ -0,0 +1,93 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TravelCompanyDataModels.Models; +using TravelCompanyContracts.ViewModels; +using TravelCompanyContracts.BindingModels; +using System.Xml.Linq; + + +namespace TravelCompanyFileImplement.Models +{ + public class Travel : ITravelModel + { + public int Id { get; private set; } + public string TravelName { get; private set; } = string.Empty; + public double Price { get; private set; } + public Dictionary Conditions { get; private set; } = new(); + private Dictionary? _travelConditions = null; + public Dictionary TravelConditions + { + get + { + if (_travelConditions == null) + { + var source = DataFileSingleton.GetInstance(); + _travelConditions = Conditions.ToDictionary(x => x.Key, y => ((source.Conditions.FirstOrDefault(z => z.Id == y.Key) as IConditionModel)!,y.Value)); + } + return _travelConditions; + } + } + public static Travel? Create(TravelBindingModel model) + { + if (model == null) + { + return null; + } + return new Travel() + { + Id = model.Id, + TravelName = model.TravelName, + Price = model.Price, + Conditions = model.TravelConditions.ToDictionary(x => x.Key, x=> x.Value.Item2) + }; + } + public static Travel? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Travel() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + TravelName = element.Element("TravelName")!.Value, + Price = Convert.ToDouble(element.Element("Price")!.Value), + Conditions = element.Element("TravelConditions")!.Elements("TravelCondition").ToDictionary(x =>Convert.ToInt32(x.Element("Key")?.Value), x =>Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + public void Update(TravelBindingModel model) + { + if (model == null) + { + return; + } + TravelName = model.TravelName; + Price = model.Price; + Conditions = model.TravelConditions.ToDictionary(x => x.Key, x =>x.Value.Item2); + _travelConditions = null; + } + public TravelViewModel GetViewModel => new() + { + Id = Id, + TravelName = TravelName, + Price = Price, + TravelConditions = TravelConditions + }; + public XElement GetXElement => new("Travel", + new XAttribute("Id", Id), + new XElement("TravelName", TravelName), + new XElement("Price", Price.ToString()), + new XElement("TravelConditions", Conditions.Select(x => + new XElement("TravelCondition", + + new XElement("Key", x.Key), + + new XElement("Value", x.Value))) + + .ToArray())); + } + +} diff --git a/TravelCompany/TravelCompanyFileImplement/TravelCompanyFileImplement.csproj b/TravelCompany/TravelCompanyFileImplement/TravelCompanyFileImplement.csproj new file mode 100644 index 0000000..44dc3c0 --- /dev/null +++ b/TravelCompany/TravelCompanyFileImplement/TravelCompanyFileImplement.csproj @@ -0,0 +1,16 @@ + + + + net6.0 + enable + enable + + + + + + + + + +