SecondWork

This commit is contained in:
Дарья Антонова 2023-05-04 03:45:21 +04:00
parent 28b83f6303
commit 430ef543a0
11 changed files with 578 additions and 2 deletions

View File

@ -4,6 +4,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
VisualStudioVersion = 17.3.32922.545
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryView", "ConfectioneryView\ConfectioneryView.csproj", "{9B75FC6A-4403-406F-BD78-70A656A9C38C}"
ProjectSection(ProjectDependencies) = postProject
{F40D4032-72B5-44F1-9657-940F5931C1C1} = {F40D4032-72B5-44F1-9657-940F5931C1C1}
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryDataModels", "ConfectioneryDataModels\ConfectioneryDataModels.csproj", "{AE5AAF6C-4EA0-45F6-A433-50D1A347DD60}"
EndProject
@ -16,6 +19,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryListImplement"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryBusinessLogic", "ConfectioneryBusinessLogic\ConfectioneryBusinessLogic.csproj", "{FF4C1B6D-E988-4CE0-AF41-C4539744B9BE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryFileImplement", "ConfectioneryFileImplement\ConfectioneryFileImplement.csproj", "{F40D4032-72B5-44F1-9657-940F5931C1C1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -42,6 +47,10 @@ Global
{FF4C1B6D-E988-4CE0-AF41-C4539744B9BE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FF4C1B6D-E988-4CE0-AF41-C4539744B9BE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FF4C1B6D-E988-4CE0-AF41-C4539744B9BE}.Release|Any CPU.Build.0 = Release|Any CPU
{F40D4032-72B5-44F1-9657-940F5931C1C1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F40D4032-72B5-44F1-9657-940F5931C1C1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F40D4032-72B5-44F1-9657-940F5931C1C1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F40D4032-72B5-44F1-9657-940F5931C1C1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\ConfectioneryContracts\ConfectioneryContracts.csproj" />
<ProjectReference Include="..\ConfectioneryDataModels\ConfectioneryDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,58 @@
using ConfectioneryFileImplement.Models;
using System.Xml.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfectioneryFileImplement
{
public class DataFileSingleton
{
private static DataFileSingleton? instance;
private readonly string IngredientFileName = "Ingredient.xml";
private readonly string OrderFileName = "Order.xml";
private readonly string SweetsFileName = "Sweets.xml";
public List<Ingredient> Ingredients { get; private set; }
public List<Order> Orders { get; private set; }
public List<Sweets> ListSweets { get; private set; }
public static DataFileSingleton GetInstance()
{
if (instance == null)
{
instance = new DataFileSingleton();
}
return instance;
}
public void SaveIngredients() => SaveData(Ingredients, IngredientFileName,
"Ingredients", x => x.GetXElement);
public void SaveListSweets() => SaveData(ListSweets, SweetsFileName,
"ListSweets", x => x.GetXElement);
public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement);
private DataFileSingleton()
{
Ingredients = LoadData(IngredientFileName, "Ingredient", x => Ingredient.Create(x)!)!;
ListSweets = LoadData(SweetsFileName, "Sweets", x => Sweets.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,79 @@
using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.SearchModels;
using ConfectioneryContracts.StoragesContracts;
using ConfectioneryContracts.ViewModels;
using ConfectioneryFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfectioneryFileImplement.Implements
{
public class IngredientStorage : IIngredientStorage
{
private readonly DataFileSingleton source;
public IngredientStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<IngredientViewModel> GetFullList()
{
return source.Ingredients.Select(x => x.GetViewModel).ToList();
}
public List<IngredientViewModel> GetFilteredList(IngredientSearchModel model)
{
if (string.IsNullOrEmpty(model.IngredientName))
{
return new();
}
return source.Ingredients.Where(x =>
x.IngredientName.Contains(model.IngredientName)).Select(x => x.GetViewModel).ToList();
}
public IngredientViewModel? GetElement(IngredientSearchModel model)
{
if (string.IsNullOrEmpty(model.IngredientName) && !model.Id.HasValue)
{
return null;
}
return source.Ingredients.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.IngredientName) && x.IngredientName ==
model.IngredientName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public IngredientViewModel? Insert(IngredientBindingModel model)
{
model.Id = source.Ingredients.Count > 0 ? source.Ingredients.Max(x => x.Id) + 1 : 1;
var newIngredient = Ingredient.Create(model);
if (newIngredient == null)
{
return null;
}
source.Ingredients.Add(newIngredient);
source.SaveIngredients();
return newIngredient.GetViewModel;
}
public IngredientViewModel? Update(IngredientBindingModel model)
{
var ingredient = source.Ingredients.FirstOrDefault(x => x.Id == model.Id);
if (ingredient == null)
{
return null;
}
ingredient.Update(model);
source.SaveIngredients();
return ingredient.GetViewModel;
}
public IngredientViewModel? Delete(IngredientBindingModel model)
{
var element = source.Ingredients.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
source.Ingredients.Remove(element);
source.SaveIngredients();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,96 @@
using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.SearchModels;
using ConfectioneryContracts.StoragesContracts;
using ConfectioneryContracts.ViewModels;
using ConfectioneryFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfectioneryFileImplement.Implements
{
public class OrderStorage : IOrderStorage
{
private readonly DataFileSingleton source;
public OrderStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<OrderViewModel> GetFullList()
{
return source.Orders.Select(x => GetViewModel(x)).ToList();
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
return source.Orders.Where(x => x.Id.Equals(model.Id)).Select(x => GetViewModel(x)).ToList();
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
return source.Orders.FirstOrDefault(x =>
(model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
private OrderViewModel GetViewModel(Order order)
{
var viewModel = order.GetViewModel;
foreach (var sweets in source.ListSweets)
{
if (sweets.Id == order.SweetsId)
{
viewModel.SweetsName = sweets.SweetsName;
break;
}
}
return viewModel;
}
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 GetViewModel(newOrder);
}
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 GetViewModel(order);
}
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 GetViewModel(element);
}
return null;
}
}
}

View File

@ -0,0 +1,79 @@
using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.SearchModels;
using ConfectioneryContracts.StoragesContracts;
using ConfectioneryContracts.ViewModels;
using ConfectioneryFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfectioneryFileImplement.Implements
{
public class SweetsStorage : ISweetsStorage
{
private readonly DataFileSingleton source;
public SweetsStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<SweetsViewModel> GetFullList()
{
return source.ListSweets.Select(x => x.GetViewModel).ToList();
}
public List<SweetsViewModel> GetFilteredList(SweetsSearchModel model)
{
if (string.IsNullOrEmpty(model.SweetsName))
{
return new();
}
return source.ListSweets.Where(x =>
x.SweetsName.Contains(model.SweetsName)).Select(x => x.GetViewModel).ToList();
}
public SweetsViewModel? GetElement(SweetsSearchModel model)
{
if (string.IsNullOrEmpty(model.SweetsName) && !model.Id.HasValue)
{
return null;
}
return source.ListSweets.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.SweetsName) && x.SweetsName ==
model.SweetsName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public SweetsViewModel? Insert(SweetsBindingModel model)
{
model.Id = source.ListSweets.Count > 0 ? source.ListSweets.Max(x => x.Id) + 1 : 1;
var newSweets = Sweets.Create(model);
if (newSweets == null)
{
return null;
}
source.ListSweets.Add(newSweets);
source.SaveListSweets();
return newSweets.GetViewModel;
}
public SweetsViewModel? Update(SweetsBindingModel model)
{
var sweets = source.ListSweets.FirstOrDefault(x => x.Id == model.Id);
if (sweets == null)
{
return null;
}
sweets.Update(model);
source.SaveListSweets();
return sweets.GetViewModel;
}
public SweetsViewModel? Delete(SweetsBindingModel model)
{
var element = source.ListSweets.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
source.ListSweets.Remove(element);
source.SaveListSweets();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,64 @@
using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.ViewModels;
using ConfectioneryDataModels.Models;
using System.Xml.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfectioneryFileImplement.Models
{
public class Ingredient : IIngredientModel
{
public int Id { get; private set; }
public string IngredientName { get; private set; } = string.Empty;
public double Cost { get; set; }
public static Ingredient? Create(IngredientBindingModel model)
{
if (model == null)
{
return null;
}
return new Ingredient()
{
Id = model.Id,
IngredientName = model.IngredientName,
Cost = model.Cost
};
}
public static Ingredient? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Ingredient()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
IngredientName = element.Element("IngredientName")!.Value,
Cost = Convert.ToDouble(element.Element("Cost")!.Value)
};
}
public void Update(IngredientBindingModel model)
{
if (model == null)
{
return;
}
IngredientName = model.IngredientName;
Cost = model.Cost;
}
public IngredientViewModel GetViewModel => new()
{
Id = Id,
IngredientName = IngredientName,
Cost = Cost
};
public XElement GetXElement => new("Ingredient",
new XAttribute("Id", Id),
new XElement("IngredientName", IngredientName),
new XElement("Cost", Cost.ToString()));
}
}

View File

@ -0,0 +1,89 @@
using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.ViewModels;
using ConfectioneryDataModels.Enums;
using ConfectioneryDataModels.Models;
using System.Xml.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfectioneryFileImplement.Models
{
public class Order : IOrderModel
{
public int Id { get; private set; }
public int SweetsId { get; private set; }
public int Count { get; private set; }
public double Sum { get; private set; }
public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен;
public DateTime DateCreate { get; private set; } = DateTime.Now;
public DateTime? DateImplement { get; private set; }
public static Order? Create(OrderBindingModel? model)
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
SweetsId = model.SweetsId,
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),
SweetsId = Convert.ToInt32(element.Element("SweetsId")!.Value),
Sum = Convert.ToDouble(element.Element("Sum")!.Value),
Count = Convert.ToInt32(element.Element("Count")!.Value),
Status = (OrderStatus)Enum.Parse(typeof(OrderStatus), 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;
DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
SweetsId = SweetsId,
Count = Count,
Sum = Sum,
DateCreate = DateCreate,
DateImplement = DateImplement,
Id = Id,
Status = Status,
};
public XElement GetXElement => new("Order",
new XAttribute("Id", Id),
new XElement("SweetsId", SweetsId),
new XElement("Count", Count.ToString()),
new XElement("Sum", Sum.ToString()),
new XElement("Status", Status.ToString()),
new XElement("DateCreate", DateCreate.ToString()),
new XElement("DateImplement", DateImplement.ToString()));
}
}

View File

@ -0,0 +1,87 @@
using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.ViewModels;
using ConfectioneryDataModels.Models;
using System.Xml.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfectioneryFileImplement.Models
{
public class Sweets : ISweetsModel
{
public int Id { get; private set; }
public string SweetsName { get; private set; } = string.Empty;
public double Price { get; private set; }
public Dictionary<int, int> Ingredients { get; private set; } = new();
private Dictionary<int, (IIngredientModel, int)>? _productIngredients = null;
public Dictionary<int, (IIngredientModel, int)> SweetsIngredients
{
get
{
if (_productIngredients == null)
{
var source = DataFileSingleton.GetInstance();
_productIngredients = Ingredients.ToDictionary(x => x.Key, y => ((source.Ingredients.FirstOrDefault(z => z.Id == y.Key) as IIngredientModel)!, y.Value));
}
return _productIngredients;
}
}
public static Sweets? Create(SweetsBindingModel model)
{
if (model == null)
{
return null;
}
return new Sweets()
{
Id = model.Id,
SweetsName = model.SweetsName,
Price = model.Price,
Ingredients = model.SweetsIngredients.ToDictionary(x => x.Key, x => x.Value.Item2)
};
}
public static Sweets? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Sweets()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
SweetsName = element.Element("SweetsName")!.Value,
Price = Convert.ToDouble(element.Element("Price")!.Value),
Ingredients = element.Element("SweetsIngredients")!.Elements("SweetsIngredient").ToDictionary(x =>
Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value))
};
}
public void Update(SweetsBindingModel model)
{
if (model == null)
{
return;
}
SweetsName = model.SweetsName;
Price = model.Price;
Ingredients = model.SweetsIngredients.ToDictionary(x => x.Key, x =>
x.Value.Item2);
_productIngredients = null;
}
public SweetsViewModel GetViewModel => new()
{
Id = Id,
SweetsName = SweetsName,
Price = Price,
SweetsIngredients = SweetsIngredients
};
public XElement GetXElement => new("Sweets",
new XAttribute("Id", Id),
new XElement("SweetsName", SweetsName),
new XElement("Price", Price.ToString()),
new XElement("SweetsIngredients", Ingredients.Select(x => new XElement("SweetsIngredient",
new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray()));
}
}

View File

@ -31,6 +31,7 @@
<ItemGroup>
<ProjectReference Include="..\ConfectioneryBusinessLogic\ConfectioneryBusinessLogic.csproj" />
<ProjectReference Include="..\ConfectioneryContracts\ConfectioneryContracts.csproj" />
<ProjectReference Include="..\ConfectioneryFileImplement\ConfectioneryFileImplement.csproj" />
<ProjectReference Include="..\ConfectioneryListImplement\ConfectioneryListImplement.csproj" />
</ItemGroup>

View File

@ -1,7 +1,7 @@
using ConfectioneryBusinessLogic.BusinessLogics;
using ConfectioneryContracts.BusinessLogicsContracts;
using ConfectioneryContracts.StoragesContracts;
using ConfectioneryListImplement.Implements;
using ConfectioneryFileImplement.Implements;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
@ -26,7 +26,7 @@ namespace ConfectioneryView
ConfigureServices(services);
_serviceProvider = services.BuildServiceProvider();
//Application.Run(_serviceProvider.GetRequiredService<FormMain>());
Application.Run(_serviceProvider.GetRequiredService<FormMain>());
}
private static void ConfigureServices(ServiceCollection services)