Новая реализация хранилища магазинов

This commit is contained in:
Володя 2023-05-01 14:18:15 +03:00
parent 1a02b213fd
commit 3923eacebd
12 changed files with 290 additions and 29 deletions

View File

@ -14,6 +14,7 @@ namespace AutomobilePlantContracts.BindingModels
public string Adress { get; set; } = String.Empty ;
public DateTime DateOpen { get; set; } = new();
public int Fullness { get; set; }
public Dictionary<int, (ICarModel, int)> Cars { get; set; } = new();

View File

@ -1,6 +1,7 @@
using AutomobilePlantContracts.BindingModels;
using AutomobilePlantContracts.SearchModel;
using AutomobilePlantContracts.ViewModel;
using AutomobilePlantDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
@ -13,6 +14,8 @@ namespace AutomobilePlantContracts.StoragesContracts
{
List<CarShopViewModel> GetFullList();
List<CarShopViewModel> GetFilteredList(CarShopSearchModel model);
bool CheckShop (CarShopSearchModel model);
bool TrySell (CarShopSearchModel model, ICarModel car);
CarShopViewModel? GetElement(CarShopSearchModel model);
CarShopViewModel? Insert(CarShopBindingModel model);
CarShopViewModel? Update(CarShopBindingModel model);

View File

@ -16,6 +16,8 @@ namespace AutomobilePlantContracts.ViewModel
public string Adress { get; set; } = String.Empty;
[DisplayName("Дата открытия")]
public DateTime DateOpen { get; set; } = new();
[DisplayName("Наполненность")]
public int Fullness { get; set; }
public Dictionary<int, (ICarModel, int)> Cars { get; set; } = new();

View File

@ -11,6 +11,7 @@ namespace AutomobilePlantDataModels.Models
string ShopName { get; }
string Adress { get; }
DateTime DateOpen { get; }
int Fullness { get; }
Dictionary<int, (ICarModel, int)> Cars { get; }
}
}

View File

@ -2,6 +2,7 @@
using AutomobilePlantContracts.SearchModel;
using AutomobilePlantContracts.StoragesContracts;
using AutomobilePlantContracts.ViewModel;
using AutomobilePlantDataModels.Models;
using AutomobilePlantListImplement.Models;
using System;
using System.Collections.Generic;
@ -117,5 +118,15 @@ namespace AutomobilePlantListImplement.Implements
return null;
}
public bool CheckShop(CarShopSearchModel model)
{
throw new NotImplementedException();
}
public bool TrySell(CarShopSearchModel model, ICarModel car)
{
throw new NotImplementedException();
}
}
}

View File

@ -18,6 +18,7 @@ namespace AutomobilePlantListImplement.Models
public string Adress { get; private set; } = String.Empty;
public DateTime DateOpen { get; private set; }
public int Fullness { get; set; }
public Dictionary<int, (ICarModel, int)> Cars { get; private set; } = new ();

View File

