ура 9 страница

This commit is contained in:
Sergey Kozyrev 2024-04-02 21:56:35 +04:00
parent aceba1ae3c
commit 5ecbabffe4
7 changed files with 295 additions and 2 deletions

View File

@ -15,12 +15,15 @@ namespace SewingDressesDatabaseImplement.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();
}
using var context = new SewingDressesDatabase();
return context.Orders.Where(x => x.Id == model.Id).Select(x => AcessDressesStorage(x.GetViewModel, context)).ToList();
if (model.Id.HasValue)
return context.Orders.Where(x => x.Id == model.Id).Where(x => x.ClientId == model.ClientId).Select(x => AcessDressesStorage(x.GetViewModel, context)).ToList();
else
return context.Orders.Where(x => x.DateCreate >= model.DateFrom).Where(x => x.ClientId == model.ClientId).Where(x => x.DateCreate <= model.DateTo).Select(x => AcessDressesStorage(x.GetViewModel, context)).ToList();
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
@ -81,7 +84,14 @@ namespace SewingDressesDatabaseImplement.Implements
{
return null;
}
var client = context.Clients.FirstOrDefault(x => x.Id == model.ClientId);
if (client == null)
{
return null;
}
model.DressName = dress.DressName;
model.ClientFIO = client.ClientFIO;
model.ClientId = client.Id;
return model;
}

View File

@ -9,9 +9,11 @@ namespace SewingDressesFileImplement
private readonly string ComponentFileName = "Component.xml";
private readonly string OrderFileName = "Order.xml";
private readonly string DressFileName = "Dress.xml";
private readonly string ClientFileName = "Client.xml";
public List<Component> Components { get; private set; }
public List<Order> Orders { get; private set; }
public List<Dress> Dresses { get; private set;}
public List<Client> Clients { get; private set; }
public static DataFileSingleton GetInstance()
{
if (instance == null)
@ -39,11 +41,13 @@ namespace SewingDressesFileImplement
public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement);
public void SaveDresses() => SaveData(Dresses, DressFileName, "Dresses", 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)!)!;
Dresses = LoadData(DressFileName, "Dress", x => Dress.Create(x)!)!;
Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!;
Clients = LoadData(ClientFileName, "Client", x => Client.Create(x)!)!;
}
}
}

View File

@ -0,0 +1,61 @@
using SewingDressesContracts.StoragesContracts;
using SewingDressesContracts.BindingModels;
using SewingDressesContracts.SearchModels;
using SewingDressesFileImplement.Models;
using SewingDressesContracts.ViewModels;
namespace SewingDressesFileImplement.Implements
{
public class ClientStorage : IClientStorage
{
private readonly DataFileSingleton _source;
public ClientStorage()
{
_source = DataFileSingleton.GetInstance();
}
public ClientViewModel? Insert(ClientBindingModel model)
{
model.Id = _source.Clients.Count > 0 ? _source.Clients.Max(client => client.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 client = _source.Clients.FirstOrDefault(client => client.Id == model.Id);
if (client == null) { return null; }
_source.Clients.Remove(client);
_source.SaveClients();
return client.GetViewModel;
}
public ClientViewModel? GetElement(ClientSearchModel model)
{
if (string.IsNullOrEmpty(model.Email) && !model.Id.HasValue) { return null; }
return _source.Clients.FirstOrDefault(x => (!(string.IsNullOrEmpty(model.Email)) && model.Email == x.Email) || (model.Id.HasValue && model.Id == x.Id))?.GetViewModel;
}
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
{
if (string.IsNullOrEmpty(model.Email)) { return new(); }
return _source.Clients.Where(x => x.Email.Equals(model.Email)).Select(x => x.GetViewModel).ToList();
}
public List<ClientViewModel> GetFullList()
{
return _source.Clients.Select(x => x.GetViewModel).ToList();
}
}
}

View File

@ -0,0 +1,65 @@
using SewingDressesContracts.BindingModels;
using SewingDressesContracts.ViewModels;
using SewingDressesDataModels.Models;
using System.Xml.Linq;
namespace SewingDressesFileImplement.Models
{
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.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),
new XElement("Password", Password));
}
}

View File

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

View File

@ -0,0 +1,106 @@
using SewingDressesContracts.BindingModels;
using SewingDressesContracts.SearchModels;
using SewingDressesContracts.StoragesContracts;
using SewingDressesContracts.ViewModels;
using SewingDressesListImplement.Models;
namespace SewingDressesListImplement.Implements
{
public class ClientStorage : IClientStorage
{
private readonly DataListSingleton _source;
public ClientStorage()
{
_source = DataListSingleton.GetInstance();
}
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 (model.Id == _source.Clients[i].Id)
{
var element = _source.Clients[i];
_source.Clients.RemoveAt(i);
return element.GetViewModel;
}
}
return null;
}
public ClientViewModel? GetElement(ClientSearchModel model)
{
if (string.IsNullOrEmpty(model.Email) && !model.Id.HasValue)
{
return null;
}
foreach (var client in _source.Clients)
{
if ((!string.IsNullOrEmpty(model.Email) && model.Email == client.Email) || (model.Id.HasValue && model.Id.Value == client.Id))
{
return client.GetViewModel;
}
}
return null;
}
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
{
var result = new List<ClientViewModel>();
if (string.IsNullOrEmpty(model.Email))
{
return result;
}
foreach (var client in _source.Clients)
{
if (client.Email.Contains(model.Email))
{
result.Add(client.GetViewModel);
}
}
return result;
}
public List<ClientViewModel> GetFullList()
{
var result = new List<ClientViewModel>();
foreach (var client in _source.Clients)
{
result.Add(client.GetViewModel);
}
return result;
}
}
}

View File

@ -0,0 +1,45 @@
using SewingDressesContracts.BindingModels;
using SewingDressesContracts.ViewModels;
using SewingDressesDataModels.Models;
namespace SewingDressesListImplement.Models
{
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 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
};
}
}