This commit is contained in:
Danil Markov 2023-04-21 14:32:11 +04:00
parent cb8fb410b3
commit 8d0be1216c
5 changed files with 383 additions and 381 deletions

View File

@ -3,50 +3,53 @@ using System.Xml.Linq;
namespace LawFirmFileImplement
{
internal class DataFileSingleton
{
private static DataFileSingleton? instance;
private readonly string BlankFileName = "Blank.xml";
private readonly string OrderFileName = "Order.xml";
private readonly string DocumentFileName = "Document.xml";
public List<Blank> Blanks { get; private set; }
public List<Order> Orders { get; private set; }
public List<Document> Documents { get; private set; }
public static DataFileSingleton GetInstance()
{
if (instance == null)
{
instance = new DataFileSingleton();
}
return instance;
}
public void SaveBlanks() => SaveData(Blanks, BlankFileName, "Blanks", x => x.GetXElement);
public void SaveDocuments() => SaveData(Documents, DocumentFileName, "Documents", x => x.GetXElement);
public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement);
private DataFileSingleton()
{
Blanks = LoadData(BlankFileName, "Blank", x => Blank.Create(x)!)!;
Documents = LoadData(DocumentFileName, "Document", x => Document.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);
}
}
}
}
internal class DataFileSingleton
{
private static DataFileSingleton? instance;
private readonly string BlankFileName = "Blank.xml";
private readonly string OrderFileName = "Order.xml";
private readonly string DocumentFileName = "Document.xml";
public List<Blank> Blanks { get; private set; }
public List<Order> Orders { get; private set; }
public List<Document> Documents { get; private set; }
public static DataFileSingleton GetInstance()
{
if (instance == null)
{
instance = new DataFileSingleton();
}
return instance;
}
public void SaveBlanks() => SaveData(Blanks, BlankFileName, "Blanks", x => x.GetXElement);
public void SaveDocuments() => SaveData(Documents, DocumentFileName, "Documents", x => x.GetXElement);
public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement);
private DataFileSingleton()
{
Blanks = LoadData(BlankFileName, "Blank", x => Blank.Create(x)!)!;
Documents = LoadData(DocumentFileName, "Document", x => Document.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

@ -6,69 +6,69 @@ using LawFirmFileImplement.Models;
namespace LawFirmFileImplement.Implements
{
public class DocumentStorage : IDocumentStorage
{
private readonly DataFileSingleton source;
public DocumentStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<DocumentViewModel> GetFullList()
{
return source.Documents.Select(x => x.GetViewModel).ToList();
}
public List<DocumentViewModel> GetFilteredList(DocumentSearchModel model)
{
if (string.IsNullOrEmpty(model.DocumentName))
{
return new();
}
return source.Documents.Where(x =>
x.DocumentName.Contains(model.DocumentName)).Select(x => x.GetViewModel).ToList();
}
public DocumentViewModel? GetElement(DocumentSearchModel model)
{
if (string.IsNullOrEmpty(model.DocumentName) && !model.Id.HasValue)
{
return null;
}
return source.Documents.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.DocumentName) && x.DocumentName ==
model.DocumentName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public DocumentViewModel? Insert(DocumentBindingModel model)
{
model.Id = source.Documents.Count > 0 ? source.Documents.Max(x => x.Id) + 1 : 1;
var newDocument = Document.Create(model);
if (newDocument == null)
{
return null;
}
source.Documents.Add(newDocument);
source.SaveDocuments();
return newDocument.GetViewModel;
}
public DocumentViewModel? Update(DocumentBindingModel model)
{
var document = source.Documents.FirstOrDefault(x => x.Id == model.Id);
if (document == null)
{
return null;
}
document.Update(model);
source.SaveDocuments();
return document.GetViewModel;
}
public DocumentViewModel? Delete(DocumentBindingModel model)
{
var element = source.Documents.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
source.Documents.Remove(element);
source.SaveDocuments();
return element.GetViewModel;
}
return null;
}
}
}
public class DocumentStorage : IDocumentStorage
{
private readonly DataFileSingleton source;
public DocumentStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<DocumentViewModel> GetFullList()
{
return source.Documents.Select(x => x.GetViewModel).ToList();
}
public List<DocumentViewModel> GetFilteredList(DocumentSearchModel model)
{
if (string.IsNullOrEmpty(model.DocumentName))
{
return new();
}
return source.Documents.Where(x =>
x.DocumentName.Contains(model.DocumentName)).Select(x => x.GetViewModel).ToList();
}
public DocumentViewModel? GetElement(DocumentSearchModel model)
{
if (string.IsNullOrEmpty(model.DocumentName) && !model.Id.HasValue)
{
return null;
}
return source.Documents.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.DocumentName) && x.DocumentName ==
model.DocumentName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public DocumentViewModel? Insert(DocumentBindingModel model)
{
model.Id = source.Documents.Count > 0 ? source.Documents.Max(x => x.Id) + 1 : 1;
var newDocument = Document.Create(model);
if (newDocument == null)
{
return null;
}
source.Documents.Add(newDocument);
source.SaveDocuments();
return newDocument.GetViewModel;
}
public DocumentViewModel? Update(DocumentBindingModel model)
{
var document = source.Documents.FirstOrDefault(x => x.Id == model.Id);
if (document == null)
{
return null;
}
document.Update(model);
source.SaveDocuments();
return document.GetViewModel;
}
public DocumentViewModel? Delete(DocumentBindingModel model)
{
var element = source.Documents.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
source.Documents.Remove(element);
source.SaveDocuments();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -6,78 +6,78 @@ using LawFirmFileImplement.Models;
namespace LawFirmFileImplement.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;
}
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;
var document = source.Documents.FirstOrDefault(x => x.Id == order.DocumentId);
if (document != null)
{
viewModel.DocumentName = document.DocumentName;
}
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;
}
}
}
return source.Orders.FirstOrDefault(x =>
(model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
private OrderViewModel GetViewModel(Order order)
{
var viewModel = order.GetViewModel;
var document = source.Documents.FirstOrDefault(x => x.Id == order.DocumentId);
if (document != null)
{
viewModel.DocumentName = document.DocumentName;
}
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

@ -5,87 +5,86 @@ using System.Xml.Linq;
namespace LawFirmFileImplement.Models
{
public class Document : IDocumentModel
{
public int Id { get; private set; }
public string DocumentName { get; private set; } = string.Empty;
public double Price { get; private set; }
public Dictionary<int, int> Blanks { get; private set; } = new();
private Dictionary<int, (IBlankModel, int)>? _documentBlanks =
null;
public Dictionary<int, (IBlankModel, int)> DocumentBlanks
{
get
{
if (_documentBlanks == null)
{
var source = DataFileSingleton.GetInstance();
_documentBlanks = Blanks.ToDictionary(x => x.Key, y =>
((source.Blanks.FirstOrDefault(z => z.Id == y.Key) as IBlankModel)!,
y.Value));
}
return _documentBlanks;
}
}
public static Document? Create(DocumentBindingModel model)
{
if (model == null)
{
return null;
}
return new Document()
{
Id = model.Id,
DocumentName = model.DocumentName,
Price = model.Price,
Blanks = model.DocumentBlanks.ToDictionary(x => x.Key, x
=> x.Value.Item2)
};
}
public static Document? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Document()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
DocumentName = element.Element("DocumentName")!.Value,
Price = Convert.ToDouble(element.Element("Price")!.Value),
Blanks =
element.Element("DocumentBlanks")!.Elements("DocumentBlank")
.ToDictionary(x =>
Convert.ToInt32(x.Element("Key")?.Value), x =>
Convert.ToInt32(x.Element("Value")?.Value))
};
}
public void Update(DocumentBindingModel model)
{
if (model == null)
{
return;
}
DocumentName = model.DocumentName;
Price = model.Price;
Blanks = model.DocumentBlanks.ToDictionary(x => x.Key, x =>
x.Value.Item2);
_documentBlanks = null;
}
public DocumentViewModel GetViewModel => new()
{
Id = Id,
DocumentName = DocumentName,
Price = Price,
DocumentBlanks = DocumentBlanks
};
public XElement GetXElement => new("Document",
new XAttribute("Id", Id),
new XElement("DocumentName", DocumentName),
new XElement("Price", Price.ToString()),
new XElement("DocumentBlanks", Blanks.Select(x =>
new XElement("DocumentBlank",
new XElement("Key", x.Key),
new XElement("Value", x.Value))).ToArray()));
}
}
public class Document : IDocumentModel
{
public int Id { get; private set; }
public string DocumentName { get; private set; } = string.Empty;
public double Price { get; private set; }
public Dictionary<int, int> Blanks { get; private set; } = new();
private Dictionary<int, (IBlankModel, int)>? _documentBlanks = null;
public Dictionary<int, (IBlankModel, int)> DocumentBlanks
{
get
{
if (_documentBlanks == null)
{
var source = DataFileSingleton.GetInstance();
_documentBlanks = Blanks.ToDictionary(x => x.Key, y =>
((source.Blanks.FirstOrDefault(z => z.Id == y.Key) as IBlankModel)!,
y.Value));
}
return _documentBlanks;
}
}
public static Document? Create(DocumentBindingModel model)
{
if (model == null)
{
return null;
}
return new Document()
{
Id = model.Id,
DocumentName = model.DocumentName,
Price = model.Price,
Blanks = model.DocumentBlanks.ToDictionary(x => x.Key, x
=> x.Value.Item2)
};
}
public static Document? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Document()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
DocumentName = element.Element("DocumentName")!.Value,
Price = Convert.ToDouble(element.Element("Price")!.Value),
Blanks =
element.Element("DocumentBlanks")!.Elements("DocumentBlank")
.ToDictionary(x =>
Convert.ToInt32(x.Element("Key")?.Value), x =>
Convert.ToInt32(x.Element("Value")?.Value))
};
}
public void Update(DocumentBindingModel model)
{
if (model == null)
{
return;
}
DocumentName = model.DocumentName;
Price = model.Price;
Blanks = model.DocumentBlanks.ToDictionary(x => x.Key, x =>
x.Value.Item2);
_documentBlanks = null;
}
public DocumentViewModel GetViewModel => new()
{
Id = Id,
DocumentName = DocumentName,
Price = Price,
DocumentBlanks = DocumentBlanks
};
public XElement GetXElement => new("Document",
new XAttribute("Id", Id),
new XElement("DocumentName", DocumentName),
new XElement("Price", Price.ToString()),
new XElement("DocumentBlanks", Blanks.Select(x =>
new XElement("DocumentBlank",
new XElement("Key", x.Key),
new XElement("Value", x.Value))).ToArray()));
}
}

