Files
aspnet-microservice-template/Services/UsersProfile/UserProfileService.cs

122 lines
4.0 KiB
C#
Executable File

using AutoMapper;
using GamificationService.Database.Repositories;
using GamificationService.Exceptions.Services.ProfileService;
using GamificationService.Models.Database;
using GamificationService.Models.DTO;
namespace GamificationService.Services.UsersProfile;
public class UserProfileService : IUserProfileService
{
private readonly UnitOfWork _unitOfWork;
# region Services
private readonly ILogger<UserProfileService> _logger;
private readonly IMapper _mapper;
#endregion
#region Constructor
public UserProfileService(UnitOfWork unitOfWork, ILogger<UserProfileService> logger, IMapper mapper)
{
_unitOfWork = unitOfWork;
_logger = logger;
_mapper = mapper;
}
#endregion
# region Methods
public async Task<UserProfileDTO> AddUserProfile(long userId, UserProfileCreateDTO userProfile)
{
UserProfile userProfileEntity = _mapper.Map<UserProfile>(userProfile);
userProfileEntity.UserId = userId;
return _mapper.Map<UserProfileDTO>(await AddUserProfile(userProfileEntity));
}
public async Task<UserProfile> AddUserProfile(UserProfile userProfile)
{
UserProfile userProfileEntity = userProfile;
// Make sure a user profile for the given user does not exist yet
if (_unitOfWork.UserProfileRepository.Get(x => x.UserId == userProfile.UserId).Any())
{
_logger.LogWarning("A user profile already exists for the given user id: {UserId}", userProfile.UserId);
throw new ProfileExistsException($"{userProfile.UserId}");
}
await _unitOfWork.UserProfileRepository.InsertAsync(userProfileEntity);
if (await _unitOfWork.SaveAsync())
{
_logger.LogInformation("User profile added for user id: {UserId}", userProfile.UserId);
return userProfileEntity;
}
_logger.LogError("Failed to add user profile for user id: {UserId}", userProfile.UserId);
throw new ProfileCreationException();
}
public UserProfile? GetUserProfileByUserId(long id)
{
return _unitOfWork.UserProfileRepository.Get(x => x.UserId == id).FirstOrDefault();
}
public UserProfile? GetUserProfileById(long id)
{
return _unitOfWork.UserProfileRepository.GetByID(id);
}
public async Task<bool> UpdateUserProfileByUserId(long userId, UserProfileCreateDTO userProfile)
{
var userProfileEntityUpdated = _mapper.Map<UserProfile>(userProfile);
var profile = _unitOfWork.UserProfileRepository
.Get(x => x.UserId == userId).FirstOrDefault() ?? throw new ProfileNotFoundException($"{userId}");
userProfileEntityUpdated.Id = profile.Id;
return await UpdateUserProfile(userProfileEntityUpdated);
}
public async Task<bool> UpdateUserProfile(UserProfile userProfile)
{
var userProfileEntityUpdated = userProfile;
var userProfileEntity = await _unitOfWork.UserProfileRepository.GetByIDAsync(userProfileEntityUpdated.Id);
if (userProfileEntity == null)
{
throw new ProfileNotFoundException($"{userProfileEntityUpdated.Id}");
}
_mapper.Map(userProfileEntityUpdated, userProfileEntity);
if (!await _unitOfWork.SaveAsync())
{
throw new ProfileUpdateException($"Failed to update user profile {userProfileEntityUpdated.Id}");
}
_logger.LogInformation("User profile updated for user id: {UserId}", userProfile.UserId);
return true;
}
public bool DeleteUserProfile(long id)
{
var profile = _unitOfWork.UserProfileRepository.GetByID(id);
if (profile == null)
{
throw new ProfileNotFoundException($"{id}");
}
_unitOfWork.UserProfileRepository.Delete(id);
if (_unitOfWork.Save())
{
_logger.LogInformation("User profile deleted: {UserId}", id);
return true;
}
throw new ProfileDeletionException($"Failed to delete user profile {id}");
}
#endregion
}