Lab2 рабочая

This commit is contained in:
Arklightning 2023-03-25 08:05:18 +04:00
parent 7e220017ac
commit e6f4b5d103
12 changed files with 304 additions and 111 deletions

View File

@ -12,5 +12,6 @@ namespace CarRepairShopContracts.BindingModels
public int Id { get; set; }
public string DetailName { get; set; } = string.Empty;
public double Cost { get; set; }
public Dictionary<int, (IDetailModel, int)> DetailComponents { get; set; } = new();
}
}

View File

@ -15,5 +15,6 @@ namespace CarRepairShopContracts.ViewModels
public string DetailName { get; set; } = string.Empty;
[DisplayName("Цена")]
public double Cost { get; set; }
public Dictionary<int, (ICarModel, int)> DetailComponents { get; set; } = new();
}
}

View File

@ -10,6 +10,6 @@ namespace CarRepairShopDataModels.Models
{
string CarName { get; }
double Price { get; }
Dictionary<int, (IDetailModel, int)> CarDetails { get; }
//Dictionary<int, (IDetailModel, int)> CarDetails { get; }
}
}

View File

@ -6,4 +6,9 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\CarRepairShopContracts\CarRepairShopContracts.csproj" />
<ProjectReference Include="..\CarRepairShopDataModels\CarRepairShopDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -13,10 +13,10 @@ namespace CarRepairShopFileImplement
internal class DataFileSingleton
{
private static DataFileSingleton? instance;
private readonly string CarFileName = "Component.xml";
private readonly string CarFileName = "Car.xml";
private readonly string OrderFileName = "Order.xml";
private readonly string DetailFileName = "Detail.xml";
public List<Component> Components { get; private set; }
public List<Car> Cars { get; private set; }
public List<Order> Orders { get; private set; }
public List<Detail> Details { get; private set; }
public static DataFileSingleton GetInstance()
@ -27,13 +27,13 @@ namespace CarRepairShopFileImplement
}
return instance;
}
public void SaveComponents() => SaveData(Components, CarFileName, "Components", x => x.GetXElement);
public void SaveDishes() => SaveData(Details, DetailFileName, "Dishes", x => x.GetXElement);
public void SaveCars() => SaveData(Cars, CarFileName, "Cars", x => x.GetXElement);
public void SaveDetails() => SaveData(Details, DetailFileName, "Details", 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)!)!;
Cars = LoadData(CarFileName, "Car", x => Car.Create(x)!)!;
Details = LoadData(DetailFileName, "Detail", 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)

View File

