добавил недостающие реализации Клиента

This commit is contained in:
dyakonovr 2024-04-21 14:04:32 +04:00
parent 593cd58ef5
commit b4081cb911
10 changed files with 369 additions and 33 deletions

View File

@ -14,9 +14,11 @@ namespace SoftwareInstallationFileImplement
private readonly string ComponentFileName = "Component.xml";
private readonly string OrderFileName = "Order.xml";
private readonly string PackageFileName = "Package.xml";
private readonly string ClientFileName = "Client.xml";
public List<Component> Components { get; private set; }
public List<Order> Orders { get; private set; }
public List<Package> Packages { get; private set; }
public List<Client> Clients { get; private set; }
public static DataFileSingleton GetInstance()
{
if (instance == null)
@ -28,18 +30,19 @@ namespace SoftwareInstallationFileImplement
public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement);
public void SavePackages() => SaveData(Packages, PackageFileName, "Packages", 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()
{
Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!;
Packages = LoadData(PackageFileName, "Package", x => Package.Create(x)!)!;
Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!;
Clients = LoadData(ClientFileName, "Client", x => Client.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 XDocument.Load(filename)?.Root?.Elements(xmlNodeName)?.Select(selectFunction)?.ToList();
}
return new List<T>();
}

View File

@ -0,0 +1,85 @@
using SoftwareInstallationContracts.BindingModels;
using SoftwareInstallationContracts.SearchModels;
using SoftwareInstallationContracts.StoragesContracts;
using SoftwareInstallationContracts.ViewModels;
using SoftwareInstallationFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoftwareInstallationFileImplement.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.ClientFIO.Contains(model.Email) &&
string.IsNullOrEmpty(model.Password) || x.ClientFIO.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 element = source.Clients.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
source.Clients.Remove(element);
source.SaveClients();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -6,6 +6,7 @@ using SoftwareInstallationFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
@ -50,16 +51,18 @@ namespace SoftwareInstallationFileImplement.Implements
return source.Orders.Where(x => (
(!model.Id.HasValue || x.Id == model.Id) &&
(!model.DateFrom.HasValue || x.DateCreate >= model.DateFrom) &&
(!model.DateTo.HasValue || x.DateCreate <= model.DateTo)
(!model.DateTo.HasValue || x.DateCreate <= model.DateTo) &&
(!model.ClientId.HasValue || x.ClientId == model.ClientId)
)
)
.Select(x => GetPackageName(x.GetViewModel))
.Select(GetClientName)
.ToList();
}
public List<OrderViewModel> GetFullList()
{
return source.Orders.Select(x => GetPackageName(x.GetViewModel)).ToList();
return source.Orders.Select(x => GetPackageName(x.GetViewModel)).Select(GetClientName).ToList();
}
public OrderViewModel? Insert(OrderBindingModel model)
@ -100,5 +103,15 @@ namespace SoftwareInstallationFileImplement.Implements
}
return viewModel;
}
private OrderViewModel GetClientName(OrderViewModel viewModel)
{
var client = source.Clients.FirstOrDefault(x => x.Id == viewModel.ClientId);
if (client != null)
{
viewModel.ClientFIO = client.ClientFIO;
}
return viewModel;
}
}
}

View File

@ -0,0 +1,72 @@
using SoftwareInstallationContracts.BindingModels;
using SoftwareInstallationContracts.ViewModels;
using SoftwareInstallationDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace SoftwareInstallationFileImplement.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