@ -28,9 +28,9 @@
/// </summary>
private void InitializeComponent()
{
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle3 = new System.Windows.Forms.DataGridViewCellStyle();
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle4 = new System.Windows.Forms.DataGridViewCellStyle();
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle5 = new System.Windows.Forms.DataGridViewCellStyle();
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle6 = new System.Windows.Forms.DataGridViewCellStyle();
this.ShopNameLabel = new System.Windows.Forms.Label();
this.AdressLabel = new System.Windows.Forms.Label();
this.ShopNameTextBox = new System.Windows.Forms.TextBox();
@ -44,8 +44,11 @@
this.ComponentNameField = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.CountField = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.DateOpenPicker = new System.Windows.Forms.DateTimePicker();
this.FullnessnumericUpDown = new System.Windows.Forms.NumericUpDown();
this.Fullness = new System.Windows.Forms.Label();
this.ComponentsGroupBox.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.DataGridView)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.FullnessnumericUpDown)).BeginInit();
this.SuspendLayout();
//
// ShopNameLabel
@ -132,38 +135,38 @@
//
// DataGridView
//
dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Control;
dataGridViewCellStyle1.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.WindowText;
dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
this.DataGridView.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;
dataGridViewCellStyle4.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
dataGridViewCellStyle4.BackColor = System.Drawing.SystemColors.Control;
dataGridViewCellStyle4.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
dataGridViewCellStyle4.ForeColor = System.Drawing.SystemColors.WindowText;
dataGridViewCellStyle4.SelectionBackColor = System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle4.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
dataGridViewCellStyle4.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
this.DataGridView.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle4;
this.DataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.DataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.ID,
this.ComponentNameField,
this.CountField});
dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
dataGridViewCellStyle2.BackColor = System.Drawing.SystemColors.Window;
dataGridViewCellStyle2.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
dataGridViewCellStyle2.ForeColor = System.Drawing.SystemColors.ControlText;
dataGridViewCellStyle2.SelectionBackColor = System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle2.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
dataGridViewCellStyle2.WrapMode = System.Windows.Forms.DataGridViewTriState.False;
this.DataGridView.DefaultCellStyle = dataGridViewCellStyle2;
dataGridViewCellStyle5.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
dataGridViewCellStyle5.BackColor = System.Drawing.SystemColors.Window;
dataGridViewCellStyle5.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
dataGridViewCellStyle5.ForeColor = System.Drawing.SystemColors.ControlText;
dataGridViewCellStyle5.SelectionBackColor = System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle5.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
dataGridViewCellStyle5.WrapMode = System.Windows.Forms.DataGridViewTriState.False;
this.DataGridView.DefaultCellStyle = dataGridViewCellStyle5;
this.DataGridView.Location = new System.Drawing.Point(7, 29);
this.DataGridView.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.DataGridView.Name = "DataGridView";
dataGridViewCellStyle3.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
dataGridViewCellStyle3.BackColor = System.Drawing.SystemColors.Control;
dataGridViewCellStyle3.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
dataGridViewCellStyle3.ForeColor = System.Drawing.SystemColors.WindowText;
dataGridViewCellStyle3.SelectionBackColor = System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle3.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
dataGridViewCellStyle3.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
this.DataGridView.RowHeadersDefaultCellStyle = dataGridViewCellStyle3;
dataGridViewCellStyle6.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
dataGridViewCellStyle6.BackColor = System.Drawing.SystemColors.Control;
dataGridViewCellStyle6.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
dataGridViewCellStyle6.ForeColor = System.Drawing.SystemColors.WindowText;
dataGridViewCellStyle6.SelectionBackColor = System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle6.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
dataGridViewCellStyle6.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
this.DataGridView.RowHeadersDefaultCellStyle = dataGridViewCellStyle6;
this.DataGridView.RowHeadersWidth = 51;
this.DataGridView.RowTemplate.Height = 25;
this.DataGridView.Size = new System.Drawing.Size(641, 429);
@ -193,16 +196,34 @@
//
// DateOpenPicker
//
this.DateOpenPicker.Location = new System.Drawing.Point(471, 8);
this.DateOpenPicker.Location = new System.Drawing.Point(495, 12);
this.DateOpenPicker.Name = "DateOpenPicker";
this.DateOpenPicker.Size = new System.Drawing.Size(250, 27);
this.DateOpenPicker.TabIndex = 6;
//
// FullnessnumericUpDown
//
this.FullnessnumericUpDown.Location = new System.Drawing.Point(595, 57);
this.FullnessnumericUpDown.Name = "FullnessnumericUpDown";
this.FullnessnumericUpDown.Size = new System.Drawing.Size(150, 27);
this.FullnessnumericUpDown.TabIndex = 7;
//
// Fullness
//
this.Fullness.AutoSize = true;
this.Fullness.Location = new System.Drawing.Point(469, 64);
this.Fullness.Name = "Fullness";
this.Fullness.Size = new System.Drawing.Size(103, 20);
this.Fullness.TabIndex = 8;
this.Fullness.Text = "Ограничение";
//
// FormShopCar
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(853, 664);
this.Controls.Add(this.Fullness);
this.Controls.Add(this.FullnessnumericUpDown);
this.Controls.Add(this.DateOpenPicker);
this.Controls.Add(this.ComponentsGroupBox);
this.Controls.Add(this.AdressTextBox);
@ -215,6 +236,7 @@
this.Load += new System.EventHandler(this.FormCar_Load);
this.ComponentsGroupBox.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.DataGridView)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.FullnessnumericUpDown)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
@ -235,5 +257,7 @@
private DataGridViewTextBoxColumn ComponentNameField;
private DataGridViewTextBoxColumn CountField;
private DateTimePicker DateOpenPicker;
private NumericUpDown FullnessnumericUpDown;
private Label Fullness;
}
}

