129 lines
3.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using HotelContracts.BindingModels;
using HotelContracts.BusinessLogicsContracts;
using HotelContracts.SearchModels;
using HotelContracts.StoragesContracts;
using HotelContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HotelBusinessLogic.BusinessLogic
{
public class DinnerLogic : IDinnerLogic
{
private readonly ILogger _logger;
private readonly IDinnerStorage _dinnerStorage;
public DinnerLogic(ILogger<DinnerLogic> logger, IDinnerStorage dinnerStorage)
{
_logger = logger;
_dinnerStorage = dinnerStorage;
}
public List<DinnerViewModel>? ReadList(DinnerSearchModel? model)
{
_logger.LogInformation("ReadList. DinnerName:{DinnerName}.Id:{ Id}", model?.DinnerName, model?.Id);
var list = model == null ? _dinnerStorage.GetFullList() : _dinnerStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public DinnerViewModel? ReadElement(DinnerSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. DinnerName:{DinnerName}.Id:{Id}", model.DinnerName, model.Id);
var element = _dinnerStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public bool Create(DinnerBindingModel model)
{
CheckModel(model);
if (_dinnerStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(DinnerBindingModel model)
{
CheckModel(model);
if (_dinnerStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(DinnerBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_dinnerStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(DinnerBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.DinnerName))
{
throw new ArgumentNullException("Нет имени обеда", nameof(model.DinnerName));
}
if (model.DinnerPrice < 0)
{
throw new ArgumentNullException("Стоимость обеда не может быть меньше 0", nameof(model.DinnerPrice));
}
if (model.DinnerСalorieСontent < 0)
{
throw new ArgumentNullException("Калорийность обеда не может быть меньше 0", nameof(model.DinnerСalorieСontent));
}
_logger.LogInformation("Dinner. DinnerName:{DinnerName}.DinnerPrice:{DinnerPrice}.DinnerСalorieСontent:{DinnerСalorieСontent}. Id: { Id}", model.DinnerName, model.DinnerPrice, model.DinnerСalorieСontent, model.Id);
}
}
}