File implement

This commit is contained in:
ShabOl 2024-04-11 00:57:19 +04:00
parent edfe10dd44
commit 3e7ccbdc62
5 changed files with 179 additions and 3 deletions

View File

@ -10,18 +10,22 @@ namespace AutoWorkshopFileImplement
private readonly string ComponentFileName = "Component.xml";
private readonly string OrderFileName = "Order.xml";
private readonly string RepairFileName = "Repair.xml";
private readonly string ClientFileName = "Client.xml";
public List<Component> Components { get; private set; }
public List<Order> Orders { get; private set; }
public List<Repair> Repairs { get; private set; }
public List<Client> Clients { get; private set; }
private DataFileSingleton()
{
Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!;
Repairs = LoadData(RepairFileName, "Repair", x => Repair.Create(x)!)!;
Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!;
Clients = LoadData(ClientFileName, "Client", x => Client.Create(x)!)!;
}
public static DataFileSingleton GetInstance()
@ -37,6 +41,7 @@ namespace AutoWorkshopFileImplement
public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement);
public void SaveRepairs() => SaveData(Repairs, RepairFileName, "Repairs", x => x.GetXElement);
public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement);
public void SaveClients() => SaveData(Clients, ClientFileName, "Clients", x => x.GetXElement);
private static List<T>? LoadData<T>(string FileName, string XmlNodeName, Func<XElement, T> SelectFunction)
{

View File

@ -0,0 +1,84 @@
using AutoWorkshopContracts.BindingModels;
using AutoWorkshopContracts.SearchModels;
using AutoWorkshopContracts.StoragesContracts;
using AutoWorkshopContracts.ViewModels;
using AutoWorkshopFileImplement.Models;
namespace AutoWorkshopFileImplement.Implements
{
public class ClientStorage : IClientStorage
{
private readonly DataFileSingleton _source;
public ClientStorage()
{
_source = DataFileSingleton.GetInstance();
}
public List<ClientViewModel> GetFullList()
{
return _source.Clients
.Select(x => x.GetViewModel)
.ToList();
}
public List<ClientViewModel> GetFilteredList(ClientSearchModel Model)
{
if (string.IsNullOrEmpty(Model.ClientFIO) && string.IsNullOrEmpty(Model.Email) && string.IsNullOrEmpty(Model.Password))
return new();
return _source.Clients
.Where(x => (string.IsNullOrEmpty(Model.ClientFIO) || x.ClientFIO.Contains(Model.ClientFIO) &&
(string.IsNullOrEmpty(Model.Email) || x.Email.Contains(Model.Email)) &&
(string.IsNullOrEmpty(Model.Password) || x.Password.Contains(Model.Password))))
.Select(x => x.GetViewModel)
.ToList();
}
public ClientViewModel? GetElement(ClientSearchModel Model)
{
return _source.Clients
.FirstOrDefault(x => (string.IsNullOrEmpty(Model.ClientFIO) || x.ClientFIO == Model.ClientFIO) &&
(!Model.Id.HasValue || x.Id == Model.Id) && (string.IsNullOrEmpty(Model.Email) || x.Email == Model.Email) &&
(string.IsNullOrEmpty(Model.Password) || x.Password == Model.Password))?
.GetViewModel;
}
public ClientViewModel? Insert(ClientBindingModel Model)
{
Model.Id = _source.Clients.Count > 0 ? _source.Clients.Max(x => x.Id) + 1 : 1;
var NewClient = Client.Create(Model);
if (NewClient == null)
return null;
_source.Clients.Add(NewClient);
_source.SaveClients();
return NewClient.GetViewModel;
}
public ClientViewModel? Update(ClientBindingModel Model)
{
var Client = _source.Clients.FirstOrDefault(x => x.Id == Model.Id);
if (Client == null)
return null;
Client.Update(Model);
_source.SaveClients();
return Client.GetViewModel;
}
public ClientViewModel? Delete(ClientBindingModel Model)
{
var Client = _source.Clients.FirstOrDefault(rec => rec.Id == Model.Id);
if (Client == null)
return null;
_source.Clients.Remove(Client);
_source.SaveClients();
return Client.GetViewModel;
}
}
}

View File

@ -26,7 +26,8 @@ namespace AutoWorkshopFileImplement.Implements
return new();
return _source.Orders
.Where(x => x.DateCreate >= Model.DateFrom && x.DateCreate <= Model.DateTo)
.Where(x => x.DateCreate >= Model.DateFrom && x.DateCreate <= Model.DateTo &&
(!Model.ClientId.HasValue || x.ClientId == Model.ClientId))
.Select(x => AddRepairName(x.GetViewModel))
.ToList();
}
@ -94,5 +95,13 @@ namespace AutoWorkshopFileImplement.Implements
Model.RepairName = SelectedRepair?.RepairName ?? string.Empty;
return Model;
}
public OrderViewModel AddClientFIO(OrderViewModel Model)
{
var SelectedClient = _source.Clients.FirstOrDefault(x => x.Id == Model.Id);
Model.ClientFIO = SelectedClient?.ClientFIO ?? string.Empty;
return Model;
}
}
}