View File

@ -110,6 +110,7 @@ namespace AutomobilePlant
ShopName = ShopNameTextBox.Text,
DateOpen = DateOpenPicker.Value,
Adress = AdressTextBox.Text,
Fullness = (int)FullnessnumericUpDown.Value,
Cars = _cars
};
var operationResult = _id.HasValue ? _logic.Update(model) :

View File

@ -37,7 +37,7 @@ namespace AutomobilePlant
services.AddTransient<IComponentStorage, ComponentStorage>();
services.AddTransient<IOrderStorage, OrderStorage>();
services.AddTransient<ICarStorage, CarStorage>();
services.AddTransient<ICarShopStorage, CarShopStorege>();
services.AddTransient<ICarShopStorage, CarShopStorage>();
services.AddTransient<IComponentLogic, ComponentLogic>();
services.AddTransient<IOrderLogic, OrderLogic>();

View File

@ -14,9 +14,11 @@ namespace AutomomilePlantFileImplement
private readonly string ComponentFileName = "Component.xml";
private readonly string OrderFileName = "Order.xml";
private readonly string CarFileName = "Car.xml";
private readonly string CarShopFileName = "CarShop.xml";
public List<Component> Components { get; private set; }
public List<Order> Orders { get; private set; }
public List<Car> Cars { get; private set; }
public List<CarShop> CarShops { get; private set; }
public static DataFileSingleton GetInstance()
{
if (instance == null)
@ -29,6 +31,8 @@ namespace AutomomilePlantFileImplement
"Components", x => x.GetXElement);
public void SaveCars() => SaveData(Cars, CarFileName,
"Cars", x => x.GetXElement);
public void SaveCarShops() => SaveData(CarShops, CarShopFileName,
"CarShops", x => x.GetXElement);
public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x
=> x.GetXElement);
private DataFileSingleton()
@ -38,6 +42,7 @@ namespace AutomomilePlantFileImplement
Cars = LoadData(CarFileName, "Car", x =>
Car.Create(x)!)!;
Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!;
CarShops = LoadData(CarShopFileName, "CarShop", x => CarShop.Create(x)!)!;
}
private static List<T>? LoadData<T>(string filename, string xmlNodeName,
Func<XElement, T> selectFunction)

View File

