96 lines
2.3 KiB
C#
96 lines
2.3 KiB
C#
using Contracts.BindingModels;
|
|
using Contracts.BusinessLogicContracts;
|
|
using Contracts.Converters;
|
|
using Contracts.Exceptions;
|
|
using Contracts.SearchModels;
|
|
using Contracts.StorageContracts;
|
|
using Contracts.ViewModels;
|
|
using Microsoft.Extensions.Logging;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace BusinessLogic.BusinessLogic
|
|
{
|
|
public class UserLogic : IUserLogic
|
|
{
|
|
private readonly ILogger _logger;
|
|
private readonly IUserStorage _userStorage;
|
|
|
|
public UserLogic(ILogger<UserLogic> logger, IUserStorage userStorage)
|
|
{
|
|
_logger = logger;
|
|
_userStorage = userStorage;
|
|
}
|
|
|
|
public UserViewModel Create(UserBindingModel model)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(model);
|
|
|
|
var user = _userStorage.Insert(model);
|
|
if (user is null)
|
|
{
|
|
throw new Exception("Insert operation failed.");
|
|
}
|
|
|
|
return UserConverter.ToView(user);
|
|
}
|
|
|
|
public UserViewModel Delete(UserSearchModel model)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(model);
|
|
|
|
_logger.LogInformation("Delete user. Id: {0}", model.Id);
|
|
var user = _userStorage.Delete(model);
|
|
if (user is null)
|
|
{
|
|
throw new Exception("Update operation failed.");
|
|
}
|
|
|
|
return UserConverter.ToView(user);
|
|
}
|
|
|
|
public IEnumerable<UserViewModel> ReadElements(UserSearchModel? model)
|
|
{
|
|
_logger.LogInformation("ReadList. Id: {Id}", model?.Id);
|
|
var list = _userStorage.GetList(model);
|
|
if (list is null || list.Count() == 0)
|
|
{
|
|
_logger.LogWarning("ReadList return null list");
|
|
return [];
|
|
}
|
|
_logger.LogInformation("ReadList. Count: {Count}", list.Count());
|
|
|
|
return list.Select(UserConverter.ToView);
|
|
}
|
|
|
|
public UserViewModel ReadElement(UserSearchModel model)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(model);
|
|
|
|
_logger.LogInformation("ReadElement. Id: {0}", model.Id);
|
|
var user = _userStorage.GetElement(model);
|
|
if (user is null)
|
|
{
|
|
throw new ElementNotFoundException();
|
|
}
|
|
_logger.LogInformation("ReadElement find. Id: {0}", user.Id);
|
|
|
|
return UserConverter.ToView(user);
|
|
}
|
|
|
|
public UserViewModel Update(UserBindingModel model)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(model);
|
|
|
|
var user = _userStorage.Update(model);
|
|
if (user is null)
|
|
{
|
|
throw new Exception("Update operation failed.");
|
|
}
|
|
return UserConverter.ToView(user);
|
|
}
|
|
}
|
|
} |