ListImplement

This commit is contained in:
ShabOl 2024-04-16 19:50:39 +04:00
parent 1e49ce1e21
commit 6700d24634
5 changed files with 205 additions and 4 deletions

View File

@ -11,6 +11,7 @@ namespace AutoWorkshopFileImplement
private readonly string OrderFileName = "Order.xml";
private readonly string RepairFileName = "Repair.xml";
private readonly string ClientFileName = "Client.xml";
private readonly string ImplementerFileName = "Implementer.xml";
public List<Component> Components { get; private set; }
@ -20,12 +21,15 @@ namespace AutoWorkshopFileImplement
public List<Client> Clients { get; private set; }
public List<Implementer> Implementers { 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)!)!;
Implementers = LoadData(ImplementerFileName, "Implementer", x => Implementer.Create(x)!)!;
}
public static DataFileSingleton GetInstance()
@ -42,6 +46,7 @@ namespace AutoWorkshopFileImplement
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);
public void SaveImplementers() => SaveData(Implementers, ImplementerFileName, "Implementers", x => x.GetXElement);
private static List<T>? LoadData<T>(string FileName, string XmlNodeName, Func<XElement, T> SelectFunction)
{

View File

@ -0,0 +1,97 @@
using AutoWorkshopContracts.BindingModels;
using AutoWorkshopContracts.SearchModels;
using AutoWorkshopContracts.StoragesContracts;
using AutoWorkshopContracts.ViewModels;
using AutoWorkshopFileImplement.Models;
namespace AutoWorkshopFileImplement.Implements
{
public class ImplementerStorage : IImplementerStorage
{
private readonly DataFileSingleton _source;
public ImplementerStorage()
{
_source = DataFileSingleton.GetInstance();
}
public List<ImplementerViewModel> GetFullList()
{
return _source.Implementers.Select(x => x.GetViewModel).ToList();
}
public List<ImplementerViewModel> GetFilteredList(ImplementerSearchModel Model)
{
if (Model == null)
return new();
if (Model.Id.HasValue)
{
var Res = GetElement(Model);
return Res != null ? new() { Res } : new();
}
if (Model.ImplementerFIO != null)
{
return _source.Implementers
.Where(x => x.ImplementerFIO.Equals(Model.ImplementerFIO))
.Select(x => x.GetViewModel)
.ToList();
}
return new();
}
public ImplementerViewModel? GetElement(ImplementerSearchModel Model)
{
if (Model.Id.HasValue)
return _source.Implementers.FirstOrDefault(x => x.Id == Model.Id)?.GetViewModel;
if (Model.ImplementerFIO != null && Model.Password != null)
return _source.Implementers.FirstOrDefault(x => x.ImplementerFIO.Equals(Model.ImplementerFIO) && x.Password.Equals(Model.Password))?.GetViewModel;
if (Model.ImplementerFIO != null)
return _source.Implementers.FirstOrDefault(x => x.ImplementerFIO.Equals(Model.ImplementerFIO))?.GetViewModel;
return null;
}
public ImplementerViewModel? Insert(ImplementerBindingModel Model)
{
Model.Id = _source.Implementers.Count > 0 ? _source.Implementers.Max(x => x.Id) + 1 : 1;
var Res = Implementer.Create(Model);
if (Res != null)
{
_source.Implementers.Add(Res);
_source.SaveImplementers();
}
return Res?.GetViewModel;
}
public ImplementerViewModel? Update(ImplementerBindingModel Model)
{
var Res = _source.Implementers.FirstOrDefault(x => x.Id == Model.Id);
if (Res != null)
{
Res.Update(Model);
_source.SaveImplementers();
}
return Res?.GetViewModel;
}
public ImplementerViewModel? Delete(ImplementerBindingModel Model)
{
var Res = _source.Implementers.FirstOrDefault(x => x.Id == Model.Id);
if (Res != null)
{
_source.Implementers.Remove(Res);
_source.SaveImplementers();
}
return Res?.GetViewModel;
}
}
}

View File

@ -26,14 +26,16 @@ namespace AutoWorkshopFileImplement.Implements
if (Model.DateFrom.HasValue)
{
return _source.Orders
.Where(x => (x.DateCreate >= Model.DateFrom && x.DateCreate <= Model.DateTo) && (!Model.ClientId.HasValue || x.ClientId == Model.ClientId))
.Select(x => AddClientFIO(AddRepairName(x.GetViewModel)))
.Where(x => (x.DateCreate >= Model.DateFrom && x.DateCreate <= Model.DateTo) && (!Model.ClientId.HasValue || x.ClientId == Model.ClientId)
&& (!Model.ImplementerId.HasValue || x.ImplementerId == Model.ImplementerId))
.Select(x => AddImplementerFIO(AddClientFIO(AddRepairName(x.GetViewModel))))
.ToList();
}
return _source.Orders
.Where(x => (Model.Id.HasValue && x.Id == Model.Id) || (Model.ClientId.HasValue && x.ClientId == Model.ClientId))
.Select(x => AddClientFIO(AddRepairName(x.GetViewModel)))
.Where(x => (Model.Id.HasValue && x.Id == Model.Id) || (Model.ClientId.HasValue && x.ClientId == Model.ClientId)
|| (Model.ImplementerId.HasValue && x.ImplementerId == Model.ImplementerId))
.Select(x => AddImplementerFIO(AddClientFIO(AddRepairName(x.GetViewModel))))
.ToList();
}
@ -108,5 +110,16 @@ namespace AutoWorkshopFileImplement.Implements
Model.ClientFIO = SelectedClient?.ClientFIO ?? string.Empty;
return Model;
}
public OrderViewModel AddImplementerFIO(OrderViewModel Model)
{
var SelectedImplementer = _source.Implementers.FirstOrDefault(x => x.Id == Model.ImplementerId);
if (SelectedImplementer != null)
{
Model.ImplementerFIO = SelectedImplementer.ImplementerFIO;
}
return Model;
}
}
}

