Unfinished starters

This commit is contained in:
Arklightning 2023-03-14 08:36:29 +04:00
parent 09d271632e
commit 7e220017ac
11 changed files with 394 additions and 3 deletions

View File

@ -14,7 +14,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarRepairShopBisinessLogic"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarRepairShopContracts", "CarRepairShopContracts\CarRepairShopContracts.csproj", "{CE6A6A6F-A0D3-428F-B70F-E73695DD2C91}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CarRepairShopListImplement", "CarRepairShopListImplement\CarRepairShopListImplement.csproj", "{7A19A758-A68B-4B08-8F70-62D42A777926}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarRepairShopListImplement", "CarRepairShopListImplement\CarRepairShopListImplement.csproj", "{7A19A758-A68B-4B08-8F70-62D42A777926}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CarRepairShopFileImplement", "CarRepairShopFileImplement\CarRepairShopFileImplement.csproj", "{5B6F8AFD-D465-4D71-A381-9ED66B40A1D3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -42,6 +44,10 @@ Global
{7A19A758-A68B-4B08-8F70-62D42A777926}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7A19A758-A68B-4B08-8F70-62D42A777926}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7A19A758-A68B-4B08-8F70-62D42A777926}.Release|Any CPU.Build.0 = Release|Any CPU
{5B6F8AFD-D465-4D71-A381-9ED66B40A1D3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5B6F8AFD-D465-4D71-A381-9ED66B40A1D3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5B6F8AFD-D465-4D71-A381-9ED66B40A1D3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5B6F8AFD-D465-4D71-A381-9ED66B40A1D3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -177,6 +177,7 @@
this.ButtonSave.TabIndex = 5;
this.ButtonSave.Text = "Сохранить";
this.ButtonSave.UseVisualStyleBackColor = true;
this.ButtonSave.Click += new System.EventHandler(this.ButtonSave_Click);
//
// ButtonCancel
//
@ -187,6 +188,7 @@
this.ButtonCancel.TabIndex = 6;
this.ButtonCancel.Text = "Отмена";
this.ButtonCancel.UseVisualStyleBackColor = true;
this.ButtonCancel.Click += new System.EventHandler(this.ButtonCancel_Click);
//
// FormCar
//
@ -202,6 +204,7 @@
this.Controls.Add(this.label1);
this.Name = "FormCar";
this.Text = "Автомобиль";
this.Load += new System.EventHandler(this.FormCar_Load);
this.groupBox1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit();
this.ResumeLayout(false);

View File

@ -97,6 +97,7 @@
this.Controls.Add(this.dataGridView);
this.Name = "FormCars";
this.Text = "Машины";
this.Load += new System.EventHandler(this.FormCars_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit();
this.ResumeLayout(false);

View File

@ -97,6 +97,7 @@
this.Controls.Add(this.dataGridView);
this.Name = "FormDetails";
this.Text = "Детали";
this.Load += new System.EventHandler(this.FormDetails_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit();
this.ResumeLayout(false);

View File

@ -123,14 +123,14 @@
// ДеталиToolStripMenuItem
//
this.ДеталиToolStripMenuItem.Name = еталиToolStripMenuItem";
this.ДеталиToolStripMenuItem.Size = new System.Drawing.Size(125, 22);
this.ДеталиToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.ДеталиToolStripMenuItem.Text = "Детали";
this.ДеталиToolStripMenuItem.Click += new System.EventHandler(this.ДеталиToolStripMenuItem_Click);
//
// МашиныToolStripMenuItem
//
this.МашиныToolStripMenuItem.Name = "МашиныToolStripMenuItem";
this.МашиныToolStripMenuItem.Size = new System.Drawing.Size(125, 22);
this.МашиныToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.МашиныToolStripMenuItem.Text = "Машины";
this.МашиныToolStripMenuItem.Click += new System.EventHandler(this.АвтомобилиToolStripMenuItem_Click);
//
@ -149,6 +149,7 @@
this.MainMenuStrip = this.menuStrip1;
this.Name = "FormMain";
this.Text = "Автомастерская";
this.Load += new System.EventHandler(this.FormMain_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit();
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();

View File

@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,55 @@
using CarRepairShopFileImplement.Models;
using FoodOrdersFileImplement.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace CarRepairShopFileImplement
{
internal class DataFileSingleton
{
private static DataFileSingleton? instance;
private readonly string CarFileName = "Component.xml";
private readonly string OrderFileName = "Order.xml";
private readonly string DetailFileName = "Detail.xml";
public List<Component> Components { get; private set; }
public List<Order> Orders { get; private set; }
public List<Detail> Details { get; private set; }
public static DataFileSingleton GetInstance()
{
if (instance == null)
{
instance = new DataFileSingleton();
}
return instance;
}
public void SaveComponents() => SaveData(Components, CarFileName, "Components", x => x.GetXElement);
public void SaveDishes() => SaveData(Details, DetailFileName, "Dishes", x => x.GetXElement);
public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement);
private DataFileSingleton()
{
Components = LoadData(CarFileName, "Component", x => Component.Create(x)!)!;
Details = LoadData(DetailFileName, "Dish", x => Detail.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,74 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CarRepairShopFileImplement.Implements
{
internal class ComponentStorage
{
private readonly DataFileSingleton _source;
public ComponentStorage()
{
_source = DataFileSingleton.GetInstance();
}
public List<ComponentViewModel> GetFullList()
{
return _source.Components.Select(x => x.GetViewModel).ToList();
}
public List<ComponentViewModel> GetFilteredList(ComponentSearchModel model)
{
if (string.IsNullOrEmpty(model.ComponentName))
{
return new();
}
return _source.Components.Where(x => x.ComponentName.Contains(model.ComponentName)).Select(x => x.GetViewModel).ToList();
}
//FirstOrDefault выбирается первый или ничего, то есть вернёт первое совпадение или null
public ComponentViewModel? GetElement(ComponentSearchModel model)
{
if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue)
{
return null;
}
return _source.Components.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public ComponentViewModel? Insert(ComponentBindingModel model)
{
model.Id = _source.Components.Count > 0 ? _source.Components.Max(x => x.Id) + 1 : 1;
var newComponent = Component.Create(model);
if (newComponent == null)
{
return null;
}
_source.Components.Add(newComponent);
_source.SaveComponents();
return newComponent.GetViewModel;
}
public ComponentViewModel? Update(ComponentBindingModel model)
{
var component = _source.Components.FirstOrDefault(x => x.Id == model.Id);
if (component == null)
{
return null;
}
component.Update(model);
_source.SaveComponents();
return component.GetViewModel;
}
public ComponentViewModel? Delete(ComponentBindingModel model)
{
var element = _source.Components.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
_source.Components.Remove(element);
_source.SaveComponents();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,61 @@
using CarRepairShopContracts.ViewModels;
using CarRepairShopDataModels.Models;
using CarRepairShopContracts.BindingModels;
using System.Xml.Linq;
namespace CarRepairShopFileImplement.Models
{
public class Car : ICarModel
{
public int Id { get; private set; }
public string ComponentName { get; private set; } = string.Empty;
public double Cost { get; set; }
public static Car? Create(CarBindingModel model)
{
if (model == null)
{
return null;
}
return new Car()
{
Id = model.Id,
ComponentName = model.ComponentName,
Cost = model.Cost
};
}
public static Car? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Car()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
ComponentName = element.Element("ComponentName")!.Value,
Cost = Convert.ToDouble(element.Element("Cost")!.Value)
};
}
public void Update(CarBindingModel model)
{
if (model == null)
{
return;
}
ComponentName = model.ComponentName;
Cost = model.Cost;
}
public CarViewModel GetViewModel => new()
{
Id = Id,
ComponentName = ComponentName,
Cost = Cost
};
public XElement GetXElement => new(
"Component",
new XAttribute("Id", Id),
new XElement("ComponentName", ComponentName),
new XElement("Cost", Cost.ToString())
);
}
}

View File

@ -0,0 +1,94 @@
using CarRepairShopContracts.BindingModels;
using CarRepairShopContracts.ViewModels;
using CarRepairShopDataModels.Models;
using FoodOrdersFileImplement;
using System.Xml.Linq;
namespace FoodOrdersFileImplement.Models
{
public class Detail : ICarModel
{
public int Id { get; private set; }
public string DetailName { get; private set; } = string.Empty;
public double Price { get; private set; }
//словарь для файла, так как нам в файле нужно хранить просто id компонента и его количество
public Dictionary<int, int> Components { get; private set; } = new();
private Dictionary<int, (ICarModel, int)>? _detailComponents = null;
public Dictionary<int, (ICarModel, int)> DetailComponents
{
get
{
if (_detailComponents == null)
{
var _source = DataFileSingleton.GetInstance();
_detailComponents = Components.ToDictionary(x => x.Key, y => ((_source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, y.Value));
}
return _detailComponents;
}
}
public static Detail? Create(CarBindingModel model)
{
if (model == null)
{
return null;
}
return new Detail()
{
Id = model.Id,
DetailName = model.DetailName,
Price = model.Price,
Components = model.DetailComponents.ToDictionary(x => x.Key, x => x.Value.Item2)
};
}
public static Detail? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Detail()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
DetailName = element.Element("DetailName")!.Value,
Price = Convert.ToDouble(element.Element("Price")!.Value),
Components = element.Element("DetailComponents")!.Elements("DetailComponent").ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value))
};
}
public void Update(DetailBindingModel model)
{
if (model == null)
{
return;
}
DetailName = model.DetailName;
Price = model.Price;
Components = model.DetailComponents.ToDictionary(x => x.Key, x => x.Value.Item2);
//обнуляем словарь, чтобы в случае обновления, у нас был в дальнейшем сформирован актуальный словарь
// с помощью get метода
_detailComponents = null;
}
public DetailViewModel GetViewModel => new()
{
Id = Id,
DetailName = DetailName,
Price = Price,
DetailComponents = DetailComponents
};
public XElement GetXElement => new(
"Detail",
new XAttribute("Id", Id),
new XElement("DetailName", DetailName),
new XElement("Price", Price.ToString()),
new XElement("DetailComponents", Components.Select(x =>
new XElement("DishComponent",
new XElement("Key", x.Key),
new XElement("Value", x.Value))).ToArray()
)
);
}
}

View File

@ -0,0 +1,86 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace CarRepairShopFileImplement.Models
{
internal class Order : IOrderModel
{
public int Id { get; private set; }
public int DishId { 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; }
public DateTime? DateImplement { get; private set; }
public static Order? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Order()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
DishId = Convert.ToInt32(element.Element("DishId")!.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 static Order? Create(OrderBindingModel? model)
{
if (model == null)
{
return null;
}
return new Order
{
Id = model.Id,
DishId = model.DishId,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
};
}
public void Update(OrderBindingModel? model)
{
if (model == null)
{
return;
}
Status = model.Status;
DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
Id = Id,
DishId = DishId,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement
};
public XElement GetXElement => new(
"Order",
new XAttribute("Id", Id),
new XElement("DishId", DishId.ToString()),
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())
);
}
}