по новой добавлена логика (почему-то удалилась)
This commit is contained in:
parent
0778a6d2c7
commit
3a4c173849
@ -1,12 +1,116 @@
|
|||||||
using System;
|
using ConfectioneryBusinessLogic.BusinessLogics;
|
||||||
using System.Collections.Generic;
|
using ConfectioneryContracts.BindingModels;
|
||||||
using System.Linq;
|
using ConfectioneryContracts.BusinessLogicsContracts;
|
||||||
using System.Text;
|
using ConfectioneryContracts.SearchModels;
|
||||||
using System.Threading.Tasks;
|
using ConfectioneryContracts.StoragesContract;
|
||||||
|
using ConfectioneryContracts.ViewModels;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
namespace ConfectioneryBusinessLogic
|
namespace ConfectioneryBusinessLogic
|
||||||
{
|
{
|
||||||
internal class ClientLogic
|
public class ClientLogic : IClientLogic
|
||||||
{
|
{
|
||||||
|
private readonly ILogger _logger;
|
||||||
|
private readonly IClientStorage _clientStorage;
|
||||||
|
public ClientLogic(ILogger<ClientLogic> logger, IClientStorage clientStorage)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
_clientStorage = clientStorage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Create(ClientBindingModel model)
|
||||||
|
{
|
||||||
|
CheckModel(model);
|
||||||
|
if (_clientStorage.Insert(model) == null)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Insert operation failed");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Delete(ClientBindingModel model)
|
||||||
|
{
|
||||||
|
CheckModel(model, false);
|
||||||
|
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
||||||
|
if (_clientStorage.Delete(model) == null)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Delete operation failed");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ClientViewModel? ReadElement(ClientSearchModel model)
|
||||||
|
{
|
||||||
|
if (model == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(model));
|
||||||
|
}
|
||||||
|
_logger.LogInformation("ReadElement. Email:{Email}.Id:{ Id}",
|
||||||
|
model.Email, model.Id);
|
||||||
|
var element = _clientStorage.GetElement(model);
|
||||||
|
if (element == null)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("ReadElement element not found");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
|
||||||
|
return element;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<ClientViewModel>? ReadList(ClientSearchModel? model)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("ReadList. Email:{Email}.Id:{ Id} ", model?.Email, model?.Id);
|
||||||
|
var list = (model == null) ? _clientStorage.GetFullList() :
|
||||||
|
_clientStorage.GetFilteredList(model);
|
||||||
|
if (list == null)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("ReadList return null list");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Update(ClientBindingModel model)
|
||||||
|
{
|
||||||
|
CheckModel(model);
|
||||||
|
if (_clientStorage.Update(model) == null)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Update operation failed");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void CheckModel(ClientBindingModel model, bool withParams = true)
|
||||||
|
{
|
||||||
|
if (model == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(model));
|
||||||
|
}
|
||||||
|
if (!withParams)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (string.IsNullOrEmpty(model.ClientFIO))
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException("Нет фио клиента", nameof(model.ClientFIO));
|
||||||
|
}
|
||||||
|
if (string.IsNullOrEmpty(model.Email))
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException("Нет логина клиента", nameof(model.Email));
|
||||||
|
}
|
||||||
|
_logger.LogInformation("Client. Id: {Id}, FIO: {fio}, email: {email}", model.Id, model.ClientFIO, model.Email );
|
||||||
|
var element = _clientStorage.GetElement(new ClientSearchModel
|
||||||
|
{
|
||||||
|
Email = model.Email,
|
||||||
|
});
|
||||||
|
if (element != null && element.Id != model.Id)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("Клиент с таким логином уже есть");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,8 +6,9 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using System.Xml.Linq;
|
||||||
|
|
||||||
namespace ConfectioneryListImplement
|
namespace ConfectioneryFileImplement
|
||||||
{
|
{
|
||||||
public class Client : IClientModel
|
public class Client : IClientModel
|
||||||
{
|
{
|
||||||
@ -33,6 +34,21 @@ namespace ConfectioneryListImplement
|
|||||||
Password = model.Password
|
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)
|
public void Update(ClientBindingModel model)
|
||||||
{
|
{
|
||||||
@ -52,5 +68,12 @@ namespace ConfectioneryListImplement
|
|||||||
Email = Email,
|
Email = Email,
|
||||||
Password = Password,
|
Password = Password,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
public XElement GetXElement => new("Client",
|
||||||
|
new XAttribute("Id", Id),
|
||||||
|
new XElement("FIO", ClientFIO),
|
||||||
|
new XElement("Email", Email),
|
||||||
|
new XElement("Password", Password)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,20 +2,15 @@
|
|||||||
using ConfectioneryContracts.SearchModels;
|
using ConfectioneryContracts.SearchModels;
|
||||||
using ConfectioneryContracts.StoragesContract;
|
using ConfectioneryContracts.StoragesContract;
|
||||||
using ConfectioneryContracts.ViewModels;
|
using ConfectioneryContracts.ViewModels;
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace ConfectioneryListImplement
|
namespace ConfectioneryFileImplement
|
||||||
{
|
{
|
||||||
public class ClientStorage : IClientStorage
|
public class ClientStorage : IClientStorage
|
||||||
{
|
{
|
||||||
private readonly DataListSingleton _source;
|
private readonly DataFileSingleton _source;
|
||||||
public ClientStorage()
|
public ClientStorage()
|
||||||
{
|
{
|
||||||
_source = DataListSingleton.GetInstance();
|
_source = DataFileSingleton.GetInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ClientViewModel? Delete(ClientBindingModel model)
|
public ClientViewModel? Delete(ClientBindingModel model)
|
||||||
@ -24,6 +19,7 @@ namespace ConfectioneryListImplement
|
|||||||
if (res != null)
|
if (res != null)
|
||||||
{
|
{
|
||||||
_source.Clients.Remove(res);
|
_source.Clients.Remove(res);
|
||||||
|
_source.SaveClients();
|
||||||
}
|
}
|
||||||
return res?.GetViewModel;
|
return res?.GetViewModel;
|
||||||
}
|
}
|
||||||
@ -61,10 +57,12 @@ namespace ConfectioneryListImplement
|
|||||||
|
|
||||||
public ClientViewModel? Insert(ClientBindingModel model)
|
public ClientViewModel? Insert(ClientBindingModel model)
|
||||||
{
|
{
|
||||||
|
model.Id = _source.Clients.Count > 0 ? _source.Clients.Max(x => x.Id) + 1 : 1;
|
||||||
var res = Client.Create(model);
|
var res = Client.Create(model);
|
||||||
if (res != null)
|
if (res != null)
|
||||||
{
|
{
|
||||||
_source.Clients.Add(res);
|
_source.Clients.Add(res);
|
||||||
|
_source.SaveClients();
|
||||||
}
|
}
|
||||||
return res?.GetViewModel;
|
return res?.GetViewModel;
|
||||||
}
|
}
|
||||||
@ -72,7 +70,11 @@ namespace ConfectioneryListImplement
|
|||||||
public ClientViewModel? Update(ClientBindingModel model)
|
public ClientViewModel? Update(ClientBindingModel model)
|
||||||
{
|
{
|
||||||
var res = _source.Clients.FirstOrDefault(x => x.Id == model.Id);
|
var res = _source.Clients.FirstOrDefault(x => x.Id == model.Id);
|
||||||
res?.Update(model);
|
if (res != null)
|
||||||
|
{
|
||||||
|
res.Update(model);
|
||||||
|
_source.SaveClients();
|
||||||
|
}
|
||||||
return res?.GetViewModel;
|
return res?.GetViewModel;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user