fileimplement

This commit is contained in:
Мк Игорь 2023-05-02 21:53:34 +04:00
parent c2efaf9524
commit da2318fda7
7 changed files with 199 additions and 19 deletions

View File

@ -9,10 +9,12 @@ namespace BlacksmithWorkshopFileImplement
private readonly string OrderFileName = "Order.xml";
private readonly string ManufactureFileName = "Manufacture.xml";
private readonly string ClientFileName = "Client.xml";
public List<Component> Components { get; private set; }
private readonly string ImplementerFileName = "Implementer.xml";
public List<Component> Components { get; private set; }
public List<Order> Orders { get; private set; }
public List<Manufacture> Manufactures { get; private set; }
public List<Client> Clients { get; private set; }
public List<Implementer> Implementers { get; private set; }
public static DataFileSingleton GetInstance()
{
if (instance == null)
@ -25,13 +27,15 @@ namespace BlacksmithWorkshopFileImplement
public void SaveManufactures() => SaveData(Manufactures, ManufactureFileName, "Manufactures", x => x.GetXElement);
public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement);
public void SaveClients() => SaveData(Clients, ClientFileName, "Clients", x => x.GetXElement);
private DataFileSingleton()
public void SaveImplementers() => SaveData(Implementers, ImplementerFileName, "Implementer", x => x.GetXElement);
private DataFileSingleton()
{
Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!;
Manufactures = LoadData(ManufactureFileName, "Manufacture", x => Manufacture.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)!)!;
}
private static List<T>? LoadData<T>(string filename, string xmlNodeName, Func<XElement, T> selectFunction)
{
if (File.Exists(filename))

View File

@ -0,0 +1,88 @@
using BlacksmithWorkshopContracts.BindingModels;
using BlacksmithWorkshopContracts.SearchModels;
using BlacksmithWorkshopContracts.StoragesContracts;
using BlacksmithWorkshopContracts.ViewModels;
using BlacksmithWorkshopFileImplement.Models;
namespace BlacksmithWorkshopFileImplement.Implements
{
public class ImplementerStorage : IImplementerStorage
{
private readonly DataFileSingleton _source;
public ImplementerStorage()
{
_source = DataFileSingleton.GetInstance();
}
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;
}
public ImplementerViewModel? GetElement(ImplementerSearchModel model)
{
if (!model.Id.HasValue && (string.IsNullOrEmpty(model.ImplementerFIO) || string.IsNullOrEmpty(model.Password)))
{
return new();
}
if (model.Id.HasValue)//Сначала ищем по Id
{
return _source.Implementers
.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
}
return _source.Implementers//Затем по логину и паролю
.FirstOrDefault(x => x.ImplementerFIO == model.ImplementerFIO && x.Password == model.Password)?.GetViewModel;
}
public List<ImplementerViewModel> GetFilteredList(ImplementerSearchModel model)
{
if (!model.Id.HasValue && string.IsNullOrEmpty(model.ImplementerFIO))
{
return new();
}
if (model.Id.HasValue)
{
return _source.Implementers
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
return _source.Implementers
.Where(x => x.ImplementerFIO == model.ImplementerFIO)
.Select(x => x.GetViewModel)
.ToList();
}
public List<ImplementerViewModel> GetFullList()
{
return _source.Implementers.Select(x => x.GetViewModel).ToList();
}
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;
}
}
}

View File

