astral-api/Astral.Services/Services/UserGroupService.cs

138 lines
4.3 KiB
C#
Raw Normal View History

2024-12-11 21:36:30 +01:00
// <copyright file="UserGroupService.cs" company="alveus.dev">
// Copyright (c) alveus.dev. All rights reserved. Licensed under the MIT License.
// </copyright>
using Astral.Core.Entities;
using Astral.Core.Exceptions;
using Astral.Core.RepositoryInterfaces;
using Astral.Services.Dtos;
using Astral.Services.Exceptions;
using Astral.Services.Interfaces;
using AutoMapper;
using Injectio.Attributes;
using Microsoft.Extensions.Logging;
namespace Astral.Services.Services;
/// <inheritdoc />
[RegisterScoped]
public class UserGroupService : IUserGroupService
{
private readonly ILogger<UserGroupService> _logger;
private readonly IMapper _mapper;
private readonly IUserGroupRepository _userGroupRepository;
private readonly IUserRepository _userRepository;
/// <summary>
/// Initializes a new instance of the <see cref="UserGroupService" /> class.
/// </summary>
/// <param name="userGroupRepository">Instance of <see cref="IUserGroupRepository" />.</param>
/// <param name="userRepository">Instance of <see cref="IUserRepository" />.</param>
/// <param name="mapper">Instance of <see cref="IMapper" />.</param>
/// <param name="logger">Instance of <see cref="ILogger" />.</param>
public UserGroupService(
IUserGroupRepository userGroupRepository,
IUserRepository userRepository,
IMapper mapper,
ILogger<UserGroupService> logger)
{
_userGroupRepository = userGroupRepository;
_userRepository = userRepository;
_mapper = mapper;
_logger = logger;
}
/// <inheritdoc />
public async Task<UserGroupDto> CreateUserGroup(Guid ownerUserId, string title, string description)
{
var user = await _userRepository.FindByIdAsync(ownerUserId);
if (user is null)
{
_logger.LogError("Attempted to create a user group for a user that does not exist: {userId}", ownerUserId);
throw new UserNotFoundException();
}
var uniqueId = await GetUniqueId();
var userGroup = new UserGroup
{
Id = uniqueId,
CreatedAt = DateTime.UtcNow,
OwnerUserId = ownerUserId,
Title = title,
Description = description ?? string.Empty,
Internal = false
};
_logger.LogInformation(
"Creating new user group {groupTitle} [{groupId}] for user {user} [{userId}]",
userGroup.Title,
userGroup.Id,
user.Username,
user.Id);
await _userGroupRepository.AddAsync(userGroup);
return _mapper.Map<UserGroupDto>(userGroup);
}
/// <inheritdoc />
public async Task<UserGroupDto> CreateInternalGroup(Guid ownerUserId, string title, string description)
{
var user = await _userRepository.FindByIdAsync(ownerUserId);
if (user is null)
{
_logger.LogError("Attempted to create a user group for a user that does not exist: {userId}", ownerUserId);
throw new UserNotFoundException();
}
var uniqueId = await GetUniqueId();
var userGroup = new UserGroup
{
Id = uniqueId,
CreatedAt = DateTime.UtcNow,
OwnerUserId = ownerUserId,
Title = title,
Description = description ?? string.Empty,
Internal = true
};
_logger.LogInformation(
"Creating new internal user group {groupTitle} [{groupId}] for user {user} [{userId}]",
userGroup.Title,
userGroup.Id,
user.Username,
user.Id);
await _userGroupRepository.AddAsync(userGroup);
return _mapper.Map<UserGroupDto>(userGroup);
}
/// <summary>
/// Create a unique guid.
/// </summary>
/// <returns>The new guid.</returns>
private async Task<Guid> GetUniqueId()
{
var attempt = 0;
const int maxAttempts = 10;
var newId = Guid.NewGuid();
while (await _userGroupRepository.FindByIdAsync(newId) is not null)
{
attempt++;
if (attempt >= maxAttempts)
{
_logger.LogCritical("Unable to generate a unique guid for user group");
throw new UnexpectedErrorException();
}
newId = Guid.NewGuid();
}
return newId;
}
}