@ -0,0 +1,79 @@
using CarRepairShopContracts.BindingModels;
using CarRepairShopContracts.SearchModels;
using CarRepairShopContracts.StoragesContracts;
using CarRepairShopContracts.ViewModels;
using CarRepairShopFileImplement.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CarRepairShopFileImplement.Implements
{
internal class CarStorage : ICarStorage
{
private readonly DataFileSingleton _source;
public CarStorage()
{
_source = DataFileSingleton.GetInstance();
}
public List<CarViewModel> GetFullList()
{
return _source.Cars.Select(x => x.GetViewModel).ToList();
}
public List<CarViewModel> GetFilteredList(CarSearchModel model)
{
if (string.IsNullOrEmpty(model.CarName))
{
return new();
}
return _source.Cars.Where(x => x.CarName.Contains(model.CarName)).Select(x => x.GetViewModel).ToList();
}
//FirstOrDefault выбирается первый или ничего, то есть вернёт первое совпадение или null
public CarViewModel? GetElement(CarSearchModel model)
{
if (string.IsNullOrEmpty(model.CarName) && !model.Id.HasValue)
{
return null;
}
return _source.Cars.FirstOrDefault(x => (!string.IsNullOrEmpty(model.CarName) && x.CarName == model.CarName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public CarViewModel? Insert(CarBindingModel model)
{
model.Id = _source.Cars.Count > 0 ? _source.Cars.Max(x => x.Id) + 1 : 1;
var newComponent = Car.Create(model);
if (newComponent == null)
{
return null;
}
_source.Cars.Add(newComponent);
_source.SaveCars();
return newComponent.GetViewModel;
}
public CarViewModel? Update(CarBindingModel model)
{
var component = _source.Cars.FirstOrDefault(x => x.Id == model.Id);
if (component == null)
{
return null;
}
component.Update(model);
_source.SaveCars();
return component.GetViewModel;
}
public CarViewModel? Delete(CarBindingModel model)
{
var element = _source.Cars.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
_source.Cars.Remove(element);
_source.SaveCars();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -1,74 +0,0 @@
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,79 @@
using CarRepairShopContracts.BindingModels;
using CarRepairShopContracts.SearchModels;
using CarRepairShopContracts.StoragesContracts;
using CarRepairShopContracts.ViewModels;
using CarRepairShopFileImplement.Models;
using FoodOrdersFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CarRepairShopFileImplement.Implements
{
internal class DetailStorage : IDetailStorage
{
private readonly DataFileSingleton _source;
public DetailStorage()
{
_source = DataFileSingleton.GetInstance();
}
public List<DetailViewModel> GetFullList()
{
return _source.Details.Select(x => x.GetViewModel).ToList();
}
public List<DetailViewModel> GetFilteredList(DetailSearchModel model)
{
if (string.IsNullOrEmpty(model.DetailName))
{
return new();
}
return _source.Details.Where(x => x.DetailName.Contains(model.DetailName)).Select(x => x.GetViewModel).ToList();
}
public DetailViewModel? GetElement(DetailSearchModel model)
{
if (string.IsNullOrEmpty(model.DetailName) && !model.Id.HasValue)
{
return null;
}
return _source.Details.FirstOrDefault(x => (!string.IsNullOrEmpty(model.DetailName) && x.DetailName == model.DetailName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public DetailViewModel? Insert(DetailBindingModel model)
{
model.Id = _source.Details.Count > 0 ? _source.Details.Max(x => x.Id) + 1 : 1;
var newDoc = Detail.Create(model);
if (newDoc == null)
{
return null;
}
_source.Details.Add(newDoc);
_source.SaveDetails();
return newDoc.GetViewModel;
}
public DetailViewModel? Update(DetailBindingModel model)
{
var detail = _source.Details.FirstOrDefault(x => x.Id == model.Id);
if (detail == null)
{
return null;
}
detail.Update(model);
_source.SaveDetails();
return detail.GetViewModel;
}
public DetailViewModel? Delete(DetailBindingModel model)
{
var document = _source.Details.FirstOrDefault(x => x.Id == model.Id);
if (document == null)
{
return null;
}
document.Update(model);
_source.SaveDetails();
return document.GetViewModel;
}
}
}

View File

@ -0,0 +1,87 @@
using CarRepairShopContracts.BindingModels;
using CarRepairShopContracts.SearchModels;
using CarRepairShopContracts.StoragesContracts;
using CarRepairShopContracts.ViewModels;
using CarRepairShopFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CarRepairShopFileImplement.Implements
{
internal 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 == 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;
viewModel.CarName = _source.Details.FirstOrDefault(x => (x.Id == order.CarId))?.DetailName;
return viewModel;
}
public OrderViewModel? Delete(OrderBindingModel model)
{
var order = _source.Orders.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
return null;
}
order.Update(model);
_source.SaveOrders();
return order.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 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 order.GetViewModel;
}
}
}

View File

@ -1,6 +1,9 @@
using CarRepairShopContracts.ViewModels;
using CarRepairShopContracts.BindingModels;
using CarRepairShopContracts.SearchModels;
using CarRepairShopContracts.StoragesContracts;
using CarRepairShopContracts.ViewModels;
using CarRepairShopFileImplement.Models;
using CarRepairShopDataModels.Models;
using CarRepairShopContracts.BindingModels;
using System.Xml.Linq;
namespace CarRepairShopFileImplement.Models
@ -8,10 +11,10 @@ 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 string CarName { get; private set; } = string.Empty;
public double Price { get; set; }
public static Car? Create(CarBindingModel model)
{
{
if (model == null)
{
return null;
@ -19,8 +22,8 @@ namespace CarRepairShopFileImplement.Models
return new Car()
{
Id = model.Id,
ComponentName = model.ComponentName,
Cost = model.Cost
CarName = model.CarName,
Price = model.Price
};
}
public static Car? Create(XElement element)
@ -32,8 +35,8 @@ namespace CarRepairShopFileImplement.Models
return new Car()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
ComponentName = element.Element("ComponentName")!.Value,
Cost = Convert.ToDouble(element.Element("Cost")!.Value)
CarName = element.Element("ComponentName")!.Value,
Price= Convert.ToDouble(element.Element("Cost")!.Value)
};
}
public void Update(CarBindingModel model)
@ -42,20 +45,20 @@ namespace CarRepairShopFileImplement.Models
{
return;
}
ComponentName = model.ComponentName;
Cost = model.Cost;
CarName = model.CarName;
Price = model.Price;
}
public CarViewModel GetViewModel => new()
{
Id = Id,
ComponentName = ComponentName,
Cost = Cost
CarName = CarName,
Price = Price
};
public XElement GetXElement => new(
"Component",
new XAttribute("Id", Id),
new XElement("ComponentName", ComponentName),
new XElement("Cost", Cost.ToString())
new XElement("CartName", CarName),
new XElement("Cost", Price.ToString())
);
}
}

View File

@ -1,17 +1,22 @@
using CarRepairShopContracts.BindingModels;
using CarRepairShopContracts.SearchModels;
using CarRepairShopContracts.StoragesContracts;
using CarRepairShopContracts.ViewModels;
using CarRepairShopFileImplement.Models;
using CarRepairShopDataModels.Models;
using FoodOrdersFileImplement;
using System.Xml.Linq;
using CarRepairShopFileImplement;
namespace FoodOrdersFileImplement.Models
{
public class Detail : ICarModel
public class Detail : IDetailModel
{
public int Id { get; private set; }
public string DetailName { get; private set; } = string.Empty;
public double Price { get; private set; }
public double Cost { get; private set; }
//словарь для файла, так как нам в файле нужно хранить просто id компонента и его количество
public Dictionary<int, int> Components { get; private set; } = new();
@ -25,12 +30,12 @@ namespace FoodOrdersFileImplement.Models
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));
_detailComponents = Components.ToDictionary(x => x.Key, y => ((_source.Cars.FirstOrDefault(z => z.Id == y.Key) as ICarModel)!, y.Value));
}
return _detailComponents;
}
}
public static Detail? Create(CarBindingModel model)
public static Detail? Create(DetailBindingModel model)
{
if (model == null)
{
@ -40,7 +45,7 @@ namespace FoodOrdersFileImplement.Models
{
Id = model.Id,
DetailName = model.DetailName,
Price = model.Price,
Cost = model.Cost,
Components = model.DetailComponents.ToDictionary(x => x.Key, x => x.Value.Item2)
};
}
@ -54,7 +59,7 @@ namespace FoodOrdersFileImplement.Models
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
DetailName = element.Element("DetailName")!.Value,
Price = Convert.ToDouble(element.Element("Price")!.Value),
Cost = 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))
};
}
@ -65,7 +70,7 @@ namespace FoodOrdersFileImplement.Models
return;
}
DetailName = model.DetailName;
Price = model.Price;
Cost = model.Cost;
Components = model.DetailComponents.ToDictionary(x => x.Key, x => x.Value.Item2);
//обнуляем словарь, чтобы в случае обновления, у нас был в дальнейшем сформирован актуальный словарь
// с помощью get метода
@ -75,7 +80,7 @@ namespace FoodOrdersFileImplement.Models
{
Id = Id,
DetailName = DetailName,
Price = Price,
Cost = Cost,
DetailComponents = DetailComponents
};
@ -83,7 +88,7 @@ namespace FoodOrdersFileImplement.Models
"Detail",
new XAttribute("Id", Id),
new XElement("DetailName", DetailName),
new XElement("Price", Price.ToString()),
new XElement("Price", Cost.ToString()),
new XElement("DetailComponents", Components.Select(x =>
new XElement("DishComponent",
new XElement("Key", x.Key),

View File

@ -1,4 +1,11 @@
using System;
using CarRepairShopContracts.BindingModels;
using CarRepairShopContracts.SearchModels;
using CarRepairShopContracts.StoragesContracts;
using CarRepairShopContracts.ViewModels;
using CarRepairShopFileImplement.Models;
using CarRepairShopDataModels.Models;
using CarRepairShopDataModels.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -10,7 +17,7 @@ namespace CarRepairShopFileImplement.Models
internal class Order : IOrderModel
{
public int Id { get; private set; }
public int DishId { get; private set; }
public int CarId { get; private set; }
public int Count { get; private set; }
public double Sum { get; private set; }
public OrderStatus Status { get; private set; }
@ -26,7 +33,7 @@ namespace CarRepairShopFileImplement.Models
return new Order()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
DishId = Convert.ToInt32(element.Element("DishId")!.Value),
CarId = 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),
@ -44,7 +51,7 @@ namespace CarRepairShopFileImplement.Models
return new Order
{
Id = model.Id,
DishId = model.DishId,
CarId = model.CarId,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
@ -64,7 +71,7 @@ namespace CarRepairShopFileImplement.Models
public OrderViewModel GetViewModel => new()
{
Id = Id,
DishId = DishId,
CarId = CarId,
Count = Count,
Sum = Sum,
Status = Status,
@ -75,7 +82,7 @@ namespace CarRepairShopFileImplement.Models
public XElement GetXElement => new(
"Order",
new XAttribute("Id", Id),
new XElement("DishId", DishId.ToString()),
new XElement("DishId", CarId.ToString()),
new XElement("Count", Count.ToString()),
new XElement("Sum", Sum.ToString()),
new XElement("Status", Status.ToString()),