@ -14,16 +14,17 @@ namespace BlacksmithWorkshopFileImplement.Implements
{
source = DataFileSingleton.GetInstance();
}
private OrderViewModel AddManufactureAndClientName (OrderViewModel model)
private OrderViewModel AddManufactureAndClientAndImplementerName (OrderViewModel model)
{
model.ManufactureName = source.Manufactures.SingleOrDefault(x => x.Id == model.ManufactureId)?.ManufactureName ?? string.Empty;
model.ClientFIO = source.Clients.SingleOrDefault(x => x.Id == model.ClientId)?.ClientFIO ?? string.Empty;
model.ImplementerFIO = source.Implementers.SingleOrDefault(x => x.Id == model.ImplementerId)?.ImplementerFIO ?? string.Empty;
return model;
}
public List<OrderViewModel> GetFullList()
{
return source.Orders
.Select(x => AddManufactureAndClientName(x.GetViewModel))
.Select(x => AddManufactureAndClientAndImplementerName(x.GetViewModel))
.ToList();
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
@ -36,21 +37,21 @@ namespace BlacksmithWorkshopFileImplement.Implements
{
return source.Orders
.Where(x => x.Id == model.Id)
.Select(x => AddManufactureAndClientName(x.GetViewModel))
.Select(x => AddManufactureAndClientAndImplementerName(x.GetViewModel))
.ToList();
}
else if (model.DateFrom.HasValue && model.DateTo.HasValue)//если Id в модели нет, ищем по датам
{
return source.Orders
.Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo)
.Select(x => AddManufactureAndClientName(x.GetViewModel))
.Select(x => AddManufactureAndClientAndImplementerName(x.GetViewModel))
.ToList();
}
else if (model.ClientId.HasValue)//далее ищем по Id клиента
{
return source.Orders
.Where(x => x.ClientId == model.ClientId)
.Select(x => AddManufactureAndClientName(x.GetViewModel))
.Select(x => AddManufactureAndClientAndImplementerName(x.GetViewModel))
.ToList();
}
return new();//если нет ни того, ни другого, ни третьего, возвращаем пустой список
@ -66,7 +67,7 @@ namespace BlacksmithWorkshopFileImplement.Implements
{
return null;
}
return AddManufactureAndClientName(order);
return AddManufactureAndClientAndImplementerName(order);
}
public OrderViewModel? Insert(OrderBindingModel model)
{

View File

@ -1,8 +1,71 @@
using BlacksmithWorkshopDataModels.Models;
using BlacksmithWorkshopContracts.BindingModels;
using BlacksmithWorkshopContracts.ViewModels;
using BlacksmithWorkshopDataModels.Models;
using System.Xml.Linq;
namespace BlacksmithWorkshopFileImplement.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(XElement element)
{
if (element == null)
{
return null;
}
return new()
{
ImplementerFIO = element.Element("FIO")!.Value,
Password = element.Element("Password")!.Value,
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
Qualification = Convert.ToInt32(element.Element("Qualification")!.Value),
WorkExperience = Convert.ToInt32(element.Element("WorkExperience")!.Value),
};
}
public static Implementer? Create(ImplementerBindingModel model)
{
if (model == null)
{
return null;
}
return new()
{
Id = model.Id,
Password = model.Password,
Qualification = model.Qualification,
ImplementerFIO = model.ImplementerFIO,
WorkExperience = model.WorkExperience,
};
}
public void Update(ImplementerBindingModel model)
{
if (model == null)
{
return;
}
Password = model.Password;
Qualification = model.Qualification;
ImplementerFIO = model.ImplementerFIO;
WorkExperience = model.WorkExperience;
}
public ImplementerViewModel GetViewModel => new()
{
Id = Id,
Password = Password,
Qualification = Qualification,
ImplementerFIO = ImplementerFIO,
};
public XElement GetXElement => new("Client",
new XAttribute("Id", Id),
new XElement("Password", Password),
new XElement("FIO", ImplementerFIO),
new XElement("Qualification", Qualification),
new XElement("WorkExperience", WorkExperience)
);
}
}

View File

@ -2,6 +2,7 @@
using BlacksmithWorkshopContracts.ViewModels;
using BlacksmithWorkshopDataModels.Enums;
using BlacksmithWorkshopDataModels.Models;
using System.Reflection;
using System.Xml.Linq;
namespace BlacksmithWorkshopFileImplement.Models
@ -16,8 +17,7 @@ namespace BlacksmithWorkshopFileImplement.Models
public DateTime DateCreate { get; private set; } = DateTime.Now;
public DateTime? DateImplement { get; private set; }
public int ClientId { get; private set; }
//TODO
public int? ImplementerId => throw new NotImplementedException();
public int? ImplementerId { get; set; }
public static Order? Create(OrderBindingModel? model)
{
if (model == null)
@ -33,8 +33,9 @@ namespace BlacksmithWorkshopFileImplement.Models
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
ClientId = model.ClientId
};
ClientId = model.ClientId,
ImplementerId = model.ImplementerId,
};
}
public static Order? Create(XElement element)
{
@ -57,7 +58,8 @@ namespace BlacksmithWorkshopFileImplement.Models
DateCreate = Convert.ToDateTime(element.Element("DateCreate")!.Value),
DateImplement = dateImplement,
ClientId = Convert.ToInt32(element.Element("ClientId")!.Value),
};
ImplementerId = Convert.ToInt32(element.Element("ImplementerId")!.Value),
};
}
public void Update(OrderBindingModel? model)
{
@ -66,6 +68,10 @@ namespace BlacksmithWorkshopFileImplement.Models
return;
}
Status = model.Status;
if (model.ImplementerId != null)
{
ImplementerId = model.ImplementerId;
}
if (model.DateImplement != null)
{
DateImplement = model.DateImplement;
@ -91,7 +97,8 @@ namespace BlacksmithWorkshopFileImplement.Models
new XElement("Status", (int)Status),
new XElement("DateCreate", DateCreate),
new XElement("DateImplement", DateImplement),
new XElement("ClientId", ClientId)
new XElement("ClientId", ClientId),
new XElement("ImplementerId", ImplementerId)
);
}
}

View File

