все хранилища

This commit is contained in:
bekodeg 2024-05-19 17:58:47 +04:00
parent 62dcefa246
commit 23c9d3a280
10 changed files with 380 additions and 38 deletions

View File

@ -10,9 +10,11 @@ namespace SushiBarFileImplement
private readonly string ComponentFileName = "Component.xml";
private readonly string OrderFileName = "Order.xml";
private readonly string SushiFileName = "Sushi.xml";
private readonly string ClientFileName = "Sushi.xml";
public List<Component> Components { get; private set; }
public List<Order> Orders { get; private set; }
public List<Sushi> Sushis { get; private set; }
public List<Client> Clients { get; private set; }
public static DataFileSingleton GetInstance()
{
if (instance == null)
@ -24,11 +26,13 @@ namespace SushiBarFileImplement
public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement);
public void SaveSushis() => SaveData(Sushis, SushiFileName, "Sushis", x => x.GetXElement);
public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement);
public void SaveClients() => SaveData(Clients, ClientFileName, "Client", x => x.GetXElement);
private DataFileSingleton()
{
Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!;
Sushis = LoadData(SushiFileName, "Sushi", x => Sushi.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

@ -0,0 +1,78 @@
using SushiBarContracts.BindingModels;
using SushiBarContracts.SearchModels;
using SushiBarContracts.ViewModels;
using SushiBarFileImplement.Models;
namespace SushiBarFileImplement.Implements
{
public class ClientStorage
{
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

@ -16,21 +16,18 @@ namespace SushiBarFileImplement.Implements
public List<OrderViewModel> GetFullList()
{
return source.Orders
.Select(x => AttachSushiName(x.GetViewModel))
.Select(x => AccessClientStorage(AccessSushiStorage(x.GetViewModel))!)
.ToList();
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
return source.Orders
.Where(o =>
(model.Id.HasValue && o.Id == model.Id) ||
(model.DateFrom.HasValue && model.DateTo.HasValue &&
model.DateFrom < o.DateCreate && o.DateCreate < model.DateTo))
.Select(x => AttachSushiName(x.GetViewModel))
.Where(o =>
(!model.Id.HasValue || o.Id == model.Id) &&
(!model.DateFrom.HasValue || o.DateCreate >= model.DateFrom) &&
(!model.DateTo.HasValue || o.DateCreate <= model.DateTo) &&
(!model.ClientId.HasValue || o.ClientId == model.ClientId))
.Select(o => AccessClientStorage(AccessSushiStorage(o.GetViewModel))!)
.ToList();
}
public OrderViewModel? GetElement(OrderSearchModel model)
@ -39,8 +36,9 @@ namespace SushiBarFileImplement.Implements
{
return null;
}
return AttachSushiName(source.Orders
.FirstOrDefault(x => (x.Id == model.Id))?.GetViewModel);
return AccessClientStorage(AccessSushiStorage(
source.Orders
.FirstOrDefault(x => (x.Id == model.Id))?.GetViewModel));
}
public OrderViewModel? Insert(OrderBindingModel model)
{
@ -52,18 +50,18 @@ namespace SushiBarFileImplement.Implements
}
source.Orders.Add(newOrder);
source.SaveOrders();
return AttachSushiName(newOrder.GetViewModel);
return AccessClientStorage(AccessSushiStorage(newOrder.GetViewModel));
}
public OrderViewModel? Update(OrderBindingModel model)
{
var order = source.Orders.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
var element = source.Orders.FirstOrDefault(x => x.Id == model.Id);
if (element == null)
{
return null;
}
order.Update(model);
element.Update(model);
source.SaveOrders();
return AttachSushiName(order.GetViewModel);
return AccessClientStorage(AccessSushiStorage(element.GetViewModel));
}
public OrderViewModel? Delete(OrderBindingModel model)
{
@ -72,14 +70,32 @@ namespace SushiBarFileImplement.Implements
{
source.Orders.Remove(element);
source.SaveOrders();
return AttachSushiName(element.GetViewModel);
return AccessClientStorage(AccessSushiStorage(element.GetViewModel));
}
return null;
}
private OrderViewModel AttachSushiName(OrderViewModel ?model)
public OrderViewModel? AccessSushiStorage(OrderViewModel? model)
{
if (model == null) { return null; }
model.SushiName = source.Sushis.First(x => x.Id == model.SushiId).SushiName;
if (model == null)
return null;
foreach (var iceCream in source.Sushis)
{
if (iceCream.Id == model.SushiId)
{
model.SushiName = iceCream.SushiName;
break;
}
}
return model;
}
public OrderViewModel? AccessClientStorage(OrderViewModel? model)
{
if (model == null)
return null;
var client = source.Clients.FirstOrDefault(x => x.Id == model.Id);
if (client != null)
model.ClientFIO = client.ClientFIO;
return model;
}
}

View File

@ -0,0 +1,66 @@
using SushiBarContracts.BindingModels;
using SushiBarContracts.ViewModels;
using System.Xml.Linq;
namespace SushiBarFileImplement.Models
{
public class Client
{
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

@ -2,11 +2,6 @@
using SushiBarContracts.ViewModels;
using SushiBarDataModels.Enums;
using SushiBarDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace SushiBarFileImplement.Models
@ -31,6 +26,7 @@ namespace SushiBarFileImplement.Models
{
Id = model.Id,
SushiId = model.SushiId,
ClientId = model.ClientId,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
@ -48,6 +44,7 @@ namespace SushiBarFileImplement.Models
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
SushiId = Convert.ToInt32(element.Element("SushiId")!.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.ToString()),
@ -69,6 +66,7 @@ namespace SushiBarFileImplement.Models
{
Id = Id,
SushiId = SushiId,
ClientId = ClientId,
Count = Count,
Sum = Sum,
Status = Status,
@ -78,6 +76,7 @@ namespace SushiBarFileImplement.Models
public XElement GetXElement => new("Order",
new XAttribute("Id", Id),
new XElement("SushiId", SushiId),
new XElement("ClientId", ClientId),
new XElement("Count", Count),
new XElement("Sum", Sum.ToString()),
new XElement("Status", Status.ToString()),

View File

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

View File

@ -0,0 +1,99 @@
using SushiBarContracts.BindingModels;
using SushiBarContracts.SearchModels;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;
using SushiBarListImplement.Models;
namespace SushiBarListImplement.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

@ -18,7 +18,7 @@ namespace SushiBarListImplement.Implements
var result = new List<OrderViewModel>();
foreach (var order in _source.Orders)
{
result.Add(AttachSushiName(order.GetViewModel));
result.Add(AccessClientStorage(AccessSushiStorage(order.GetViewModel)));
}
return result;
}
@ -29,9 +29,9 @@ namespace SushiBarListImplement.Implements
{
if (order.Id == model.Id ||
(model.DateFrom.HasValue && model.DateTo.HasValue &&
model.DateFrom < order.DateCreate && order.DateCreate < model.DateTo)))
model.DateFrom < order.DateCreate && order.DateCreate < model.DateTo))
{
result.Add(AttachSushiName(order.GetViewModel));
result.Add(AccessClientStorage(AccessSushiStorage(order.GetViewModel)));
}
}
return result;
@ -46,7 +46,7 @@ namespace SushiBarListImplement.Implements
{
if (model.Id.HasValue && order.Id == model.Id)
{
return AttachSushiName(order.GetViewModel);
return AccessClientStorage(AccessSushiStorage(order.GetViewModel));
}
}
return null;
@ -67,7 +67,7 @@ namespace SushiBarListImplement.Implements
return null;
}
_source.Orders.Add(newOrder);
return AttachSushiName(newOrder.GetViewModel);
return AccessClientStorage(AccessSushiStorage(newOrder.GetViewModel));
}
public OrderViewModel? Update(OrderBindingModel model)
{
@ -76,7 +76,7 @@ namespace SushiBarListImplement.Implements
if (order.Id == model.Id)
{
order.Update(model);
return AttachSushiName(order.GetViewModel);
return AccessClientStorage(AccessSushiStorage(order.GetViewModel));
}
}
return null;
@ -89,22 +89,30 @@ namespace SushiBarListImplement.Implements
{
var element = _source.Orders[i];
_source.Orders.RemoveAt(i);
return AttachSushiName(element.GetViewModel);
return AccessClientStorage(AccessSushiStorage(element.GetViewModel));
}
}
return null;
}
private OrderViewModel AttachSushiName(OrderViewModel model)
public OrderViewModel AccessSushiStorage(OrderViewModel model)
{
foreach (var sushi in _source.Sushis)
foreach (var iceCream in _source.Sushis)
{
if (sushi.Id == model.SushiId)
if (iceCream.Id == model.SushiId)
{
model.SushiName = sushi.SushiName;
return model;
model.SushiName = iceCream.SushiName;
break;
}
}
return model;
}
public OrderViewModel AccessClientStorage(OrderViewModel model)
{
var client = _source.Clients.FirstOrDefault(x => x.Id == model.ClientId);
if (client != null)
model.ClientFIO = client.ClientFIO;
return model;
}
}
}

View File

@ -0,0 +1,67 @@
using SushiBarContracts.BindingModels;
using SushiBarContracts.ViewModels;
using SushiBarDataModels.Models;
using System.Xml.Linq;
namespace SushiBarListImplement.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

@ -29,6 +29,7 @@ namespace SushiBarListImplement.Models
{
Id = model.Id,
SushiId = model.SushiId,
ClientId = model.ClientId,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
@ -43,6 +44,7 @@ namespace SushiBarListImplement.Models
return;
}
SushiId = model.SushiId;
ClientId = model.ClientId;
Count = model.Count;
Sum = model.Sum;
Status = model.Status;
@ -52,6 +54,7 @@ namespace SushiBarListImplement.Models
{
Id = Id,
SushiId = SushiId,
ClientId = ClientId,
Count = Count,
Sum = Sum,
Status = Status,