@ -31,6 +31,7 @@ namespace SoftwareInstallationFileImplement.Models
{
Id = model.Id,
PackageId = model.PackageId,
ClientId = model.ClientId,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
@ -49,6 +50,7 @@ namespace SoftwareInstallationFileImplement.Models
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
PackageId = Convert.ToInt32(element.Element("PackageId")!.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),
@ -77,6 +79,7 @@ namespace SoftwareInstallationFileImplement.Models
{
Id = Id,
PackageId = PackageId,
ClientId = ClientId,
Count = Count,
Sum = Sum,
Status = Status,
@ -86,6 +89,7 @@ namespace SoftwareInstallationFileImplement.Models
public XElement GetXElement => new("Order",
new XAttribute("Id", Id),
new XElement("PackageId", PackageId.ToString()),
new XElement("ClientId", ClientId.ToString()),
new XElement("Count", Count.ToString()),
new XElement("Sum", Sum.ToString()),
new XElement("Status", Status.ToString()),

View File

@ -13,11 +13,13 @@ namespace SoftwareInstallationListImplement
public List<Component> Components { get; set; }
public List<Order> Orders { get; set; }
public List<Package> Packages { get; set; }
public List<Client> Clients { get; set; }
private DataListSingleton()
{
Components = new List<Component>();
Orders = new List<Order>();
Packages = new List<Package>();
Clients = new List<Client>();
}
public static DataListSingleton GetInstance()
{

View File

@ -0,0 +1,104 @@
using SoftwareInstallationContracts.BindingModels;
using SoftwareInstallationContracts.SearchModels;
using SoftwareInstallationContracts.StoragesContracts;
using SoftwareInstallationContracts.ViewModels;
using SoftwareInstallationListImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoftwareInstallationListImplement.Implements
{
public class ClientStorage : IClientStorage
{
private readonly DataListSingleton _source;
public ClientStorage()
{
_source = DataListSingleton.GetInstance();
}
public List<ClientViewModel> GetFullList()
{
var result = new List<ClientViewModel>();
foreach (var client in _source.Clients)
{
result.Add(client.GetViewModel);
}
return result;
}
public List<ClientViewModel> GetFilteredList(ClientSearchModel
model)
{
var result = new List<ClientViewModel>();
if (string.IsNullOrEmpty(model.ClientFIO) && string.IsNullOrEmpty(model.Email) && string.IsNullOrEmpty(model.Password))
{
return result;
}
foreach (var client in _source.Clients)
{
if (client.ClientFIO.Contains(model.ClientFIO))
{
result.Add(client.GetViewModel);
}
}
return result;
}
public ClientViewModel? GetElement(ClientSearchModel model)
{
foreach (var client in _source.Clients)
{
if ((string.IsNullOrEmpty(model.ClientFIO) || client.ClientFIO == model.ClientFIO) &&
(!model.Id.HasValue || client.Id == model.Id) && (string.IsNullOrEmpty(model.Email) || client.Email == model.Email) &&
(string.IsNullOrEmpty(model.Password) || client.Password == model.Password))
{
return client.GetViewModel;
}
}
return null;
}
public ClientViewModel? Insert(ClientBindingModel model)
{
model.Id = 1;
foreach (var client in _source.Clients)
{
if (model.Id <= client.Id)
{
model.Id = client.Id + 1;
}
}
var newClient = Client.Create(model);
if (newClient == null)
{
return null;
}
_source.Clients.Add(newClient);
return newClient.GetViewModel;
}
public ClientViewModel? Update(ClientBindingModel model)
{
foreach (var client in _source.Clients)
{
if (client.Id == model.Id)
{
client.Update(model);
return client.GetViewModel;
}
}
return null;
}
public ClientViewModel? Delete(ClientBindingModel model)
{
for (int i = 0; i < _source.Clients.Count; ++i)
{
if (_source.Clients[i].Id == model.Id)
{
var element = _source.Clients[i];
_source.Clients.RemoveAt(i);
return element.GetViewModel;
}
}
return null;
}
}
}

View File

@ -54,37 +54,26 @@ namespace SoftwareInstallationListImplement.Implements
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
var result = new List<OrderViewModel>();
if (!model.Id.HasValue)
{
return result;
}
foreach (var order in _source.Orders)
{
if (model.Id.HasValue && order.Id == model.Id)
{
result.Add(GetViewModelName(order));
}
}
return result;
}
public List<OrderViewModel> GetFullList(OrderSearchModel model)
{
var result = new List<OrderViewModel>();
foreach (var order in _source.Orders)
{
if ((!model.Id.HasValue || order.Id == model.Id) &&
(!model.DateFrom.HasValue || order.DateCreate >= model.DateFrom) &&
(!model.DateTo.HasValue || order.DateCreate <= model.DateTo))
(!model.DateFrom.HasValue || order.DateCreate >= model.DateFrom) &&
(!model.DateTo.HasValue || order.DateCreate <= model.DateTo) &&
(!model.ClientId.HasValue || order.ClientId == model.ClientId)) ;
{
result.Add(GetViewModelName(order.GetViewModel));
result.Add(GetClientName(GetViewModelName(order.GetViewModel)));
}
}
return result;
}
public List<OrderViewModel> GetFullList()
{
var result = new List<OrderViewModel>();
foreach (var order in _source.Orders)
{
result.Add(GetClientName(GetViewModelName(order.GetViewModel)));
}
return result;
}
@ -125,18 +114,30 @@ namespace SoftwareInstallationListImplement.Implements
return null;
}
private OrderViewModel GetViewModelName(Order model)
private OrderViewModel GetViewModelName(OrderViewModel viewModel)
{
var res = model.GetViewModel;
foreach (var package in _source.Packages)
{
if (package.Id == model.PackageId)
if (package.Id == viewModel.PackageId)
{
res.PackageName = package.PackageName;
viewModel.PackageName = package.PackageName;
break;
}
}
return res;
return viewModel;
}
private OrderViewModel GetClientName(OrderViewModel viewModel)
{
foreach (var client in _source.Clients)
{
if (client.Id == viewModel.ClientId)
{
viewModel.ClientFIO = client.ClientFIO;
break;
}
}
return viewModel;
}
}
}

View File

@ -0,0 +1,50 @@
using SoftwareInstallationContracts.BindingModels;
using SoftwareInstallationContracts.ViewModels;
using SoftwareInstallationDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoftwareInstallationListImplement.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 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
};
}
}

View File

@ -31,6 +31,7 @@ namespace SoftwareInstallationListImplement.Models
return new Order
{
PackageId = model.PackageId,
ClientId = model.ClientId,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
@ -53,6 +54,7 @@ namespace SoftwareInstallationListImplement.Models
{
Id = Id,
PackageId = PackageId,
ClientId = ClientId,
Count = Count,
Sum = Sum,
Status = Status,