View File

@ -5,113 +5,113 @@ using Microsoft.Extensions.Logging;
namespace LawFirmView
{
public partial class FormCreateOrder : Form
{
private readonly ILogger _logger;
private readonly IDocumentLogic _logicD;
private readonly IOrderLogic _logicO;
public FormCreateOrder(ILogger<FormCreateOrder> logger, IDocumentLogic logicD, IOrderLogic logicO)
{
InitializeComponent();
_logger = logger;
_logicD = logicD;
_logicO = logicO;
}
private void FormCreateOrder_Load(object sender, EventArgs e)
{
_logger.LogInformation("Загрузка документов для заказа");
try
{
var list = _logicD.ReadList(null);
if (list != null)
{
comboBoxDocument.DisplayMember = "DocumentName";
comboBoxDocument.ValueMember = "Id";
comboBoxDocument.DataSource = list;
comboBoxDocument.SelectedItem = null;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки документов");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void CalcSum()
{
if (comboBoxDocument.SelectedValue != null &&
!string.IsNullOrEmpty(textBoxCount.Text))
{
try
{
int id = Convert.ToInt32(comboBoxDocument.SelectedValue);
var document = _logicD.ReadElement(new DocumentSearchModel
{
Id = id
});
int count = Convert.ToInt32(textBoxCount.Text);
textBoxSum.Text = Math.Round(count * (document?.Price ?? 0), 2).ToString();
_logger.LogInformation("Расчет суммы заказа");
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка расчета суммы заказа");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
private void TextBoxCount_TextChanged(object sender, EventArgs e)
{
CalcSum();
}
private void ComboBoxDocument_SelectedIndexChanged(object sender, EventArgs e)
{
CalcSum();
}
private void ButtonSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxCount.Text))
{
MessageBox.Show("Заполните поле Количество", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (comboBoxDocument.SelectedValue == null)
{
MessageBox.Show("Выберите документ", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_logger.LogInformation("Создание заказа");
try
{
var operationResult = _logicO.CreateOrder(new OrderBindingModel
{
DocumentId = Convert.ToInt32(comboBoxDocument.SelectedValue),
Count = Convert.ToInt32(textBoxCount.Text),
Sum = Convert.ToDouble(textBoxSum.Text)
});
if (!operationResult)
{
throw new Exception("Ошибка при создании заказа. Дополнительная информация в логах.");
}
MessageBox.Show("Сохранение прошло успешно", "Сообщение",
MessageBoxButtons.OK, MessageBoxIcon.Information);
DialogResult = DialogResult.OK;
Close();
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания заказа");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
}
public partial class FormCreateOrder : Form
{
private readonly ILogger _logger;
private readonly IDocumentLogic _logicD;
private readonly IOrderLogic _logicO;
public FormCreateOrder(ILogger<FormCreateOrder> logger, IDocumentLogic logicD, IOrderLogic logicO)
{
InitializeComponent();
_logger = logger;
_logicD = logicD;
_logicO = logicO;
}
private void FormCreateOrder_Load(object sender, EventArgs e)
{
_logger.LogInformation("Загрузка документов для заказа");
try
{
var list = _logicD.ReadList(null);
if (list != null)
{
comboBoxDocument.DisplayMember = "DocumentName";
comboBoxDocument.ValueMember = "Id";
comboBoxDocument.DataSource = list;
comboBoxDocument.SelectedItem = null;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки документов");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void CalcSum()
{
if (comboBoxDocument.SelectedValue != null &&
!string.IsNullOrEmpty(textBoxCount.Text))
{
try
{
int id = Convert.ToInt32(comboBoxDocument.SelectedValue);
var document = _logicD.ReadElement(new DocumentSearchModel
{
Id = id
});
int count = Convert.ToInt32(textBoxCount.Text);
textBoxSum.Text = Math.Round(count * (document?.Price ?? 0), 2).ToString();
_logger.LogInformation("Расчет суммы заказа");
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка расчета суммы заказа");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
private void TextBoxCount_TextChanged(object sender, EventArgs e)
{
CalcSum();
}
private void ComboBoxDocument_SelectedIndexChanged(object sender, EventArgs e)
{
CalcSum();
}
private void ButtonSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxCount.Text))
{
MessageBox.Show("Заполните поле Количество", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (comboBoxDocument.SelectedValue == null)
{
MessageBox.Show("Выберите документ", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_logger.LogInformation("Создание заказа");
try
{
var operationResult = _logicO.CreateOrder(new OrderBindingModel
{
DocumentId = Convert.ToInt32(comboBoxDocument.SelectedValue),
Count = Convert.ToInt32(textBoxCount.Text),
Sum = Convert.ToDouble(textBoxSum.Text)
});
if (!operationResult)
{
throw new Exception("Ошибка при создании заказа. Дополнительная информация в логах.");
}
MessageBox.Show("Сохранение прошло успешно", "Сообщение",
MessageBoxButtons.OK, MessageBoxIcon.Information);
DialogResult = DialogResult.OK;
Close();
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания заказа");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
}