Coursework_ComputerStore_Li.../ComputerStoreBusinessLogic/BusinessLogic/SellerLogic.cs

123 lines
4.0 KiB
C#
Raw Normal View History

using ComputerStoreContracts.BindingModels;
using ComputerStoreContracts.BusinessLogicContracts;
using ComputerStoreContracts.SearchModels;
using ComputerStoreContracts.StorageContracts;
using ComputerStoreContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerStoreBusinessLogic.BusinessLogic
{
public class SellerLogic : ISellerLogic
{
private readonly ILogger _logger;
private readonly ISellerStorage _sellerStorage;
public SellerLogic(ILogger<SellerLogic> logger, ISellerStorage sellerStorage)
{
_logger = logger;
_sellerStorage = sellerStorage;
}
public bool Create(SellerBindingModel model)
{
CheckSeller(model);
if (_sellerStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(SellerBindingModel model)
{
CheckSeller(model);
if (_sellerStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(SellerBindingModel model)
{
CheckSeller(model, false);
_logger.LogInformation("Delete. ID:{ID}", model.ID);
if (_sellerStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public SellerViewModel? ReadElement(SellerSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Username:{Username}.ID:{ ID}", model.Username, model.ID);
var element = _sellerStorage.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<SellerViewModel>? ReadList(SellerSearchModel? model)
{
_logger.LogInformation("ReadList. Username:{Username}. ID:{ ID}", model?.Username, model?.ID);
var list = model == null ? _sellerStorage.GetFullList() : _sellerStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public void CheckSeller(SellerBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Username))
{
throw new ArgumentNullException("Invalid username of user", nameof(model.Username));
}
if (string.IsNullOrEmpty(model.Password))
{
throw new ArgumentNullException("Invalid password of user", nameof(model.Password));
}
_logger.LogInformation("Client. Username:{ Username}. ID: { ID} ", model.Username, model.ID);
var element = _sellerStorage.GetElement(new SellerSearchModel
{
Username = model.Username
});
if (element != null && element.ID != model.ID)
{
throw new InvalidOperationException("Seller with such username already exists.");
}
}
}
}