@ -0,0 +1,96 @@
using AutomobilePlantContracts.BindingModels;
using AutomobilePlantContracts.SearchModel;
using AutomobilePlantContracts.StoragesContracts;
using AutomobilePlantContracts.ViewModel;
using AutomomilePlantFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace AutomomilePlantFileImplement.Implements
{
public class CarShopStorage : ICarShopStorage
{
private readonly DataFileSingleton source;
public CarShopStorage()
{
source = DataFileSingleton.GetInstance();
}
public CarShopViewModel? Delete(CarShopBindingModel model)
{
var element = source.CarShops.FirstOrDefault(x => x.Id ==
model.Id);
if (element != null)
{
source.CarShops.Remove(element);
source.SaveCarShops();
return element.GetViewModel;
}
return null;
}
public CarShopViewModel? GetElement(CarShopSearchModel model)
{
if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue)
{
return null;
}
return source.CarShops
.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.ShopName) && x.ShopName ==
model.ShopName) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public List<CarShopViewModel> GetFilteredList(CarShopSearchModel model)
{
if (string.IsNullOrEmpty(model.ShopName))
{
return new();
}
return source.CarShops
.Where(x => x.ShopName.Contains(model.ShopName))
.Select(x => x.GetViewModel)
.ToList();
}
public List<CarShopViewModel> GetFullList()
{
return source.CarShops
.Select(x => x.GetViewModel)
.ToList();
}
public CarShopViewModel? Insert(CarShopBindingModel model)
{
model.Id = source.CarShops.Count > 0 ? source.CarShops.Max(x =>
x.Id) + 1 : 1;
var newCarShop = CarShop.Create(model);
if (newCarShop == null)
{
return null;
}
source.CarShops.Add(newCarShop);
source.SaveCarShops();
return newCarShop.GetViewModel;
}
public CarShopViewModel? Update(CarShopBindingModel model)
{
var shop = source.CarShops.FirstOrDefault(x => x.Id ==
model.Id);
if (shop == null)
{
return null;
}
shop.Update(model);
source.SaveCarShops();
return shop.GetViewModel;
}
}
}

View File

@ -0,0 +1,116 @@
using AutomobilePlantContracts.BindingModels;
using AutomobilePlantContracts.ViewModel;
using AutomobilePlantDataModels.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace AutomomilePlantFileImplement.Models
{
public class CarShop : ICarShop
{
public string ShopName {get; set;} = String.Empty;
public string Adress { get; set; } = String.Empty;
public DateTime DateOpen { get; set; }
public int Fullness { get; set; }
public Dictionary<int, int> ShopCars { get; set; } = new();
private Dictionary<int, (ICarModel, int)> _cars = null;
public int Id { get; set; }
public Dictionary<int, (ICarModel, int)> Cars
{
get
{
if (_cars == null)
{
var source = DataFileSingleton.GetInstance();
_cars = ShopCars.ToDictionary(x => x.Key, y =>
((source.Cars.FirstOrDefault(z => z.Id == y.Key) as ICarModel)!,
y.Value));
}
return _cars;
}
}
public static CarShop? Create(CarShopBindingModel model)
{
if (model == null)
{
return null;
}
return new CarShop()
{
Id = model.Id,
ShopName = model.ShopName,
Fullness = model.Fullness,
Adress = model.Adress,
DateOpen = model.DateOpen,
ShopCars = model.Cars.ToDictionary(x => x.Key, x
=> x.Value.Item2)
};
}
public static CarShop? Create(XElement element)
{
if (element == null)
{
return null;
}
return new CarShop()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
ShopName = element.Element("ShopName")!.Value,
Adress = element.Element("Adress")!.Value,
DateOpen = DateTime.Parse(element.Element("DateOpen")!.Value),
Fullness = Convert.ToInt32(element.Element("Fullness")!.Value),
ShopCars =
element.Element("Cars")!.Elements("ShopCar")
.ToDictionary(x =>
Convert.ToInt32(x.Element("Key")?.Value), x =>
Convert.ToInt32(x.Element("Value")?.Value))
};
}
public void Update(CarShopBindingModel model)
{
if (model == null)
{
return;
}
ShopName = model.ShopName;
Adress = model.Adress;
ShopCars = model.Cars.ToDictionary(x => x.Key, x =>
x.Value.Item2);
_cars = null;
}
public CarShopViewModel GetViewModel => new()
{
Id = Id,
ShopName = ShopName,
Adress = Adress,
DateOpen = DateOpen,
Fullness = Fullness,
Cars = Cars
};
public XElement GetXElement => new("CarShop",
new XAttribute("Id", Id),
new XElement("ShopName", ShopName),
new XElement("Adress", Adress),
new XElement("DateOpen", DateOpen.ToString()),
new XElement("Fullness", Fullness.ToString()),
new XElement("Cars", ShopCars.Select(x =>
new XElement("ShopCar",
new XElement("Key", x.Key),
new XElement("Value", x.Value)))
.ToArray()));
}
}