forked from slavaxom9k/PIBD-23_Fomichev_V.S._MagicCarpet
84 lines
2.9 KiB
C#
84 lines
2.9 KiB
C#
using MagicCarpetContracts.BuisnessLogicContracts;
|
|
using MagicCarpetContracts.DataModels;
|
|
using MagicCarpetContracts.Exceptions;
|
|
using MagicCarpetContracts.Extensions;
|
|
using MagicCarpetContracts.StoragesContracts;
|
|
using Microsoft.Extensions.Logging;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MagicCarpetBusinessLogic.Implementations;
|
|
|
|
internal class TourBusinessLogicContract(ITourStorageContract tourStorageContract, ILogger logger) : ITourBusinessLogicContract
|
|
{
|
|
private readonly ILogger _logger = logger;
|
|
private readonly ITourStorageContract _tourStorageContract = tourStorageContract;
|
|
public List<TourDataModel> GetAllTours()
|
|
{
|
|
_logger.LogInformation("GetAllTours");
|
|
return _tourStorageContract.GetList() ?? throw new NullListException();
|
|
}
|
|
|
|
public List<TourHistoryDataModel> GetTourHistoryByTour(string tourId)
|
|
{
|
|
_logger.LogInformation("GetTourHistoryByTour for {tourId}", tourId);
|
|
if (tourId.IsEmpty())
|
|
{
|
|
throw new ArgumentNullException(nameof(tourId));
|
|
}
|
|
if (!tourId.IsGuid())
|
|
{
|
|
throw new ValidationException("The value in the field tourId is not a unique identifier.");
|
|
}
|
|
return _tourStorageContract.GetHistoryByTourId(tourId) ?? throw new NullListException();
|
|
}
|
|
|
|
public TourDataModel GetTourByData(string data)
|
|
{
|
|
_logger.LogInformation("Get element by data: {data}", data);
|
|
if (data.IsEmpty())
|
|
{
|
|
throw new ArgumentNullException(nameof(data));
|
|
}
|
|
if (data.IsGuid())
|
|
{
|
|
return _tourStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
|
|
}
|
|
return _tourStorageContract.GetElementByName(data) ?? throw new ElementNotFoundException(data);
|
|
}
|
|
|
|
public void InsertTour(TourDataModel tourDataModel)
|
|
{
|
|
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(tourDataModel));
|
|
ArgumentNullException.ThrowIfNull(tourDataModel);
|
|
tourDataModel.Validate();
|
|
_tourStorageContract.AddElement(tourDataModel);
|
|
}
|
|
|
|
public void UpdateTour(TourDataModel tourDataModel)
|
|
{
|
|
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(tourDataModel));
|
|
ArgumentNullException.ThrowIfNull(tourDataModel);
|
|
tourDataModel.Validate();
|
|
_tourStorageContract.UpdElement(tourDataModel);
|
|
}
|
|
|
|
public void DeleteTour(string id)
|
|
{
|
|
_logger.LogInformation("Delete by id: {id}", id);
|
|
if (id.IsEmpty())
|
|
{
|
|
throw new ArgumentNullException(nameof(id));
|
|
}
|
|
if (!id.IsGuid())
|
|
{
|
|
throw new ValidationException("Id is not a unique identifier");
|
|
}
|
|
_tourStorageContract.DelElement(id);
|
|
}
|
|
}
|