Nevaeva K. A. LabWork#2 Base #2

Closed
ksenia_nevaeva wants to merge 1 commits from lab2_base into lab1_base
11 changed files with 591 additions and 2 deletions

View File

@ -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

View File

@ -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

View File

@ -29,6 +29,7 @@
<ProjectReference Include="..\TravelCompanyBusinessLogic\TravelCompanyBusinessLogic.csproj" />
<ProjectReference Include="..\TravelCompanyContracts\TravelCompanyContracts.csproj" />
<ProjectReference Include="..\TravelCompanyDataModels\TravelCompanyDataModels.csproj" />
<ProjectReference Include="..\TravelCompanyFileImplement\TravelCompanyFileImplement.csproj" />
<ProjectReference Include="..\TravelCompanyListImplement\TravelCompanyListImplement.csproj" />
</ItemGroup>

View File

@ -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<Condition> Conditions { get; private set; }
public List<Order> Orders { get; private set; }
public List<Travel> 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<T>? LoadData<T>(string filename, string xmlNodeName,
Func<XElement, T> selectFunction)
{
if (File.Exists(filename))
{
return
XDocument.Load(filename)?.Root?.Elements(xmlNodeName)?.Select(selectFunction)?.ToList();
}
return new List<T>();
}
private static void SaveData<T>(List<T> data, string filename, string xmlNodeName, Func<T, XElement> selectFunction)
{
if (data != null)
{
new XDocument(new XElement(xmlNodeName,
data.Select(selectFunction).ToArray())).Save(filename);
}
}
}
}

View File

@ -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<ConditionViewModel> GetFullList()
{
return source.Conditions
.Select(x => x.GetViewModel)
.ToList();
}
public List<ConditionViewModel> 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;
}
}
}

View File

@ -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<OrderViewModel> GetFullList()
{
return _source.Orders
.Select(x => InsertTravelName(x.GetViewModel))
.ToList();
}
public List<OrderViewModel> 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;
}
}
}

View File

@ -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<TravelViewModel> GetFullList()
{
return _source.Travels
.Select(x => x.GetViewModel)
.ToList();
}
public List<TravelViewModel> 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;
}
}
}

View File

@ -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()));
}
}

View File

@ -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()));
}
}

View File

@ -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<int, int> Conditions { get; private set; } = new();
private Dictionary<int, (IConditionModel, int)>? _travelConditions = null;
public Dictionary<int, (IConditionModel, int)> 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()));
}
}

View File

@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\TravelCompanyBusinessLogic\TravelCompanyBusinessLogic.csproj" />
<ProjectReference Include="..\TravelCompanyContracts\TravelCompanyContracts.csproj" />
<ProjectReference Include="..\TravelCompanyDataModels\TravelCompanyDataModels.csproj" />
<ProjectReference Include="..\TravelCompanyListImplement\TravelCompanyListImplement.csproj" />
</ItemGroup>
</Project>