@ -23,6 +23,8 @@ namespace BlacksmithWorkshopListImplement.Implements
vm.ManufactureName = manufacture?.ManufactureName ?? string.Empty;
var client = _source.Clients.Find(x => x.Id == order.ClientId);
vm.ClientFIO = client?.ClientFIO ?? string.Empty;
var implementer = _source.Implementers.Find(x => x.Id == order.ImplementerId);
vm.ImplementerFIO = implementer?.ImplementerFIO ?? string.Empty;
result.Add(vm);
}
return result;
@ -45,6 +47,8 @@ namespace BlacksmithWorkshopListImplement.Implements
vm.ManufactureName = manufacture?.ManufactureName ?? string.Empty;
var client = _source.Clients.Find(x => x.Id == order.ClientId);
vm.ClientFIO = client?.ClientFIO ?? string.Empty;
var implementer = _source.Implementers.Find(x => x.Id == order.ImplementerId);
vm.ImplementerFIO = implementer?.ImplementerFIO ?? string.Empty;
result.Add(vm);
}
}
@ -60,6 +64,8 @@ namespace BlacksmithWorkshopListImplement.Implements
vm.ManufactureName = manufacture?.ManufactureName ?? string.Empty;
var client = _source.Clients.Find(x => x.Id == order.ClientId);
vm.ClientFIO = client?.ClientFIO ?? string.Empty;
var implementer = _source.Implementers.Find(x => x.Id == order.ImplementerId);
vm.ImplementerFIO = implementer?.ImplementerFIO ?? string.Empty;
result.Add(vm);
}
}
@ -75,6 +81,8 @@ namespace BlacksmithWorkshopListImplement.Implements
vm.ManufactureName = manufacture?.ManufactureName ?? string.Empty;
var client = _source.Clients.Find(x => x.Id == order.ClientId);
vm.ClientFIO = client?.ClientFIO ?? string.Empty;
var implementer = _source.Implementers.Find(x => x.Id == order.ImplementerId);
vm.ImplementerFIO = implementer?.ImplementerFIO ?? string.Empty;
result.Add(vm);
}
}
@ -90,6 +98,8 @@ namespace BlacksmithWorkshopListImplement.Implements
vm.ManufactureName = manufacture?.ManufactureName ?? string.Empty;
var client = _source.Clients.Find(x => x.Id == order.ClientId);
vm.ClientFIO = client?.ClientFIO ?? string.Empty;
var implementer = _source.Implementers.Find(x => x.Id == order.ImplementerId);
vm.ImplementerFIO = implementer?.ImplementerFIO ?? string.Empty;
result.Add(vm);
}
}
@ -111,7 +121,9 @@ namespace BlacksmithWorkshopListImplement.Implements
vm.ManufactureName = manufacture?.ManufactureName ?? string.Empty;
var client = _source.Clients.Find(x => x.Id == order.ClientId);
vm.ClientFIO = client?.ClientFIO ?? string.Empty;
return vm;
var implementer = _source.Implementers.Find(x => x.Id == order.ImplementerId);
vm.ImplementerFIO = implementer?.ImplementerFIO ?? string.Empty;
return vm;
}
else if (model.ImplementerId.HasValue && order.ImplementerId == model.ImplementerId)
{
@ -120,6 +132,8 @@ namespace BlacksmithWorkshopListImplement.Implements
vm.ManufactureName = manufacture?.ManufactureName ?? string.Empty;
var client = _source.Clients.Find(x => x.Id == order.ClientId);
vm.ClientFIO = client?.ClientFIO ?? string.Empty;
var implementer = _source.Implementers.Find(x => x.Id == order.ImplementerId);
vm.ImplementerFIO = implementer?.ImplementerFIO ?? string.Empty;
return vm;
}
else if (model.ImplementerId.HasValue && !model.Status.HasValue)
@ -129,6 +143,8 @@ namespace BlacksmithWorkshopListImplement.Implements
vm.ManufactureName = manufacture?.ManufactureName ?? string.Empty;
var client = _source.Clients.Find(x => x.Id == order.ClientId);
vm.ClientFIO = client?.ClientFIO ?? string.Empty;
var implementer = _source.Implementers.Find(x => x.Id == order.ImplementerId);
vm.ImplementerFIO = implementer?.ImplementerFIO ?? string.Empty;
return vm;
}
else if (model.ImplementerId.HasValue && model.Status.HasValue)
@ -138,6 +154,8 @@ namespace BlacksmithWorkshopListImplement.Implements
vm.ManufactureName = manufacture?.ManufactureName ?? string.Empty;
var client = _source.Clients.Find(x => x.Id == order.ClientId);
vm.ClientFIO = client?.ClientFIO ?? string.Empty;
var implementer = _source.Implementers.Find(x => x.Id == order.ImplementerId);
vm.ImplementerFIO = implementer?.ImplementerFIO ?? string.Empty;
return vm;
}
}

View File

@ -60,8 +60,7 @@ namespace BlacksmithWorkshopListImplement.Models
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
ImplementerFIO = DataListSingleton.GetInstance().Implementers.SingleOrDefault(x => x.Id == ImplementerId)?.ImplementerFIO ?? string.Empty
DateImplement = DateImplement
};
}
}