ISEbd-21 Melnikov I. O. Lab Work 07 Base #24

Closed
Igor-Melnikov wants to merge 7 commits from lab7 into lab6
10 changed files with 430 additions and 26 deletions
Showing only changes of commit 9f49e41bb6 - Show all commits

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

@ -0,0 +1,71 @@
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

@ -9,13 +9,15 @@ namespace BlacksmithWorkshopListImplement
public List<Order> Orders { get; set; }
public List<Manufacture> Manufactures { get; set; }
public List<Client> Clients { get; set; }
private DataListSingleton()
public List<Implementer> Implementers { get; set; }
private DataListSingleton()
{
Components = new List<Component>();
Orders = new List<Order>();
Manufactures = new List<Manufacture>();
Clients = new List<Client>();
}
Implementers = new List<Implementer>();
}
public static DataListSingleton GetInstance()
{
if (_instance == null)

View File

@ -0,0 +1,119 @@
using BlacksmithWorkshopContracts.BindingModels;
using BlacksmithWorkshopContracts.SearchModels;
using BlacksmithWorkshopContracts.StoragesContracts;
using BlacksmithWorkshopContracts.ViewModels;
using BlacksmithWorkshopListImplement.Models;
namespace BlacksmithWorkshopListImplement.Implements
{
public class ImplementerStorage : IImplementerStorage
{
private readonly DataListSingleton _source;
public ImplementerStorage()
{
_source = DataListSingleton.GetInstance();
}
public ImplementerViewModel? Delete(ImplementerBindingModel model)
{
for (int i = 0; i < _source.Implementers.Count; ++i)
{
if (_source.Implementers[i].Id == model.Id)
{
var element = _source.Implementers[i];
_source.Implementers.RemoveAt(i);
return element.GetViewModel;
}
}
return null;
}
public ImplementerViewModel? GetElement(ImplementerSearchModel model)
{
foreach (var implementer in _source.Implementers)
{
if (model.Id.HasValue && implementer.Id == model.Id)
{
return implementer.GetViewModel;
}
if (model.ImplementerFIO != null && model.Password != null && implementer.ImplementerFIO.Equals(model.ImplementerFIO) && implementer.Password.Equals(model.Password))
{
return implementer.GetViewModel;
}
if (model.ImplementerFIO != null && implementer.ImplementerFIO.Equals(model.ImplementerFIO))
{
return implementer.GetViewModel;
}
}
return null;
}
public List<ImplementerViewModel> GetFilteredList(ImplementerSearchModel model)
{
var result = new List<ImplementerViewModel>();
if (!model.Id.HasValue && string.IsNullOrEmpty(model.ImplementerFIO))
{
return result;
}
if (model.Id.HasValue)
{
foreach (var implementer in _source.Implementers)
{
if (implementer.Id == model.Id)
{
result.Add(implementer.GetViewModel);
}
}
return result;
}
else
{
foreach (var implementer in _source.Implementers)
{
if (implementer.ImplementerFIO == model.ImplementerFIO)
{
result.Add(implementer.GetViewModel);
}
}
return result;
}
}
public List<ImplementerViewModel> GetFullList()
{
var result = new List<ImplementerViewModel>();
foreach (var implementer in _source.Implementers)
{
result.Add(implementer.GetViewModel);
}
return result;
}
public ImplementerViewModel? Insert(ImplementerBindingModel model)
{
model.Id = 1;
foreach (var implementer in _source.Implementers)
{
if (model.Id <= implementer.Id)
{
model.Id = implementer.Id + 1;
}
}
var res = Implementer.Create(model);
if (res != null)
{
_source.Implementers.Add(res);
}
return res?.GetViewModel;
}
public ImplementerViewModel? Update(ImplementerBindingModel model)
{
foreach (var implementer in _source.Implementers)
{
if (implementer.Id == model.Id)
{
implementer.Update(model);
return implementer.GetViewModel;
}
}
return null;
}
}
}

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,11 +81,30 @@ 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;
else if (model.Status.HasValue)//далее ищем по статусу
{
foreach (var order in _source.Orders)
{
if (order.Status == model.Status)
{
OrderViewModel vm = order.GetViewModel;
var manufacture = _source.Manufactures.Find(x => x.Id == order.ManufactureId);
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;
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
@ -96,9 +121,44 @@ 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)
{
OrderViewModel vm = order.GetViewModel;
var manufacture = _source.Manufactures.Find(x => x.Id == order.ManufactureId);
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)
{
OrderViewModel vm = order.GetViewModel;
var manufacture = _source.Manufactures.Find(x => x.Id == order.ManufactureId);
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)
{
OrderViewModel vm = order.GetViewModel;
var manufacture = _source.Manufactures.Find(x => x.Id == order.ManufactureId);
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;
}
}
return null;
}
public OrderViewModel? Insert(OrderBindingModel model)

View File

@ -0,0 +1,48 @@
using BlacksmithWorkshopContracts.BindingModels;
using BlacksmithWorkshopContracts.ViewModels;
using BlacksmithWorkshopDataModels.Models;
namespace BlacksmithWorkshopListImplement.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,
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,
};
}
}

View File

@ -16,9 +16,8 @@ namespace BlacksmithWorkshopListImplement.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 static Order? Create(OrderBindingModel? model)
public int? ImplementerId { get; private set; }
public static Order? Create(OrderBindingModel? model)
{
if (model == null)
{
@ -33,7 +32,8 @@ namespace BlacksmithWorkshopListImplement.Models
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
ClientId = model.ClientId
ClientId = model.ClientId,
ImplementerId = model.ImplementerId
};
}
public void Update(OrderBindingModel? model)
@ -43,6 +43,10 @@ namespace BlacksmithWorkshopListImplement.Models
return;
}
Status = model.Status;
if (model.ImplementerId != null)
{
ImplementerId = model.ImplementerId;
}
if (model.DateImplement != null)
{
DateImplement = model.DateImplement;
@ -56,7 +60,7 @@ namespace BlacksmithWorkshopListImplement.Models
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
};
DateImplement = DateImplement
};
}
}