78 lines
2.6 KiB
C#
78 lines
2.6 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using Squirrel.BusinessLogicsContracts;
|
|
using Squirrel.DataModels;
|
|
using Squirrel.Exceptions;
|
|
using Squirrel.Extensions;
|
|
using Squirrel.StoragesContracts;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace SquirelBusinessLogic.Implementations;
|
|
|
|
internal class GuestBusinessLogicContract(IGuestStorageContract guestStorageContract, ILogger logger) : IGuestBusinessLogicContract
|
|
{
|
|
private readonly ILogger _logger = logger;
|
|
private readonly IGuestStorageContract _guestStorageContract = guestStorageContract;
|
|
public List<GuestDataModel> GetAllGuests()
|
|
{
|
|
_logger.LogInformation("GetAllGuests");
|
|
|
|
return _guestStorageContract.GetList() ?? throw new NullListException();
|
|
}
|
|
public GuestDataModel GetGuestByData(string data)
|
|
{
|
|
_logger.LogInformation("Get element by data: {data}", data);
|
|
|
|
if (data.IsEmpty())
|
|
{
|
|
throw new ArgumentNullException(nameof(data));
|
|
}
|
|
|
|
if (data.IsGuid())
|
|
{
|
|
return _guestStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
|
|
}
|
|
|
|
if (Regex.IsMatch(data, @"^((8|\+7)[\-]?)?(\(?\d{3}\)?[\-]?)?[\d]{7,10}$"))
|
|
{
|
|
return _guestStorageContract.GetElementByPhoneNumber(data) ??
|
|
throw new ElementNotFoundException(data);
|
|
}
|
|
|
|
return _guestStorageContract.GetElementByFIO(data) ?? throw new ElementNotFoundException(data);
|
|
}
|
|
public void InsertGuest(GuestDataModel guestDataModel)
|
|
{
|
|
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(guestDataModel));
|
|
ArgumentNullException.ThrowIfNull(guestDataModel); guestDataModel.Validate();
|
|
_guestStorageContract.AddElement(guestDataModel);
|
|
}
|
|
public void UpdateGuest(GuestDataModel guestDataModel)
|
|
{
|
|
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(guestDataModel));
|
|
ArgumentNullException.ThrowIfNull(guestDataModel);
|
|
guestDataModel.Validate();
|
|
_guestStorageContract.UpdElement(guestDataModel);
|
|
}
|
|
public void DeleteGuest(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");
|
|
}
|
|
_guestStorageContract.DelElement(id);
|
|
}
|
|
}
|