2024-05-01 12:51:35 +04:00
|
|
|
|
using DinerContracts.BindingModels;
|
|
|
|
|
using DinerContracts.SearchModels;
|
|
|
|
|
using DinerContracts.StoragesContracts;
|
|
|
|
|
using DinerContracts.ViewModels;
|
2024-05-01 21:05:07 +04:00
|
|
|
|
using DinerFileImplement.Models;
|
2024-05-01 12:51:35 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace DinerFileImplement.Implements {
|
|
|
|
|
public class ClientStorage : IClientStorage {
|
2024-05-01 21:05:07 +04:00
|
|
|
|
|
|
|
|
|
private readonly DataFileSingleton _source;
|
|
|
|
|
|
|
|
|
|
public ClientStorage() {
|
|
|
|
|
_source = DataFileSingleton.GetInstance();
|
2024-05-01 12:51:35 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-01 21:05:07 +04:00
|
|
|
|
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.SaveClient();
|
|
|
|
|
return newClient.GetViewModel;
|
2024-05-01 12:51:35 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-01 21:05:07 +04:00
|
|
|
|
public ClientViewModel? Update(ClientBindingModel model) {
|
|
|
|
|
var client = _source.Clients.FirstOrDefault(x => x.ID == model.ID);
|
|
|
|
|
if (client == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
client.Update(model);
|
|
|
|
|
_source.SaveClient();
|
|
|
|
|
return client.GetViewModel;
|
2024-05-01 12:51:35 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-01 21:05:07 +04:00
|
|
|
|
public ClientViewModel? Delete(ClientBindingModel model) {
|
|
|
|
|
var element = _source.Clients.FirstOrDefault(x => x.ID == model.ID);
|
|
|
|
|
if (element != null) {
|
|
|
|
|
_source.Clients.Remove(element);
|
|
|
|
|
_source.SaveClient();
|
|
|
|
|
return element.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
2024-05-01 12:51:35 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-01 21:05:07 +04:00
|
|
|
|
public ClientViewModel? GetElement(ClientSearchModel model) {
|
|
|
|
|
if (string.IsNullOrEmpty(model.ClientFIO) && !model.ID.HasValue) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return _source.Clients.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ClientFIO) &&
|
|
|
|
|
x.ClientFIO == model.ClientFIO) || model.ID.HasValue &&
|
|
|
|
|
x.ID == model.ID)?.GetViewModel;
|
2024-05-01 12:51:35 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-01 21:05:07 +04:00
|
|
|
|
public List<ClientViewModel> GetFilteredList(ClientSearchModel model) {
|
|
|
|
|
if (string.IsNullOrEmpty(model.ClientFIO)) {
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
return _source.Clients.Where(x => x.ClientFIO.Contains(model.ClientFIO))
|
|
|
|
|
.Select(x => x.GetViewModel).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ClientViewModel> GetFullList() {
|
|
|
|
|
return _source.Clients.Select(x => x.GetViewModel).ToList();
|
2024-05-01 12:51:35 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|