Реализация хранилища клиентов в файле
This commit is contained in:
parent
292fa41ba4
commit
300d108c70
@ -15,9 +15,11 @@ namespace LawFirmFileImplement
|
|||||||
private readonly string BlankFileName = "Blank.xml";
|
private readonly string BlankFileName = "Blank.xml";
|
||||||
private readonly string OrderFileName = "Order.xml";
|
private readonly string OrderFileName = "Order.xml";
|
||||||
private readonly string DocumentFileName = "Document.xml";
|
private readonly string DocumentFileName = "Document.xml";
|
||||||
|
private readonly string ClientFileName = "Client.xml";
|
||||||
public List<Blank> Blanks { get; private set; }
|
public List<Blank> Blanks { get; private set; }
|
||||||
public List<Order> Orders { get; private set; }
|
public List<Order> Orders { get; private set; }
|
||||||
public List<Document> Documents { get; private set; }
|
public List<Document> Documents { get; private set; }
|
||||||
|
public List<Client> Clients { get; private set; }
|
||||||
public static DataFileSingleton GetInstance()
|
public static DataFileSingleton GetInstance()
|
||||||
{
|
{
|
||||||
if (instance == null)
|
if (instance == null)
|
||||||
@ -29,11 +31,13 @@ namespace LawFirmFileImplement
|
|||||||
public void SaveBlanks() => SaveData(Blanks, BlankFileName, "Blanks", x => x.GetXElement);
|
public void SaveBlanks() => SaveData(Blanks, BlankFileName, "Blanks", x => x.GetXElement);
|
||||||
public void SaveDocuments() => SaveData(Documents, DocumentFileName, "Documents", x => x.GetXElement);
|
public void SaveDocuments() => SaveData(Documents, DocumentFileName, "Documents", x => x.GetXElement);
|
||||||
public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", 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()
|
private DataFileSingleton()
|
||||||
{
|
{
|
||||||
Blanks = LoadData(BlankFileName, "Blank", x => Blank.Create(x)!)!;
|
Blanks = LoadData(BlankFileName, "Blank", x => Blank.Create(x)!)!;
|
||||||
Documents = LoadData(DocumentFileName, "Document", x => Document.Create(x)!)!;
|
Documents = LoadData(DocumentFileName, "Document", x => Document.Create(x)!)!;
|
||||||
Orders = LoadData(OrderFileName, "Order", x => Order.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)
|
private static List<T>? LoadData<T>(string filename, string xmlNodeName, Func<XElement, T> selectFunction)
|
||||||
{
|
{
|
||||||
|
86
LawFirm/LawFirmFileImplement/Implements/ClientStorage.cs
Normal file
86
LawFirm/LawFirmFileImplement/Implements/ClientStorage.cs
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
using LawFirmContracts.BindingModels;
|
||||||
|
using LawFirmContracts.SearchModels;
|
||||||
|
using LawFirmContracts.StorageContracts;
|
||||||
|
using LawFirmContracts.ViewModels;
|
||||||
|
using LawFirmFileImplement.Models;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace LawFirmFileImplement.Implements
|
||||||
|
{
|
||||||
|
public class ClientStorage : IClientStorage
|
||||||
|
{
|
||||||
|
private readonly DataFileSingleton source;
|
||||||
|
public ClientStorage()
|
||||||
|
{
|
||||||
|
source = DataFileSingleton.GetInstance();
|
||||||
|
}
|
||||||
|
|
||||||
|
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 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 List<ClientViewModel> GetFullList()
|
||||||
|
{
|
||||||
|
return source.Clients.Select(x => x.GetViewModel).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
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 client = source.Clients.FirstOrDefault(x => x.Id == model.Id);
|
||||||
|
if (client == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
source.Clients.Remove(client);
|
||||||
|
source.SaveClients();
|
||||||
|
return client?.GetViewModel;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
79
LawFirm/LawFirmFileImplement/Models/Client.cs
Normal file
79
LawFirm/LawFirmFileImplement/Models/Client.cs
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
using LawFirmContracts.BindingModels;
|
||||||
|
using LawFirmContracts.ViewModels;
|
||||||
|
using LawFirmDataModels.Models;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Xml.Linq;
|
||||||
|
|
||||||
|
namespace LawFirmFileImplement.Models
|
||||||
|
{
|
||||||
|
public class Client : IClientModel
|
||||||
|
{
|
||||||
|
public int Id { get; private set; }
|
||||||
|
|
||||||
|
public string ClientFIO { get; private set; } = string.Empty;
|
||||||
|
|
||||||
|
public string Email { get; private set; } = string.Empty;
|
||||||
|
|
||||||
|
public string Password { get; private set; } = string.Empty;
|
||||||
|
|
||||||
|
public static Client? Create(ClientBindingModel model)
|
||||||
|
{
|
||||||
|
if (model == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return new()
|
||||||
|
{
|
||||||
|
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()
|
||||||
|
{
|
||||||
|
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
|
||||||
|
ClientFIO = element.Element("FIO")!.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("FIO", ClientFIO),
|
||||||
|
new XElement("Email", Email),
|
||||||
|
new XElement("Password", Password)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user