View File

@ -0,0 +1,72 @@
using AutoWorkshopContracts.BindingModels;
using AutoWorkshopContracts.ViewModels;
using AutoWorkshopDataModels.Models;
using System.Xml.Linq;
namespace AutoWorkshopFileImplement.Models
{
public class Client : IClientModel
{
public int Id { get; private set; }
public string ClientFIO { get; private set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;
public static Client? Create(ClientBindingModel Model)
{
if (Model == null)
return null;
return new Client()
{
Id = Model.Id,
ClientFIO = Model.ClientFIO,
Email = Model.Email,
Password = Model.Password,
};
}
public static Client? Create(XElement Element)
{
if (Element == null)
return null;
return new Client()
{
Id = Convert.ToInt32(Element.Attribute("Id")!.Value),
ClientFIO = Element.Element("ClientFIO")!.Value,
Email = Element.Element("Email")!.Value,
Password = Element.Element("Password")!.Value,
};
}
public void Update(ClientBindingModel Model)
{
if (Model == null)
return;
ClientFIO = Model.ClientFIO;
Email = Model.Email;
Password = Model.Password;
}
public ClientViewModel GetViewModel => new()
{
Id = Id,
ClientFIO = ClientFIO,
Email = Email,
Password = Password
};
public XElement GetXElement => new(
"Client",
new XAttribute("Id", Id),
new XElement("ClientFIO", ClientFIO),
new XElement("Email", Email.ToString()),
new XElement("Password", Password.ToString())
);
}
}

View File

@ -12,6 +12,8 @@ namespace AutoWorkshopFileImplement.Models
public int RepairId { get; private set; }
public int ClientId { get; private set; }
public int Count { get; private set; }
public double Sum { get; private set; }
@ -31,6 +33,7 @@ namespace AutoWorkshopFileImplement.Models
{
Id = Model.Id,
RepairId = Model.RepairId,
ClientId = Model.ClientId,
Count = Model.Count,
Sum = Model.Sum,
Status = Model.Status,
@ -48,6 +51,7 @@ namespace AutoWorkshopFileImplement.Models
{
Id = Convert.ToInt32(Element.Attribute("Id")!.Value),
RepairId = Convert.ToInt32(Element.Element("RepairId")!.Value),
ClientId = Convert.ToInt32(Element.Element("ClientId")!.Value),
Count = Convert.ToInt32(Element.Element("Count")!.Value),
Sum = Convert.ToDouble(Element.Element("Sum")!.Value),
Status = (OrderStatus)Enum.Parse(typeof(OrderStatus), Element.Element("Status")!.Value),
@ -69,6 +73,7 @@ namespace AutoWorkshopFileImplement.Models
{
Id = Id,
RepairId = RepairId,
ClientId = ClientId,
Count = Count,
Sum = Sum,
Status = Status,
@ -80,6 +85,7 @@ namespace AutoWorkshopFileImplement.Models
"Order",
new XAttribute("Id", Id),
new XElement("RepairId", RepairId),
new XElement("ClientId", ClientId),
new XElement("Count", Count.ToString()),
new XElement("Sum", Sum.ToString()),
new XElement("Status", Status.ToString()),