View File

@ -0,0 +1,79 @@
using AutoWorkshopContracts.BindingModels;
using AutoWorkshopContracts.ViewModels;
using AutoWorkshopDataModels.Models;
using System.Xml.Linq;
namespace AutoWorkshopFileImplement.Models
{
public class Implementer : IImplementerModel
{
public int Id { get; private set; }
public string ImplementerFIO { get; private set; } = string.Empty;
public string Password { get; private set; } = string.Empty;
public int WorkExperience { get; private set; }
public int Qualification { get; private set; }
public static Implementer? Create(ImplementerBindingModel Model)
{
if (Model == null)
return null;
return new()
{
Id = Model.Id,
ImplementerFIO = Model.ImplementerFIO,
Password = Model.Password,
WorkExperience = Model.WorkExperience,
Qualification = Model.Qualification,
};
}
public static Implementer? Create(XElement Element)
{
if (Element == null)
return null;
return new()
{
Id = Convert.ToInt32(Element.Attribute("Id")!.Value),
ImplementerFIO = Element.Element("ImplementerFIO")!.Value,
Password = Element.Element("Password")!.Value,
WorkExperience = Convert.ToInt32(Element.Element("WorkExperience")!.Value),
Qualification = Convert.ToInt32(Element.Element("Qualification")!.Value),
};
}
public void Update(ImplementerBindingModel Model)
{
if (Model == null)
return;
ImplementerFIO = Model.ImplementerFIO;
Password = Model.Password;
WorkExperience = Model.WorkExperience;
Qualification = Model.Qualification;
}
public ImplementerViewModel GetViewModel => new()
{
Id = Id,
ImplementerFIO = ImplementerFIO,
Password = Password,
WorkExperience = WorkExperience,
Qualification = Qualification,
};
public XElement GetXElement => new(
"Implementer",
new XAttribute("Id", Id),
new XElement("ImplementerFIO", ImplementerFIO),
new XElement("Password", Password),
new XElement("WorkExperience", WorkExperience),
new XElement("Qualification", Qualification)
);
}
}

View File

@ -14,6 +14,8 @@ namespace AutoWorkshopFileImplement.Models
public int ClientId { get; private set; }
public int? ImplementerId { get; set; }
public int Count { get; private set; }
public double Sum { get; private set; }
@ -34,6 +36,7 @@ namespace AutoWorkshopFileImplement.Models
Id = Model.Id,
RepairId = Model.RepairId,
ClientId = Model.ClientId,
ImplementerId = Model.ImplementerId,
Count = Model.Count,
Sum = Model.Sum,
Status = Model.Status,
@ -52,6 +55,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),
ImplementerId = Convert.ToInt32(Element.Element("ImplementerId")!.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),
@ -65,6 +69,7 @@ namespace AutoWorkshopFileImplement.Models
if (Model is null)
return;
ImplementerId = Model.ImplementerId;
Status = Model.Status;
DateImplement = Model.DateImplement;
}
@ -74,6 +79,7 @@ namespace AutoWorkshopFileImplement.Models
Id = Id,
RepairId = RepairId,
ClientId = ClientId,
ImplementerId = ImplementerId,
Count = Count,
Sum = Sum,
Status = Status,
@ -86,6 +92,7 @@ namespace AutoWorkshopFileImplement.Models
new XAttribute("Id", Id),
new XElement("RepairId", RepairId),
new XElement("ClientId", ClientId),
new XElement("ImplementerId", ImplementerId),
new XElement("Count", Count.ToString()),
new XElement("Sum", Sum.ToString()),
new XElement("Status", Status.ToString()),