chore: deleted all the stuff we don't need here
This commit is contained in:
@@ -1,47 +0,0 @@
|
||||
using GamificationService.Exceptions.UtilServices.Cookies;
|
||||
|
||||
namespace GamificationService.Services.Cookies;
|
||||
|
||||
public class CookieService : ICookieService
|
||||
{
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
private readonly ILogger<CookieService> _logger;
|
||||
private readonly IConfiguration _configuration;
|
||||
|
||||
public CookieService(IHttpContextAccessor httpContextAccessor, ILogger<CookieService> logger, IConfiguration configuration)
|
||||
{
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
_logger = logger;
|
||||
_configuration = configuration;
|
||||
}
|
||||
|
||||
public Task<bool> SetCookie(string key, string value, CookieOptions options)
|
||||
{
|
||||
try
|
||||
{
|
||||
_logger.LogDebug("Adding cookie {CookieKey} with value {CookieValue}", key, value);
|
||||
_httpContextAccessor.HttpContext.Response.Cookies.Append(key, value, options);
|
||||
return Task.FromResult(true);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to add cookie {CookieKey}", key);
|
||||
throw new SetCookiesException(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> RemoveCookie(string key)
|
||||
{
|
||||
try
|
||||
{
|
||||
_logger.LogDebug("Deleting cookie {CookieKey}", key);
|
||||
_httpContextAccessor.HttpContext.Response.Cookies.Delete(key);
|
||||
return await Task.FromResult(true);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to delete cookie {CookieKey}", key);
|
||||
throw new DeleteCookiesException(ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
namespace GamificationService.Services.Cookies;
|
||||
|
||||
public interface ICookieService
|
||||
{
|
||||
Task<bool> SetCookie(string key, string value, CookieOptions options);
|
||||
Task<bool> RemoveCookie(string key);
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
using GamificationService.Models;
|
||||
|
||||
namespace GamificationService.Services.CurrentUsers;
|
||||
|
||||
public class CurrentUserService : ICurrentUserService
|
||||
{
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
private readonly ILogger<ICurrentUserService> _logger;
|
||||
public CurrentUserService(IHttpContextAccessor httpContextAccessor, ILogger<ICurrentUserService> logger)
|
||||
{
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public UserSession GetCurrentUser()
|
||||
{
|
||||
UserSession currentUser = new UserSession
|
||||
{
|
||||
IsAuthenticated = _httpContextAccessor.HttpContext.User.Identity != null && _httpContextAccessor.HttpContext.User.Identity.IsAuthenticated,
|
||||
Login = _httpContextAccessor.HttpContext.User.Identity.Name
|
||||
};
|
||||
_logger.LogDebug($"Current user extracted: {currentUser.Login}");
|
||||
return currentUser;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
using GamificationService.Models;
|
||||
|
||||
namespace GamificationService.Services.CurrentUsers;
|
||||
|
||||
public interface ICurrentUserService
|
||||
{
|
||||
UserSession GetCurrentUser();
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using GamificationService.Models.Database;
|
||||
using GamificationService.Models.DTO;
|
||||
|
||||
namespace GamificationService.Services.JWT;
|
||||
|
||||
public interface IJwtService
|
||||
{
|
||||
string GenerateAccessToken(ApplicationUser user);
|
||||
JwtSecurityToken ValidateAccessToken(string token);
|
||||
Task<RefreshToken> GenerateRefreshTokenAsync(ApplicationUser user);
|
||||
Task RevokeRefreshTokenAsync(long userId, string refreshToken, string remoteIpAddress);
|
||||
}
|
||||
@@ -1,144 +0,0 @@
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using GamificationService.Database.Repositories;
|
||||
using GamificationService.Exceptions.Services.JwtService;
|
||||
using GamificationService.Exceptions.UtilServices.JWT;
|
||||
using GamificationService.Models.Database;
|
||||
using GamificationService.Models.DTO;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
|
||||
namespace GamificationService.Services.JWT;
|
||||
|
||||
public class JwtService : IJwtService
|
||||
{
|
||||
#region Fields
|
||||
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly ILogger<JwtService> _logger;
|
||||
private readonly UnitOfWork _unitOfWork;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
public JwtService(IConfiguration configuration, ILogger<JwtService> logger, UnitOfWork unitOfWork)
|
||||
{
|
||||
_configuration = configuration;
|
||||
_logger = logger;
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public string GenerateAccessToken(ApplicationUser user)
|
||||
{
|
||||
var jwtSettings = _configuration.GetSection("JwtSettings");
|
||||
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSettings["Key"]));
|
||||
var issuer = jwtSettings["Issuer"];
|
||||
var audience = jwtSettings["Audience"];
|
||||
|
||||
var claims = new List<Claim>
|
||||
{
|
||||
new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
|
||||
new Claim(ClaimTypes.Name, user.UserName),
|
||||
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())
|
||||
};
|
||||
|
||||
var userRoles = _unitOfWork.UserRoleRepository.Get()
|
||||
.Where(ur => ur.UserId == user.Id)
|
||||
.Select(ur => ur.Role)
|
||||
.Include(rr => rr.RoleRights)
|
||||
.ThenInclude(rr=>rr.Right)
|
||||
.ToList();
|
||||
|
||||
foreach (var role in userRoles)
|
||||
{
|
||||
claims.Add(new Claim(ClaimTypes.Role, role.Name));
|
||||
|
||||
foreach (var right in role.RoleRights.Select(rr => rr.Right))
|
||||
{
|
||||
claims.Add(new Claim("Right", right.Name));
|
||||
}
|
||||
}
|
||||
|
||||
var expires = DateTime.UtcNow.AddMinutes(double.Parse(jwtSettings["AccessTokenExpirationMinutes"]));
|
||||
|
||||
var token = new JwtSecurityToken(
|
||||
issuer: issuer,
|
||||
audience: audience,
|
||||
claims: claims,
|
||||
expires: expires,
|
||||
signingCredentials: new SigningCredentials(key, SecurityAlgorithms.HmacSha256)
|
||||
);
|
||||
|
||||
return new JwtSecurityTokenHandler().WriteToken(token);
|
||||
}
|
||||
|
||||
private string GenerateRefreshToken()
|
||||
{
|
||||
var randomNumber = new byte[32];
|
||||
using var rng = RandomNumberGenerator.Create();
|
||||
rng.GetBytes(randomNumber);
|
||||
return Convert.ToBase64String(randomNumber);
|
||||
}
|
||||
|
||||
public JwtSecurityToken ValidateAccessToken(string token)
|
||||
{
|
||||
var jwtSettings = _configuration.GetSection("JwtSettings");
|
||||
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSettings["Key"]));
|
||||
var issuer = jwtSettings["Issuer"];
|
||||
var audience = jwtSettings["Audience"];
|
||||
|
||||
var tokenHandler = new JwtSecurityTokenHandler();
|
||||
var validationParameters = new TokenValidationParameters
|
||||
{
|
||||
ValidateIssuerSigningKey = true,
|
||||
IssuerSigningKey = key,
|
||||
ValidateIssuer = true,
|
||||
ValidIssuer = issuer,
|
||||
ValidateAudience = true,
|
||||
ValidAudience = audience,
|
||||
ValidateLifetime = true,
|
||||
ClockSkew = TimeSpan.Zero
|
||||
};
|
||||
|
||||
SecurityToken validatedToken;
|
||||
var principal = tokenHandler.ValidateToken(token, validationParameters, out validatedToken);
|
||||
|
||||
return validatedToken as JwtSecurityToken;
|
||||
}
|
||||
|
||||
public async Task<RefreshToken> GenerateRefreshTokenAsync(ApplicationUser user)
|
||||
{
|
||||
var dbRefreshToken = new RefreshToken
|
||||
{
|
||||
UserId = user.Id,
|
||||
Token = GenerateRefreshToken(),
|
||||
Expires = DateTime.UtcNow.AddDays(double.Parse(_configuration["JwtSettings:RefreshTokenExpirationDays"])),
|
||||
Created = DateTime.UtcNow
|
||||
};
|
||||
|
||||
await _unitOfWork.RefreshTokenRepository.InsertAsync(dbRefreshToken);
|
||||
if (!await _unitOfWork.SaveAsync())
|
||||
{
|
||||
throw new GenerateRefreshTokenException("Failed to generate refresh token");
|
||||
}
|
||||
|
||||
return dbRefreshToken;
|
||||
}
|
||||
|
||||
public async Task RevokeRefreshTokenAsync(long userId, string refreshToken, string remoteIpAddress)
|
||||
{
|
||||
var token = await _unitOfWork.RefreshTokenRepository.Get()
|
||||
.FirstOrDefaultAsync(x => x.UserId == userId && x.Token == refreshToken);
|
||||
|
||||
if (token != null)
|
||||
{
|
||||
token.IsRevoked = true;
|
||||
token.RevokedByIp = remoteIpAddress;
|
||||
token.RevokedOn = DateTime.UtcNow;
|
||||
|
||||
await _unitOfWork.SaveAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
using System.Net.Mail;
|
||||
using GamificationService.Models.Database;
|
||||
using GamificationService.Utils;
|
||||
|
||||
namespace GamificationService.Services.NotificationService;
|
||||
|
||||
public interface INotificationService
|
||||
{
|
||||
public Task SendMailNotificationAsync(ApplicationUser user, Notification notification);
|
||||
public Task SendPushNotificationAsync(ApplicationUser user, Notification notification);
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
using System.Net.Mail;
|
||||
using GamificationService.Models.Database;
|
||||
using GamificationService.Utils;
|
||||
using GamificationService.Utils.Factory;
|
||||
|
||||
namespace GamificationService.Services.NotificationService;
|
||||
|
||||
public class NotificationService : INotificationService
|
||||
{
|
||||
#region Services
|
||||
|
||||
private readonly EmailClient _emailClient;
|
||||
private readonly ILogger<NotificationService> _logger;
|
||||
private readonly PushNotificationsClient _pushNotificationsClient;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
|
||||
public NotificationService(EmailClient emailClient, PushNotificationsClient pushNotificationsClient, ILogger<NotificationService> logger)
|
||||
{
|
||||
_emailClient = emailClient;
|
||||
_pushNotificationsClient = pushNotificationsClient;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
public async Task SendMailNotificationAsync(ApplicationUser user, Notification notification)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _emailClient.SendEmail(((MailNotification)notification).ConvertToMailMessage(), user.Email);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e,e.Message);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: Refactor, add reg.ru notifications
|
||||
public async Task SendPushNotificationAsync(ApplicationUser user, Notification notification)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _emailClient.SendEmail(((MailNotification)notification).ConvertToMailMessage(), user.Email);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e,e.Message);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
using GamificationService.Models.Database;
|
||||
|
||||
namespace GamificationService.Services.Rights;
|
||||
|
||||
public interface IRightsService
|
||||
{
|
||||
Task<Right?> CreateRightAsync(string rightName, string description);
|
||||
Task<bool> UpdateRightAsync(long rightId, string newRightName, string newDescription);
|
||||
Task<bool> DeleteRightAsync(long rightId);
|
||||
Task<Right?> GetRightByIdAsync(long rightId);
|
||||
Task<(List<Right> Rights, int TotalCount)> GetAllRightsAsync(int pageNumber = 1, int pageSize = 10);
|
||||
}
|
||||
@@ -1,103 +0,0 @@
|
||||
using GamificationService.Database.Repositories;
|
||||
using GamificationService.Models.Database;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace GamificationService.Services.Rights;
|
||||
|
||||
public class RightsService : IRightsService
|
||||
{
|
||||
#region Fields
|
||||
|
||||
private readonly UnitOfWork _unitOfWork;
|
||||
private readonly ILogger<IRightsService> _logger;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
|
||||
public RightsService(UnitOfWork unitOfWork, ILogger<IRightsService> logger)
|
||||
{
|
||||
_unitOfWork = unitOfWork;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
public async Task<Right?> CreateRightAsync(string rightName, string description)
|
||||
{
|
||||
var right = new Right
|
||||
{
|
||||
Name = rightName,
|
||||
Description = description
|
||||
};
|
||||
|
||||
await _unitOfWork.RightRepository.InsertAsync(right);
|
||||
if (await _unitOfWork.SaveAsync())
|
||||
{
|
||||
return right;
|
||||
}
|
||||
|
||||
throw new Exception($"Unable to create right for {rightName}");
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateRightAsync(long rightId, string newRightName, string newDescription)
|
||||
{
|
||||
var right = await _unitOfWork.RightRepository.GetByIDAsync(rightId);
|
||||
|
||||
if (right == null)
|
||||
{
|
||||
throw new KeyNotFoundException($"Right with ID {rightId} not found");
|
||||
}
|
||||
|
||||
right.Name = newRightName;
|
||||
right.Description = newDescription;
|
||||
|
||||
if (!await _unitOfWork.SaveAsync())
|
||||
{
|
||||
throw new Exception($"Unable to create right for {rightId}");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteRightAsync(long rightId)
|
||||
{
|
||||
var right = await _unitOfWork.RightRepository.GetByIDAsync(rightId);
|
||||
|
||||
if (right == null)
|
||||
{
|
||||
throw new KeyNotFoundException($"Right with ID {rightId} not found");
|
||||
}
|
||||
|
||||
_unitOfWork.RightRepository.Delete(right);
|
||||
if (!await _unitOfWork.SaveAsync())
|
||||
{
|
||||
throw new Exception($"Unable to delete right for {rightId}");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<Right?> GetRightByIdAsync(long rightId)
|
||||
{
|
||||
return await _unitOfWork.RightRepository.GetByIDAsync(rightId);
|
||||
}
|
||||
|
||||
public async Task<(List<Right> Rights, int TotalCount)> GetAllRightsAsync(int pageNumber = 1, int pageSize = 10)
|
||||
{
|
||||
var query = _unitOfWork.RightRepository.Get();
|
||||
|
||||
var totalItems = await query.CountAsync();
|
||||
|
||||
var pagedRights = await query
|
||||
.Skip((pageNumber - 1) * pageSize)
|
||||
.Take(pageSize)
|
||||
.ToListAsync();
|
||||
|
||||
return (pagedRights, totalItems);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
using GamificationService.Models.Database;
|
||||
|
||||
namespace GamificationService.Services.Roles;
|
||||
|
||||
public interface IRolesService
|
||||
{
|
||||
Task<ApplicationRole> CreateRoleAsync(string roleName, string description);
|
||||
Task<bool> UpdateRoleAsync(long roleId, string newRoleName, string newDescription);
|
||||
Task<bool> DeleteRoleAsync(long roleId);
|
||||
Task<bool> AddRightToRoleAsync(long roleId, long rightId);
|
||||
Task<bool> RemoveRightFromRoleAsync(long roleId, long rightId);
|
||||
Task<ApplicationRole> GetRoleByIdAsync(long roleId);
|
||||
Task<(List<ApplicationRole> Roles, int TotalCount)> GetAllRolesAsync(int pageNumber = 1, int pageSize = 10);
|
||||
}
|
||||
@@ -1,162 +0,0 @@
|
||||
using GamificationService.Database.Repositories;
|
||||
using GamificationService.Models.Database;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace GamificationService.Services.Roles;
|
||||
|
||||
public class RolesService : IRolesService
|
||||
{
|
||||
#region Services
|
||||
|
||||
private readonly ILogger<IRolesService> _logger;
|
||||
private readonly UnitOfWork _unitOfWork;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
|
||||
public RolesService(ILogger<IRolesService> logger, UnitOfWork unitOfWork)
|
||||
{
|
||||
_logger = logger;
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
//TODO: refactor database work, to be more beautiful
|
||||
//ToDo: make better exception handling
|
||||
public async Task<ApplicationRole> CreateRoleAsync(string roleName, string description)
|
||||
{
|
||||
var role = new ApplicationRole(roleName)
|
||||
{
|
||||
Description = description
|
||||
};
|
||||
|
||||
await _unitOfWork.RoleRepository.InsertAsync(role);
|
||||
if (await _unitOfWork.SaveAsync())
|
||||
{
|
||||
return role;
|
||||
}
|
||||
throw new Exception("Unable to create role");
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateRoleAsync(long roleId, string newRoleName, string newDescription)
|
||||
{
|
||||
var role = await _unitOfWork.RoleRepository.GetByIDAsync(roleId);
|
||||
|
||||
if (role == null)
|
||||
{
|
||||
throw new KeyNotFoundException($"Role with ID {roleId} not found");
|
||||
}
|
||||
|
||||
role.Name = newRoleName;
|
||||
role.Description = newDescription;
|
||||
|
||||
if (!await _unitOfWork.SaveAsync())
|
||||
{
|
||||
throw new Exception("Unable to create role");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteRoleAsync(long roleId)
|
||||
{
|
||||
var role = await _unitOfWork.RoleRepository.GetByIDAsync(roleId);
|
||||
|
||||
if (role == null)
|
||||
{
|
||||
throw new KeyNotFoundException($"Role with ID {roleId} not found");
|
||||
}
|
||||
|
||||
_unitOfWork.RoleRepository.Delete(role);
|
||||
if (!await _unitOfWork.SaveAsync())
|
||||
{
|
||||
throw new Exception("Unable to delete role");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<bool> AddRightToRoleAsync(long roleId, long rightId)
|
||||
{
|
||||
var role = await _unitOfWork.RoleRepository.Get()
|
||||
.Include(r => r.RoleRights)
|
||||
.FirstOrDefaultAsync(r => r.Id == roleId);
|
||||
|
||||
var right = await _unitOfWork.RightRepository.GetByIDAsync(rightId);
|
||||
|
||||
if (role == null || right == null)
|
||||
{
|
||||
throw new KeyNotFoundException($"Role or Right not found");
|
||||
}
|
||||
|
||||
var existingRight = role.RoleRights.FirstOrDefault(rr => rr.RightId == rightId);
|
||||
|
||||
if (existingRight == null)
|
||||
{
|
||||
role.RoleRights.Add(new RoleRight { RoleId = roleId, RightId = rightId });
|
||||
if (!await _unitOfWork.SaveAsync())
|
||||
{
|
||||
throw new Exception("Unable to add role right");
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
public async Task<bool> RemoveRightFromRoleAsync(long roleId, long rightId)
|
||||
{
|
||||
var roleRight = await _unitOfWork.RoleRightRepository.Get()
|
||||
.FirstOrDefaultAsync(rr => rr.RoleId == roleId && rr.RightId == rightId);
|
||||
|
||||
if (roleRight == null)
|
||||
{
|
||||
throw new KeyNotFoundException($"Right not found for role");
|
||||
}
|
||||
|
||||
_unitOfWork.RoleRightRepository.Delete(roleRight);
|
||||
if (!await _unitOfWork.SaveAsync())
|
||||
{
|
||||
throw new Exception("Unable to remove role right");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<ApplicationRole> GetRoleByIdAsync(long roleId)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await _unitOfWork.RoleRepository.Get()
|
||||
.Include(r => r.RoleRights)
|
||||
.ThenInclude(rr => rr.Right)
|
||||
.FirstOrDefaultAsync(r => r.Id == roleId);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, e.Message);
|
||||
throw;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public async Task<(List<ApplicationRole> Roles, int TotalCount)> GetAllRolesAsync(int pageNumber = 1, int pageSize = 10)
|
||||
{
|
||||
var query = _unitOfWork.RoleRepository.Get()
|
||||
.Include(r => r.RoleRights)
|
||||
.ThenInclude(rr => rr.Right);
|
||||
|
||||
var totalItems = await query.CountAsync();
|
||||
var pagedRoles = await query
|
||||
.Skip((pageNumber - 1) * pageSize)
|
||||
.Take(pageSize)
|
||||
.ToListAsync();
|
||||
|
||||
return (pagedRoles, totalItems);
|
||||
}
|
||||
#endregion
|
||||
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
using GamificationService.Models.Database;
|
||||
using GamificationService.Models.DTO;
|
||||
|
||||
namespace GamificationService.Services.UsersProfile;
|
||||
|
||||
public interface IUserProfileService
|
||||
{
|
||||
public Task<UserProfileDTO> AddUserProfile(long userId, UserProfileCreateDTO userProfile);
|
||||
public Task<UserProfile> AddUserProfile(UserProfile userProfile);
|
||||
public UserProfile? GetUserProfileByUserId(long id);
|
||||
public UserProfile? GetUserProfileById(long id);
|
||||
public Task<bool> UpdateUserProfileByUserId(long userId, UserProfileCreateDTO userProfile);
|
||||
public Task<bool> UpdateUserProfile(UserProfile userProfile);
|
||||
public bool DeleteUserProfile(long id);
|
||||
}
|
||||
@@ -1,121 +0,0 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user