какой то треш происходит я устала

This commit is contained in:
kamilia 2024-05-05 23:57:12 +04:00
parent a02b349485
commit b41ff6cc25
5 changed files with 184 additions and 1 deletions

View File

@ -17,6 +17,11 @@ namespace AircraftPlantDataModels.Models
/// </summary>
int PlaneId { get; }
/// <summary>
/// Идентификатор клиента
/// </summary>
int ClientId { get; }
/// <summary>
/// Количество изделий
/// </summary>

View File

@ -0,0 +1,77 @@
using AircraftPlantContracts.BindingModels;
using AircraftPlantContracts.ViewModels;
using AircraftPlantDataModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace AircraftPlantFileImplement
{
public class Client : IClientModel
{
public int Id { get; set; }
public string ClientFIO { get; 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.Attribute("ClientFIO")!.Value,
Email = element.Attribute("Email")!.Value,
Password = element.Attribute("Password")!.Value
};
}
public void Update(ClientBindingModel? model)
{
if (model == null)
{
return;
}
ClientFIO = model.ClientFIO;
Password = model.Password;
Email = model.Email;
}
public ClientViewModel GetViewModel => new()
{
Id = Id,
ClientFIO = ClientFIO,
Password = Password,
Email = Email
};
public XElement GetXElement => new("Client",
new XAttribute("Id", Id),
new XElement("ClientFIO", ClientFIO),
new XElement("Email", Email),
new XElement("Password", Password));
}
}

View File

@ -0,0 +1,97 @@
using AircraftPlantContracts.BindingModels;
using AircraftPlantContracts.SearchModels;
using AircraftPlantContracts.StoragesContracts;
using AircraftPlantContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AircraftPlantFileImplement
{
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.Email))
{
return new();
}
return _source.Clients
.Where(x => x.Email.Contains(model.Email))
.Select(x => x.GetViewModel)
.ToList();
}
public ClientViewModel? GetElement(ClientSearchModel model)
{
if (string.IsNullOrEmpty(model.Email) && !model.Id.HasValue)
{
return null;
}
return _source.Clients
.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.Email) &&
x.Email == model.Email) ||
(model.Id.HasValue && x.Id == model.Id))
?.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(x => x.Id == model.Id);
if (element != null)
{
_source.Clients.Remove(element);
_source.SaveClients();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -14,9 +14,11 @@ namespace AircraftPlantFileImplement
private readonly string ComponentFileName = "Component.xml";
private readonly string OrderFileName = "Order.xml";
private readonly string PlaneFileName = "Plane.xml";
private readonly string ClientFileName = "Client.xml";
public List<Component> Components { get; private set; }
public List<Order> Orders { get; private set; }
public List<Plane> Planes { get; private set; }
public List<Client> Clients { get; set; }
public static DataFileSingleton GetInstance()
{
if (instance == null)
@ -28,11 +30,13 @@ namespace AircraftPlantFileImplement
public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement);
public void SavePlanes() => SaveData(Planes, PlaneFileName, "Planes", 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)!)!;
Planes = LoadData(PlaneFileName, "Plane", x => Plane.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)
{

View File

@ -24,7 +24,7 @@ namespace AircraftPlantFileImplement.Implements
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue)
if (!model.Id.HasValue || !model.DateFrom.HasValue || !model.DateTo.HasValue || !model.ClientId.HasValue)
{
return new();
}