78 lines
2.6 KiB
C#
Raw Normal View History

2025-02-24 23:09:17 +04:00
using Microsoft.Extensions.Logging;
using Squirrel.BusinessLogicsContracts;
using Squirrel.DataModels;
2025-02-27 15:22:45 +04:00
using Squirrel.Exceptions;
using Squirrel.Extensions;
2025-02-24 23:09:17 +04:00
using Squirrel.StoragesContracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
2025-02-27 15:22:45 +04:00
using System.Text.Json;
using System.Text.RegularExpressions;
2025-02-24 23:09:17 +04:00
using System.Threading.Tasks;
namespace SquirelBusinessLogic.Implementations;
2025-02-27 15:22:45 +04:00
internal class GuestBusinessLogicContract(IGuestStorageContract guestStorageContract, ILogger logger) : IGuestBusinessLogicContract
2025-02-24 23:09:17 +04:00
{
private readonly ILogger _logger = logger;
2025-02-27 15:22:45 +04:00
private readonly IGuestStorageContract _guestStorageContract = guestStorageContract;
2025-02-24 23:09:17 +04:00
public List<GuestDataModel> GetAllGuests()
{
2025-02-27 15:22:45 +04:00
_logger.LogInformation("GetAllGuests");
return _guestStorageContract.GetList() ?? throw new NullListException();
2025-02-24 23:09:17 +04:00
}
public GuestDataModel GetGuestByData(string data)
{
2025-02-27 15:22:45 +04:00
_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);
2025-02-24 23:09:17 +04:00
}
public void InsertGuest(GuestDataModel guestDataModel)
{
2025-02-27 15:22:45 +04:00
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(guestDataModel));
ArgumentNullException.ThrowIfNull(guestDataModel); guestDataModel.Validate();
_guestStorageContract.AddElement(guestDataModel);
2025-02-24 23:09:17 +04:00
}
public void UpdateGuest(GuestDataModel guestDataModel)
{
2025-02-27 15:22:45 +04:00
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(guestDataModel));
ArgumentNullException.ThrowIfNull(guestDataModel);
guestDataModel.Validate();
_guestStorageContract.UpdElement(guestDataModel);
2025-02-24 23:09:17 +04:00
}
public void DeleteGuest(string id)
{
2025-02-27 15:22:45 +04:00
_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);
2025-02-24 23:09:17 +04:00
}
}