142 lines
4.5 KiB
C#
142 lines
4.5 KiB
C#
|
using CanteenContracts.BindingModels;
|
|||
|
using CanteenContracts.BusinessLogicsContracts;
|
|||
|
using CanteenContracts.SearchModel;
|
|||
|
using CanteenContracts.StoragesContracts;
|
|||
|
using CanteenContracts.View;
|
|||
|
using Microsoft.Extensions.Logging;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace CanteenBusinessLogic.BusinessLogics
|
|||
|
{
|
|||
|
public class VisitorLogic : IVisitorLogic
|
|||
|
{
|
|||
|
private readonly ILogger _logger;
|
|||
|
private readonly IVisitorStorage _visitorStorage;
|
|||
|
|
|||
|
public VisitorLogic(ILogger<VisitorLogic> logger, IVisitorStorage visitorStorage)
|
|||
|
{
|
|||
|
_logger = logger;
|
|||
|
_visitorStorage = visitorStorage;
|
|||
|
}
|
|||
|
|
|||
|
public bool Create(VisitorBindingModel model)
|
|||
|
{
|
|||
|
CheckModel(model);
|
|||
|
|
|||
|
if (_visitorStorage.Insert(model) == null)
|
|||
|
{
|
|||
|
_logger.LogWarning("Insert operation failed");
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
public bool Delete(VisitorBindingModel model)
|
|||
|
{
|
|||
|
CheckModel(model, false);
|
|||
|
|
|||
|
_logger.LogInformation("Delete. Id: {Id}", model.Id);
|
|||
|
if (_visitorStorage.Delete(model) == null)
|
|||
|
{
|
|||
|
_logger.LogWarning("Delete operation failed");
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
public VisitorViewModel? ReadElement(VisitorSearchModel model)
|
|||
|
{
|
|||
|
if (model == null)
|
|||
|
{
|
|||
|
throw new ArgumentNullException(nameof(model));
|
|||
|
}
|
|||
|
|
|||
|
_logger.LogInformation("ReadElement. Login: {Login}. Id: {Id}", model.Login, model.Id);
|
|||
|
|
|||
|
var element = _visitorStorage.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<VisitorViewModel>? ReadList(VisitorSearchModel? model)
|
|||
|
{
|
|||
|
_logger.LogInformation("ReadList. Login: {Login}. Id: {Id}", model.Login, model.Id);
|
|||
|
|
|||
|
var list = model == null ? _visitorStorage.GetFullList() : _visitorStorage.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(VisitorBindingModel model)
|
|||
|
{
|
|||
|
CheckModel(model);
|
|||
|
|
|||
|
if (_visitorStorage.Update(model) == null)
|
|||
|
{
|
|||
|
_logger.LogWarning("Update operation failed");
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
return true;
|
|||
|
}
|
|||
|
private void CheckModel(VisitorBindingModel model, bool withParams = true)
|
|||
|
{
|
|||
|
if (model == null)
|
|||
|
{
|
|||
|
throw new ArgumentNullException(nameof(model));
|
|||
|
}
|
|||
|
|
|||
|
if (!withParams)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
if (string.IsNullOrEmpty(model.FIO))
|
|||
|
{
|
|||
|
throw new ArgumentNullException("Нет ФИО у посетителя", nameof(model.FIO));
|
|||
|
}
|
|||
|
|
|||
|
if (string.IsNullOrEmpty(model.Login))
|
|||
|
{
|
|||
|
throw new ArgumentNullException("Нет логина у посетителя", nameof(model.Login));
|
|||
|
}
|
|||
|
|
|||
|
if (string.IsNullOrEmpty(model.Password))
|
|||
|
{
|
|||
|
throw new ArgumentNullException("Нет пароля у посетителя", nameof(model.Password));
|
|||
|
}
|
|||
|
|
|||
|
if (string.IsNullOrEmpty(model.PhoneNumber))
|
|||
|
{
|
|||
|
throw new ArgumentNullException("У номер телефона у посетителя", nameof(model.PhoneNumber));
|
|||
|
}
|
|||
|
|
|||
|
_logger.LogInformation("Visitor. Login: {Login}. Password: {Password}. PhoneNumber: {PhoneNumber}. Id: {Id}", model.Login, model.Password, model.PhoneNumber, model.Id);
|
|||
|
|
|||
|
var element = _visitorStorage.GetElement(new VisitorSearchModel { Login = model.Login, Password = model.Password });
|
|||
|
if (element != null && element.Id != model.Id)
|
|||
|
{
|
|||
|
throw new InvalidOperationException("Такой посетитель уже есть");
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|