2024-04-28 21:20:10 +04:00
|
|
|
|
using ElectronicsShopContracts.BindingModels;
|
|
|
|
|
using ElectronicsShopContracts.BusinessLogicContracts;
|
|
|
|
|
using ElectronicsShopContracts.SearchModels;
|
|
|
|
|
using ElectronicsShopContracts.ViewModels;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace ElectronicsShopBusinessLogic.BusinessLogic
|
|
|
|
|
{
|
2024-04-28 23:36:49 +04:00
|
|
|
|
internal class UserLogic : IUserLogic
|
2024-04-28 21:20:10 +04:00
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
//private readonly IClientStorage _storage;
|
|
|
|
|
|
|
|
|
|
// todo нет интерфейса хранилища
|
2024-04-28 23:36:49 +04:00
|
|
|
|
public UserLogic(ILogger<UserLogic> logger)
|
|
|
|
|
{
|
2024-04-28 21:20:10 +04:00
|
|
|
|
_logger = logger;
|
|
|
|
|
//storage = _storage;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-28 23:08:09 +04:00
|
|
|
|
public bool Add(UserBindingModel model)
|
2024-04-28 21:20:10 +04:00
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
// todo логика добавления в _clientStorage:_clientStorage.Insert(model) == null
|
2024-04-28 23:36:49 +04:00
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
2024-04-28 21:20:10 +04:00
|
|
|
|
_logger.LogWarning("Add operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-28 23:08:09 +04:00
|
|
|
|
public bool Update(UserBindingModel model)
|
2024-04-28 21:20:10 +04:00
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
// todo логика добавления в _clientStorage:_clientStorage.Update(model) == null
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Update operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-28 23:08:09 +04:00
|
|
|
|
public bool Delete(UserBindingModel model)
|
2024-04-28 21:20:10 +04:00
|
|
|
|
{
|
|
|
|
|
CheckModel(model, false);
|
2024-04-28 23:36:49 +04:00
|
|
|
|
_logger.LogInformation($"Delete.ID:{model.ID}");
|
2024-04-28 21:20:10 +04:00
|
|
|
|
// todo логика добавления в _clientStorage:_clientStorage.Delete(model) == null
|
2024-04-28 23:36:49 +04:00
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
2024-04-28 21:20:10 +04:00
|
|
|
|
_logger.LogWarning("Delete operation failes");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-28 23:08:09 +04:00
|
|
|
|
public UserViewModel? ReadElemet(UserSearchModel? model)
|
2024-04-28 21:20:10 +04:00
|
|
|
|
{
|
2024-04-28 23:36:49 +04:00
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
2024-04-28 21:20:10 +04:00
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
2024-04-28 23:36:49 +04:00
|
|
|
|
_logger.LogInformation($"ReadElement: logint:{model.Login}.ID:{model.ID}");
|
2024-04-28 21:20:10 +04:00
|
|
|
|
// todo element = _clientStorage.GetElement(model);
|
|
|
|
|
var element = model;
|
2024-04-28 23:36:49 +04:00
|
|
|
|
if (element == null)
|
|
|
|
|
{
|
2024-04-28 23:09:52 +04:00
|
|
|
|
_logger.LogWarning("ReadElement element not fount");
|
2024-04-28 21:20:10 +04:00
|
|
|
|
return null;
|
|
|
|
|
}
|
2024-04-28 23:36:49 +04:00
|
|
|
|
_logger.LogInformation($"ReadElement: find.ID:{element.ID}");
|
2024-04-28 21:20:10 +04:00
|
|
|
|
// todo retun element;
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-28 23:08:09 +04:00
|
|
|
|
public List<UserViewModel>? ReadList(UserSearchModel? model)
|
2024-04-28 21:20:10 +04:00
|
|
|
|
{
|
2024-04-28 23:36:49 +04:00
|
|
|
|
|
|
|
|
|
_logger.LogInformation($"ReadList: ClientID:{model?.ID}");
|
2024-04-28 21:20:10 +04:00
|
|
|
|
// todo получение списка из хранилища, model == null ? _clientStorage.GetFullList() : _clientStorage.GetFilteredList(model);
|
|
|
|
|
var list = model;
|
2024-04-28 23:36:49 +04:00
|
|
|
|
if (list == null)
|
|
|
|
|
{
|
2024-04-28 21:20:10 +04:00
|
|
|
|
_logger.LogWarning("ReadList: return null list");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2024-04-28 23:09:52 +04:00
|
|
|
|
// toto ReadList:Count:{list.count},
|
|
|
|
|
_logger.LogInformation("ReadList:Count:{Count}");
|
2024-04-28 21:20:10 +04:00
|
|
|
|
// todo return list;
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-28 23:36:49 +04:00
|
|
|
|
private void CheckModel(UserBindingModel model, bool withParams = true)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
2024-04-28 21:20:10 +04:00
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
2024-04-28 23:36:49 +04:00
|
|
|
|
if (!withParams)
|
|
|
|
|
{
|
2024-04-28 21:20:10 +04:00
|
|
|
|
return;
|
|
|
|
|
}
|
2024-04-28 23:36:49 +04:00
|
|
|
|
if (string.IsNullOrEmpty(model.Login))
|
|
|
|
|
{
|
2024-04-28 21:20:10 +04:00
|
|
|
|
throw new ArgumentNullException("Нет логина пользователя", nameof(model.Login));
|
|
|
|
|
}
|
2024-04-28 23:36:49 +04:00
|
|
|
|
if (string.IsNullOrEmpty(model.FirstName))
|
|
|
|
|
{
|
2024-04-28 21:20:10 +04:00
|
|
|
|
throw new ArgumentNullException("Нет имени пользователя", nameof(model.FirstName));
|
|
|
|
|
}
|
2024-04-28 23:36:49 +04:00
|
|
|
|
if (string.IsNullOrEmpty(model.LastName))
|
|
|
|
|
{
|
2024-04-28 21:20:10 +04:00
|
|
|
|
throw new ArgumentNullException("Нет фамилии пользоватея", nameof(model.LastName));
|
|
|
|
|
}
|
2024-04-28 23:36:49 +04:00
|
|
|
|
if (string.IsNullOrEmpty(model.Email))
|
|
|
|
|
{
|
2024-04-28 21:20:10 +04:00
|
|
|
|
throw new ArgumentNullException("Нет почты клиента", nameof(model.Email));
|
|
|
|
|
}
|
2024-04-28 23:36:49 +04:00
|
|
|
|
if (string.IsNullOrEmpty(model.Password))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Нет пароля пользователя", nameof(model.Password));
|
2024-04-28 21:20:10 +04:00
|
|
|
|
}
|
2024-04-28 23:36:49 +04:00
|
|
|
|
if (string.IsNullOrEmpty(model.PhoneNumber))
|
|
|
|
|
{
|
2024-04-28 21:20:10 +04:00
|
|
|
|
throw new ArgumentNullException("Нет номер телефона пользователя", nameof(model.PhoneNumber));
|
|
|
|
|
}
|
2024-04-28 23:09:52 +04:00
|
|
|
|
_logger.LogInformation($"Client. Login:{model.Login}.FirstName:{model.FirstName}.LastName:{model.LastName}.Email:{model.Email}." +
|
2024-04-28 21:20:10 +04:00
|
|
|
|
$"Password:{model.Password}.PhoneNumber:{model.PhoneNumber}");
|
2024-04-28 23:09:52 +04:00
|
|
|
|
/*
|
|
|
|
|
var element = _clientStorage.GetElement(new ClientSearchModel
|
|
|
|
|
{
|
|
|
|
|
Login = model.Login
|
|
|
|
|
});
|
|
|
|
|
if (element != null && element.Id != model.ID)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Клиент с таким логином уже есть");
|
|
|
|
|
}
|
|
|
|
|
*/
|
2024-04-28 21:20:10 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|