From 44881818a247cc8bce4622024c36ee13649e8549 Mon Sep 17 00:00:00 2001 From: Nikolai Papin Date: Sat, 20 Sep 2025 23:08:08 +0300 Subject: [PATCH] chore: deleted all the stuff we don't need here --- Controllers/AuthController.cs | 486 ------------------ Controllers/RightsController.cs | 186 ------- Controllers/RoleController.cs | 283 ---------- Controllers/UserProfileController.cs | 184 ------- Database/ApplicationContext.cs | 18 +- .../Extensions/ChangeTrackerExtensions.cs | 49 -- Database/Repositories/UnitOfWork.cs | 79 --- .../AuthService/AuthServiceException.cs | 26 - .../GenerateRefreshTokenException.cs | 26 - .../JwtService/JwtServiceException.cs | 26 - .../ProfileCreationException.cs | 26 - .../ProfileDeletionException.cs | 26 - .../ProfileExistsException.cs | 26 - .../ProfileNotFoundException.cs | 26 - .../ProfileUpdateException.cs | 26 - Extensions/DependencyInjectionExtensions.cs | 60 --- Mapper/MappingProfile.cs | 41 -- Models/DTO/AuthDTO.cs | 21 - Models/DTO/DisableTwoFactorDTO.cs | 7 - Models/DTO/EnableTwoFactorDTO.cs | 6 - Models/DTO/GetAllRightsResponse.cs | 11 - Models/DTO/GetAllRolesResponse.cs | 11 - Models/DTO/GetTwoFactorDTO.cs | 11 - Models/DTO/LoginResultResponse.cs | 9 - Models/DTO/RefreshTokenDTO.cs | 7 - Models/DTO/RightDTO.cs | 7 - Models/DTO/RoleDTO.cs | 7 - Models/DTO/TwoFactorDTO.cs | 16 - Models/DTO/UserProfileCreateDTO.cs | 33 -- Models/DTO/UserProfileDTO.cs | 37 -- Models/Database/ApplicationRole.cs | 15 - Models/Database/ApplicationUser.cs | 19 - Models/Database/RefreshToken.cs | 25 - Models/Database/Right.cs | 18 - Models/Database/RoleRight.cs | 10 - Models/Database/UserProfile.cs | 40 -- Models/Database/UserRole.cs | 10 - .../UserProfiles/CreateUserProfileRequest.cs | 6 - .../UserProfiles/CreateUserProfileResponse.cs | 7 - .../UserProfiles/UpdateUserProfileRequest.cs | 6 - .../UserProfiles/UpdateUserProfileResponse.cs | 6 - Models/UserSession.cs | 7 - Services/Cookies/CookieService.cs | 47 -- Services/Cookies/ICookieService.cs | 7 - Services/CurrentUsers/CurrentUserService.cs | 26 - Services/CurrentUsers/ICurrentUserService.cs | 8 - Services/JWT/IJWTService.cs | 13 - Services/JWT/JWTService.cs | 144 ------ Services/Notification/INotificationService.cs | 11 - Services/Notification/NotificationService.cs | 56 -- Services/Rights/IRightsService.cs | 12 - Services/Rights/RightsService.cs | 103 ---- Services/Roles/IRolesService.cs | 14 - Services/Roles/RolesService.cs | 162 ------ Services/UsersProfile/IUserProfileService.cs | 15 - Services/UsersProfile/UserProfileService.cs | 121 ----- Utils/Clients/EmailClient.cs | 38 -- Utils/Clients/PushNotificationsClient.cs | 150 ------ Utils/Enums/Gender.cs | 7 - Utils/Enums/InstructionTestScoreCalcMethod.cs | 8 - Utils/Enums/NotificationInformationType.cs | 9 - Utils/Factory/MailNotificationsFactory.cs | 21 - Utils/Factory/PushNotificationsFactory.cs | 28 - Utils/MailNotification.cs | 111 ---- Utils/Notification.cs | 33 -- Utils/PushNotification.cs | 40 -- Utils/TwoFactorProvider.cs | 10 - 67 files changed, 1 insertion(+), 3139 deletions(-) delete mode 100755 Controllers/AuthController.cs delete mode 100644 Controllers/RightsController.cs delete mode 100644 Controllers/RoleController.cs delete mode 100644 Controllers/UserProfileController.cs delete mode 100755 Exceptions/Services/AuthService/AuthServiceException.cs delete mode 100755 Exceptions/Services/JwtService/GenerateRefreshTokenException.cs delete mode 100755 Exceptions/Services/JwtService/JwtServiceException.cs delete mode 100644 Exceptions/UserProfileService/ProfileCreationException.cs delete mode 100644 Exceptions/UserProfileService/ProfileDeletionException.cs delete mode 100644 Exceptions/UserProfileService/ProfileExistsException.cs delete mode 100644 Exceptions/UserProfileService/ProfileNotFoundException.cs delete mode 100644 Exceptions/UserProfileService/ProfileUpdateException.cs delete mode 100755 Models/DTO/AuthDTO.cs delete mode 100644 Models/DTO/DisableTwoFactorDTO.cs delete mode 100644 Models/DTO/EnableTwoFactorDTO.cs delete mode 100644 Models/DTO/GetAllRightsResponse.cs delete mode 100644 Models/DTO/GetAllRolesResponse.cs delete mode 100644 Models/DTO/GetTwoFactorDTO.cs delete mode 100755 Models/DTO/LoginResultResponse.cs delete mode 100755 Models/DTO/RefreshTokenDTO.cs delete mode 100644 Models/DTO/RightDTO.cs delete mode 100644 Models/DTO/RoleDTO.cs delete mode 100755 Models/DTO/TwoFactorDTO.cs delete mode 100644 Models/DTO/UserProfileCreateDTO.cs delete mode 100755 Models/DTO/UserProfileDTO.cs delete mode 100755 Models/Database/ApplicationRole.cs delete mode 100755 Models/Database/ApplicationUser.cs delete mode 100755 Models/Database/RefreshToken.cs delete mode 100755 Models/Database/Right.cs delete mode 100755 Models/Database/RoleRight.cs delete mode 100755 Models/Database/UserProfile.cs delete mode 100755 Models/Database/UserRole.cs delete mode 100644 Models/Messages/UserProfiles/CreateUserProfileRequest.cs delete mode 100644 Models/Messages/UserProfiles/CreateUserProfileResponse.cs delete mode 100644 Models/Messages/UserProfiles/UpdateUserProfileRequest.cs delete mode 100644 Models/Messages/UserProfiles/UpdateUserProfileResponse.cs delete mode 100755 Models/UserSession.cs delete mode 100755 Services/Cookies/CookieService.cs delete mode 100755 Services/Cookies/ICookieService.cs delete mode 100644 Services/CurrentUsers/CurrentUserService.cs delete mode 100644 Services/CurrentUsers/ICurrentUserService.cs delete mode 100755 Services/JWT/IJWTService.cs delete mode 100755 Services/JWT/JWTService.cs delete mode 100755 Services/Notification/INotificationService.cs delete mode 100755 Services/Notification/NotificationService.cs delete mode 100755 Services/Rights/IRightsService.cs delete mode 100755 Services/Rights/RightsService.cs delete mode 100755 Services/Roles/IRolesService.cs delete mode 100755 Services/Roles/RolesService.cs delete mode 100755 Services/UsersProfile/IUserProfileService.cs delete mode 100755 Services/UsersProfile/UserProfileService.cs delete mode 100755 Utils/Clients/EmailClient.cs delete mode 100755 Utils/Clients/PushNotificationsClient.cs delete mode 100644 Utils/Enums/Gender.cs delete mode 100644 Utils/Enums/InstructionTestScoreCalcMethod.cs delete mode 100755 Utils/Enums/NotificationInformationType.cs delete mode 100755 Utils/Factory/MailNotificationsFactory.cs delete mode 100755 Utils/Factory/PushNotificationsFactory.cs delete mode 100755 Utils/MailNotification.cs delete mode 100755 Utils/Notification.cs delete mode 100755 Utils/PushNotification.cs delete mode 100644 Utils/TwoFactorProvider.cs diff --git a/Controllers/AuthController.cs b/Controllers/AuthController.cs deleted file mode 100755 index 504a4fc..0000000 --- a/Controllers/AuthController.cs +++ /dev/null @@ -1,486 +0,0 @@ -using GamificationService.Models.BasicResponses; -using GamificationService.Models.Database; -using GamificationService.Models.DTO; -using GamificationService.Services.JWT; -using GamificationService.Services.NotificationService; -using GamificationService.Utils; -using GamificationService.Utils.Factory; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Identity; -using Microsoft.AspNetCore.Mvc; -using Newtonsoft.Json; - -namespace GamificationService.Controllers; -[ApiController] -[Route("api/[controller]")] -public class AuthController : ControllerBase -{ - #region Services - - private readonly ILogger _logger; - private readonly UserManager _userManager; - private readonly SignInManager _signInManager; - private readonly IJwtService _jwtService; - private readonly INotificationService _notificationService; - private readonly MailNotificationsFactory _mailNotificationsFactory; - private readonly PushNotificationsFactory _pushNotificationsFactory; - - - #endregion - - #region Constructor - - public AuthController(ILogger logger, UserManager userManager, SignInManager signInManager, IJwtService jwtService, INotificationService notificationService, MailNotificationsFactory mailNotificationsFactory, PushNotificationsFactory pushNotificationsFactory) - { - _logger = logger; - _userManager = userManager; - _signInManager = signInManager; - _jwtService = jwtService; - _notificationService = notificationService; - _mailNotificationsFactory = mailNotificationsFactory; - _pushNotificationsFactory = pushNotificationsFactory; - } - - #endregion - - #region Actions - - #region Auth - - /// - /// Handles user registration. - /// - /// The registration model. - /// A response indicating the result of the registration. - [HttpPost("register")] - public async Task Register([FromBody] AuthDTO model) - { - try - { - var user = new ApplicationUser() - { - UserName = model.Username, - Email = model.Email, - TwoFactorEnabled = false, - TwoFactorProviders = new List() { TwoFactorProvider.NONE } - }; - - var result = await _userManager.CreateAsync(user, model.Password); - - if (!result.Succeeded) - { - _logger.LogError("User registration failed: {Errors}", result.Errors); - return BadRequest(new BasicResponse - { - Code = 400, - Message = "User registration failed" - }); - } - - _logger.LogInformation("User registered successfully: {Username}", model.Username); - return Ok(new BasicResponse - { - Code = 200, - Message = "User created successfully" - }); - } - catch (Exception ex) - { - _logger.LogError(ex, "An error occurred during user registration: {Message}", ex.Message); - return StatusCode(500, new BasicResponse - { - Code = 500, - Message = "An error occurred during user registration" - }); - } - } - - - /// - /// Handles user login. - /// - /// The login model. - /// A response indicating the result of the login. - [HttpPost("login")] - public async Task Login([FromBody] AuthDTO model) - { - try - { - var user = await _userManager.FindByNameAsync(model.Username); - - if (user == null) - { - _logger.LogError("Invalid username or password"); - return NotFound(new BasicResponse - { - Code = 404, - Message = "Invalid username or password" - }); - } - var result = await _signInManager.PasswordSignInAsync( user, model.Password, false, model.RememberMe); - - if (result.Succeeded & !result.RequiresTwoFactor) - { - var refreshToken = await _jwtService.GenerateRefreshTokenAsync(user); - var accessToken = _jwtService.GenerateAccessToken(user); - _logger.LogInformation("User logged in successfully: {Username}", model.Username); - - return Ok(new LoginResultResponse() - { - RequiresTwoFactorAuth = false, - Success = true, - Token = new RefreshTokenDTO() - { - AccessToken = accessToken, - RefreshToken = refreshToken.Token - } - }); - } - else if(result.RequiresTwoFactor) - { - var providerWithMaxWeight = user.TwoFactorProviders - .OrderByDescending(p => (int)p) - .FirstOrDefault(); - - if (providerWithMaxWeight == TwoFactorProvider.NONE) - { - _logger.LogInformation("User {Username} does not have any two-factor authentication enabled", model.Username); - return StatusCode(418, new LoginResultResponse() - { - RequiresTwoFactorAuth = false, - Success = true, - TwoFactorProvider = (int)providerWithMaxWeight - }); - } - - var code = await _userManager.GenerateTwoFactorTokenAsync(user, providerWithMaxWeight.ToString()); - await SendNotificationAsync(user, "Two-factor authentication code", code, NotificationInformationType.AUTH,providerWithMaxWeight); - _logger.LogInformation("Two-factor authentication required for user {Username}", model.Username); - return Ok(new LoginResultResponse() - { - RequiresTwoFactorAuth = true, - Success = true - }); - } - - _logger.LogError("Invalid username or password"); - return BadRequest(new BasicResponse - { - Code = 400, - Message = "Invalid username or password" - }); - - } - catch (Exception ex) - { - _logger.LogError(ex, "An error occurred during user login: {Message}", ex.Message); - return StatusCode(500, new BasicResponse - { - Code = 500, - Message = "An error occurred during user login" - }); - } - } - [HttpPost("logout")] - [Authorize] - public async Task Logout() - { - await _signInManager.SignOutAsync(); - return Ok("Logged out successfully"); - } - - [HttpPost("revoke-token")] - [Authorize] - public async Task RevokeToken() - { - var user = await _userManager.GetUserAsync(User); - if (user == null) - { - return NotFound("User not found"); - } - - await _jwtService.RevokeRefreshTokenAsync(user.Id, HttpContext.Request.Cookies["refresh_token"],GetRemoteIpAddress()); - return Ok("Token revoked successfully"); - } - #endregion - - #region Email - - [HttpGet("{username}/init-email-verification")] - public async Task VerifyEmail(string username) - { - try - { - var user = await _userManager.FindByNameAsync(username); - if (user == null) - { - _logger.LogError("Invalid username or password"); - return NotFound(new BasicResponse - { - Code = 404, - Message = "Invalid username or password" - }); - } - var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); - await SendNotificationAsync(user, "Email verification code", code, NotificationInformationType.AUTH, TwoFactorProvider.EMAIL); - _logger.LogInformation("Email verification code sent to user {Username}", username); - return Ok(new BasicResponse() - { - Code = 200, - Message = "Email verification code sent" - }); - } - catch (Exception e) - { - _logger.LogError(e, "An error occurred during email verification: {Message}", e.Message); - return StatusCode(500, new BasicResponse - { - Code = 500, - Message = "An error occurred during email verification" - }); - } - } - - [HttpGet("{username}/verify-email/{code}")] - public async Task VerifyEmail(string username, string code) - { - try - { - var user = await _userManager.FindByNameAsync(username); - if (user == null) - { - _logger.LogError("Invalid username or password"); - return NotFound(new BasicResponse - { - Code = 404, - Message = "Invalid username or password" - }); - } - - var result = await _userManager.ConfirmEmailAsync(user,code); - if (result.Succeeded) - { - _logger.LogInformation("Email verified for user {Username}", username); - user.EmailConfirmed = true; - await _userManager.UpdateAsync(user); - return Ok(new BasicResponse() - { - Code = 200, - Message = "Email verified" - }); - } - return BadRequest(new BasicResponse() - { - Code = 400, - Message = "Email verification failed" - }); - } - catch (Exception e) - { - _logger.LogError(e, "An error occurred during email verification: {Message}", e.Message); - return StatusCode(500, new BasicResponse - { - Code = 500, - Message = "An error occurred during email verification" - }); - } - } - #endregion - - #region 2FA - - [HttpPost("get-2fa-code")] - public async Task GetTwoFactorCode([FromBody] GetTwoFactorDTO model) - { - try - { - var user = await _userManager.FindByNameAsync(model.Username); - - if (user == null) - { - _logger.LogError("Invalid username or password"); - return NotFound(new BasicResponse - { - Code = 404, - Message = "Invalid username or password" - }); - } - - var providerWithRequiredWeight = user.TwoFactorProviders - .FirstOrDefault(p => (int)p == model.TwoFactorProvider); - var code = await _userManager.GenerateTwoFactorTokenAsync(user, providerWithRequiredWeight.ToString()); - await SendNotificationAsync(user, "Two-factor authentication code", code, NotificationInformationType.AUTH,providerWithRequiredWeight); - _logger.LogInformation("Two-factor authentication code sent to user {Username}", model.Username); - - return Ok(new BasicResponse() - { - Code = 200, - Message = "Code sent successfully" - }); - } - catch (Exception e) - { - _logger.LogError(e, e.Message); - return StatusCode(500, new BasicResponse() - { - Code = 500, - Message = "Failed to send code" - }); - } - } - - [HttpPost("verify-2fa")] - public async Task VerifyTwoFactorCode([FromBody] TwoFactorDTO model) - { - try - { - if (model.Username != null) - { - var user = await _userManager.FindByNameAsync(model.Username); - var providerWithRequiredWeight = user.TwoFactorProviders - .FirstOrDefault(p => (int)p == model.TwoFactorProvider); - var signInResult = _signInManager.TwoFactorSignInAsync(providerWithRequiredWeight.ToString(), - model.Code, false, model.RememberMe); - - - if (!signInResult.Result.Succeeded) - { - return BadRequest(new BasicResponse() - { - Code = 400, - Message = "Invalid code" - }); - } - - var token = _jwtService.GenerateAccessToken(user); - var refreshToken = await _jwtService.GenerateRefreshTokenAsync(user); - - _logger.LogInformation("User logged in successfully: {Username}", model.Username); - await SendNotificationAsync(user, "Login successful", "You have successfully logged in", NotificationInformationType.WARNING,TwoFactorProvider.EMAIL); - - return Ok( new LoginResultResponse() - { - RequiresTwoFactorAuth = false, - Success = true, - Token = new RefreshTokenDTO() - { - AccessToken = token, - RefreshToken = refreshToken.Token - } - }); - } - _logger.LogError("Username can't be empty"); - return NotFound(new BasicResponse() - { - Code = 404, - Message = "Username can't be empty" - }); - } - catch (Exception ex) - { - _logger.LogError(ex, "An error occurred during user verification: {Message}", ex.Message); - return StatusCode(500, new BasicResponse() - { - Code = 500, - Message = "An error occurred during user verification" - }); - } - } - - [HttpPost("enable-2fa")] - [Authorize] - public async Task EnableTwoFactor([FromBody]EnableTwoFactorDTO model) - { - var user = await _userManager.GetUserAsync(User); - if (user == null) - { - return NotFound(new BasicResponse() - { - Code = 404, - Message = "User not found" - }); - } - - user.TwoFactorProviders.Add((TwoFactorProvider)model.TwoFactorProvider); - user.TwoFactorEnabled = true; - await _userManager.UpdateAsync(user); - var secretKey = await _userManager.GenerateTwoFactorTokenAsync(user, TwoFactorProvider.AUTHENTICATOR.ToString()); - _logger.LogInformation("User logged in successfully: {Username}", User); - await SendNotificationAsync(user, "Login successful", "You have successfully logged in", NotificationInformationType.WARNING,(TwoFactorProvider)model.TwoFactorProvider); - - return Ok(new BasicResponse() - { - Code = 200, - Message = "Two-factor authentication enabled successfully" - }); - } - - [HttpPost("disable-2fa")] - [Authorize] - public async Task DisableTwoFactor([FromBody] DisableTwoFactorDTO model) - { - var user = await _userManager.GetUserAsync(User); - if (user == null) - { - return NotFound("User not found"); - } - - if (!await _userManager.VerifyTwoFactorTokenAsync(user,model.TwoFactorProvider.ToString(),model.Code)) - { - return BadRequest("Invalid verification code"); - } - - user.TwoFactorEnabled = false; - await _userManager.UpdateAsync(user); - - return Ok("Two-factor authentication disabled"); - } - - #endregion - - #region Helpers - - private async Task SendNotificationAsync(ApplicationUser user, - string title, - string message, - NotificationInformationType notificationInformationType, - TwoFactorProvider provider) - { - try - { - switch (provider) - { - case TwoFactorProvider.EMAIL: - await _notificationService.SendMailNotificationAsync(user, _mailNotificationsFactory.CreateNotification(notificationInformationType, title, message)); - break; - case TwoFactorProvider.PHONE: - throw new NotImplementedException(); - break; - case TwoFactorProvider.PUSH: - await _notificationService.SendPushNotificationAsync(user, _pushNotificationsFactory.CreateNotification(notificationInformationType, title, message)); - break; - case TwoFactorProvider.AUTHENTICATOR: - throw new NotImplementedException(); - break; - default: - break; - } - } - catch (Exception ex) - { - _logger.LogError(ex, "An error occurred during notification: {Message}", ex.Message); - } - } - - private string GetRemoteIpAddress() - { - if (HttpContext.Connection.RemoteIpAddress != null) - { - return HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString(); - } - return string.Empty; - } - #endregion - - #endregion -} diff --git a/Controllers/RightsController.cs b/Controllers/RightsController.cs deleted file mode 100644 index bbe3f82..0000000 --- a/Controllers/RightsController.cs +++ /dev/null @@ -1,186 +0,0 @@ -using GamificationService.Models.BasicResponses; -using GamificationService.Models.DTO; -using GamificationService.Services.Rights; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Mvc; - -namespace GamificationService.Controllers; - -[ApiController] -[Route("api/[controller]")] -[Authorize(Policy = "Admin")] -public class RightsController : ControllerBase -{ - #region Services - - private readonly IRightsService _rightsService; - private readonly ILogger _logger; - - #endregion - - #region Constructor - - public RightsController(IRightsService rightsService, ILogger logger) - { - _rightsService = rightsService; - _logger = logger; - } - - #endregion - - #region Methods - - [HttpGet] - public async Task GetAllRightsAsync([FromQuery] int pageNumber = 1, [FromQuery] int pageSize = 10) - { - try - { - var (rights, totalCount) = await _rightsService.GetAllRightsAsync(pageNumber, pageSize); - - _logger.LogInformation($"Retrieved {rights.Count} rights"); - - var response = new GetAllRightsResponse() - { - Rights = rights, - TotalCount = totalCount, - PageNumber = pageNumber, - PageSize = pageSize - }; - return Ok(response); - } - catch (Exception e) - { - _logger.LogError(e, e.Message); - return StatusCode(500, new BasicResponse() - { - Code = 500, - Message = "Failed to get rights.", - }); - } - } - [HttpGet("{id}")] - public async Task GetRightByIdAsync(long id) - { - var right = await _rightsService.GetRightByIdAsync(id); - _logger.LogInformation($"Retrieved right with id: {id}"); - if (right == null) - { - return NotFound(new BasicResponse() - { - - Code = 404, - Message = "Right not found" - - }); - } - return Ok(right); - } - [HttpPost] - public async Task CreateRightAsync([FromBody] RightDTO model) - { - try - { - var right = await _rightsService.CreateRightAsync(model.Name, model.Description); - - _logger.LogInformation($"Created right: {right}"); - - return CreatedAtAction(nameof(CreateRightAsync), new { id = right.Id }, right); - - } - catch (Exception e) - { - _logger.LogError(e, e.Message); - return StatusCode(500, new BasicResponse() - { - Code = 500, - Message = "Failed to create right.", - }); - } - } - [HttpPut("{id}")] - public async Task UpdateRightAsync(long id, [FromBody] RightDTO model) - { - try - { - if (await _rightsService.UpdateRightAsync(id, model.Name, model.Description)) - { - _logger.LogInformation($"Updated right: {id}"); - return Ok(new BasicResponse() - { - Code = 200, - Message = "Rights updated", - }); - } - _logger.LogError($"Unknown with right updating, {id}"); - return StatusCode(418, - new BasicResponse() - { - Code = 418, - Message = "Failed to update right." - }); - } - catch (KeyNotFoundException) - { - _logger.LogError($"Right not found, {id} "); - return NotFound(new BasicResponse() - { - Code = 404, - Message = "Right not found" - }); - } - catch (Exception e) - { - _logger.LogError(e, e.Message); - return StatusCode(500, new BasicResponse() - { - Code = 500, - Message = "Failed to update right" - }); - } - } - - [HttpDelete("{id}")] - public async Task DeleteRightAsync(long id) - { - try - { - if( await _rightsService.DeleteRightAsync(id)) - { - _logger.LogInformation($"Deleted right: {id}"); - return Ok(new BasicResponse() - { - Code = 200, - Message = "Rights deleted", - }); - } - _logger.LogError($"Unknown error with right deleting, {id} "); - return StatusCode(418, new BasicResponse() - { - Code = 418, - Message = "Failed to delete right" - }); - - } - catch (KeyNotFoundException) - { - _logger.LogError($"Role not found, {id} "); - return NotFound(new BasicResponse() - { - Code = 404, - Message = "Right not found" - }); - } - catch (Exception e) - { - _logger.LogError(e, e.Message); - return StatusCode(500, new BasicResponse() - { - Code = 500, - Message = "Failed to delete right" - }); - } - } - - - #endregion -} diff --git a/Controllers/RoleController.cs b/Controllers/RoleController.cs deleted file mode 100644 index f7fe6f9..0000000 --- a/Controllers/RoleController.cs +++ /dev/null @@ -1,283 +0,0 @@ -using GamificationService.Models.BasicResponses; -using GamificationService.Models.DTO; -using GamificationService.Services.Roles; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Http.HttpResults; -using Microsoft.AspNetCore.Mvc; - -namespace GamificationService.Controllers; -[ApiController] -[Route("api/[controller]")] -[Authorize(Policy = "Admin")] -public class RoleController : ControllerBase -{ - #region Services - - private readonly IRolesService _rolesService; - private readonly ILogger _logger; - - #endregion - - #region Constructor - - public RoleController(ILogger logger, IRolesService rolesService) - { - _logger = logger; - _rolesService = rolesService; - } - - #endregion - - #region ControllerMethods - - [HttpGet] - public async Task GetAllRolesAsync([FromQuery] int pageNumber = 1, [FromQuery] int pageSize = 10) - { - try - { - var (roles, totalCount) = await _rolesService.GetAllRolesAsync(pageNumber, pageSize); - _logger.LogInformation($"Roles found successfully, {roles.Count}"); - var response = new GetAllRolesResponse() - { - Roles = roles, - TotalCount = totalCount, - PageNumber = pageNumber, - PageSize = pageSize - }; - return Ok(response); - } - catch (Exception ex) - { - _logger.LogError(ex,ex.Message); - return StatusCode(500, new BasicResponse() - { - Code = 500, - Message = "Failed to get roles" - }); - } - - } - - - [HttpGet("{id}")] - public async Task GetRoleByIdAsync(long id) - { - var role = await _rolesService.GetRoleByIdAsync(id); - _logger.LogInformation($"Role found successfully, {role.Id}"); - if (role == null) - { - return NotFound(new BasicResponse() - { - - Code = 404, - Message = "Role not found" - - }); - } - return Ok(role); - } - - [HttpPost] - public async Task CreateRoleAsync([FromBody] RoleDTO model) - { - try - { - - var role = await _rolesService.CreateRoleAsync(model.Name, model.Description); - _logger.LogInformation($"Role created successfully, {role.Id}"); - return CreatedAtAction(nameof(CreateRoleAsync), new { id = role.Id }, role); - } - catch (Exception e) - { - _logger.LogError(e, e.Message); - return StatusCode(500, new BasicResponse() - { - Code = 500, - Message = "Failed to create role" - }); - } - } - - - [HttpPut("{id}")] - public async Task UpdateRoleAsync(long id, [FromBody] RoleDTO model) - { - try - { - if (await _rolesService.UpdateRoleAsync(id, model.Name, model.Description)) - { - _logger.LogInformation($"Role updated successfully, {id}"); - - return Ok(new BasicResponse() - { - Code = 200, - Message = "Role updated successfully" - }); - } - - _logger.LogCritical($"Unknown error with role updating, {id}"); - return StatusCode(418,new BasicResponse() - { - Code = 418, - Message = "Role not found" - }); - - } - catch (KeyNotFoundException) - { - _logger.LogError($"Role not found, {id} "); - return NotFound(new BasicResponse() - { - Code = 404, - Message = "Role not found" - }); - } - catch (Exception e) - { - _logger.LogError(e, e.Message); - return StatusCode(500, new BasicResponse() - { - Code = 500, - Message = "Failed to update role" - }); - } - - } - - [HttpDelete("{id}")] - public async Task DeleteRoleAsync(long id) - { - try - { - if (await _rolesService.DeleteRoleAsync(id)) - { - - _logger.LogInformation($"Role updated successfully, {id}"); - - return Ok(new BasicResponse() - { - Code = 200, - Message = "Role updated successfully" - }); - } - - _logger.LogCritical($"Unknown error with role deleting, RoleId {id}"); - return StatusCode(418,new BasicResponse() - { - Code = 418, - Message = "Role not found" - }); - } - catch (KeyNotFoundException) - { - _logger.LogError($"Role not found, {id} "); - return NotFound(new BasicResponse() - { - Code = 404, - Message = "Role not found" - }); - } - catch (Exception e) - { - _logger.LogError(e, e.Message); - return StatusCode(500, new BasicResponse() - { - Code = 500, - Message = "Failed to delete role" - }); - } - - } - - - [HttpPost("{roleId}/rights/{rightId}")] - public async Task AddRightToRoleAsync(long roleId, long rightId) - { - try - { - if (await _rolesService.AddRightToRoleAsync(roleId, rightId)) - { - _logger.LogInformation($"Right added to role successfully, RoleId: {roleId}, RightId: {rightId}"); - return Ok(new BasicResponse() - { - Code = 200, - Message = "Right added to role successfully" - }); - } - - _logger.LogCritical($"Unknown error with adding right to role, RoleId: {roleId}, RightId: {rightId}"); - return StatusCode(418,new BasicResponse() - { - Code = 418, - Message = "Right not found for role" - }); - } - catch(KeyNotFoundException e) - { - _logger.LogError(e, e.Message); - return NotFound(new BasicResponse() - { - Code = 404, - Message = "Right not found for role" - }); - } - catch (Exception e) - { - _logger.LogError(e, e.Message); - return StatusCode(500, new BasicResponse() - { - Code = 500, - Message = "Failed to add right to role" - }); - } - } - - [HttpDelete("{roleId}/rights/{rightId}")] - public async Task RemoveRightFromRoleAsync(long roleId, long rightId) - { - try - { - - if (await _rolesService.RemoveRightFromRoleAsync(roleId, rightId)) - { - _logger.LogInformation($"Right removed from role successfully, RoleId: {roleId}, RightId: {rightId}"); - - return Ok(new BasicResponse() - { - Code = 200, - Message = "Right removed from role successfully" - }); - } - - _logger.LogCritical($"Unknown error with removing right from role, RoleId: {roleId}, RightId: {rightId}"); - return StatusCode(418, new BasicResponse() - { - Code = 418, - Message = "Right not found right for role" - }); - - } - catch (KeyNotFoundException e) - { - _logger.LogError(e, e.Message); - return NotFound(new BasicResponse() - { - Code = 404, - Message = "Right not found for role" - }); - } - catch (Exception e) - { - _logger.LogError(e, e.Message); - return StatusCode(500, new BasicResponse() - { - Code = 500, - Message = "Failed to remove right from role" - }); - } - } - - - - #endregion -} diff --git a/Controllers/UserProfileController.cs b/Controllers/UserProfileController.cs deleted file mode 100644 index 030b08e..0000000 --- a/Controllers/UserProfileController.cs +++ /dev/null @@ -1,184 +0,0 @@ -using AutoMapper; -using GamificationService.Exceptions.Services.ProfileService; -using GamificationService.Models.Database; -using GamificationService.Models.DTO; -using GamificationService.Services.UsersProfile; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Identity; -using Microsoft.AspNetCore.Mvc; - -namespace GamificationService.Controllers; - -[ApiController] -[Authorize(Policy = "User")] -[Route("api/[controller]")] -public class UserProfileController : ControllerBase -{ - private readonly IUserProfileService _userProfilesService; - private readonly UserManager _userManager; - private readonly ILogger _logger; - private readonly IMapper _mapper; - - public UserProfileController(IUserProfileService userProfilesService, UserManager userManager, ILogger logger, IMapper mapper) - { - _userProfilesService = userProfilesService; - _userManager = userManager; - _logger = logger; - _mapper = mapper; - } - - /// - /// Gets a user profile by its ID. - /// - /// The username of the user profile's owner. - /// An containing the user profile DTO if found, or a 404 Not Found if not found. - /// Returns the user profile DTO - /// If the user profile is not found - [HttpGet("user/{username}")] - public async Task GetUserProfileByUsername(string username) - { - try - { - var user = (await _userManager.FindByNameAsync(username)); - if (user == null) - { - return NotFound(); - } - - var userProfile = _userProfilesService.GetUserProfileByUserId(user.Id); - return Ok(_mapper.Map(userProfile)); - } - catch (ProfileNotFoundException) - { - return NotFound(); - } - } - - /// - /// Gets a user profile by its ID. - /// - /// The ID of the user profile. - /// An containing the user profile DTO if found, or a 404 Not Found if not found. - /// Returns the user profile DTO - /// If the user profile is not found - [HttpGet("{id}")] - public IActionResult GetUserProfileById(long id) - { - try - { - var userProfile = _userProfilesService.GetUserProfileById(id); - return Ok(_mapper.Map(userProfile)); - } - catch (ProfileNotFoundException) - { - return NotFound(); - } - } - - /// - /// Adds a new user profile. - /// - /// The username of the user. - /// The user profile model. - /// A containing the created user profile if successful, or a 500 Internal Server Error if not successful. - /// Returns the created user profile - /// If the user is not found - [HttpPost("user/{username}")] - [Authorize(Policy = "Admin")] - public async Task AddUserProfile(string username, [FromBody] UserProfileCreateDTO model) - { - var user = (await _userManager.FindByNameAsync(username)); - if (user == null) - { - return NotFound(); - } - - try - { - var userProfile = await _userProfilesService.AddUserProfile(user.Id, model); - return Ok(_mapper.Map(userProfile)); - } - catch (ProfileNotFoundException) - { - return NotFound(); - } - } - - /// - /// Update user profile for the logged in user. - /// - /// The user profile model. - /// A containing the updated user profile if successful, or a 500 Internal Server Error if not successful. - /// Returns the updated user profile - /// If the user profile is not found - [HttpPut] - public async Task UpdateUserProfile([FromBody] UserProfileCreateDTO model) - { - string username = User.Claims.First(c => c.Type == "username").Value; - long userId = (await _userManager.FindByNameAsync(username))!.Id; - - try - { - bool result = await _userProfilesService.UpdateUserProfileByUserId(userId, model); - return Ok(result); - } - catch (ProfileNotFoundException) - { - return NotFound(); - } - } - - /// - /// Updates an existing user profile. - /// - /// The username of the user. - /// The user profile model. - /// A containing the updated user profile if successful, or a 500 Internal Server Error if not successful. - /// Returns the updated user profile - /// If the user profile is not found - [HttpPut] - [Authorize(Policy = "Admin")] - [Route("user/{userId}")] - public async Task UpdateUserProfileByUsername(string username, [FromBody] UserProfileCreateDTO model) - { - var user = (await _userManager.FindByNameAsync(username)); - if (user == null) - { - return NotFound(); - } - - try - { - bool result = await _userProfilesService.UpdateUserProfileByUserId(user.Id, model); - return Ok(result); - } - catch (ProfileNotFoundException) - { - return NotFound(); - } - } - - /// - /// Deletes an existing user profile. - /// - /// The ID of the user profile to delete. - /// A - /// Returns true. - /// If the user profile is not found - [HttpDelete("{id}")] - [Authorize(Policy = "Admin")] - public IActionResult DeleteUserProfile(long id) - { - try - { - _userProfilesService.DeleteUserProfile(id); - return Ok(); - } - catch (ProfileNotFoundException) - { - return NotFound(); - } - } - -} - diff --git a/Database/ApplicationContext.cs b/Database/ApplicationContext.cs index c49d4d7..9573b05 100755 --- a/Database/ApplicationContext.cs +++ b/Database/ApplicationContext.cs @@ -1,32 +1,16 @@ -using GamificationService.Models.Database; -using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; namespace GamificationService.Database; -public class ApplicationContext : IdentityDbContext +public class ApplicationContext : DbContext { public ApplicationContext(DbContextOptions options) : base(options) { } - public DbSet Rights { get; set; } - public DbSet RefreshTokens { get; set; } - public DbSet Users { get; set; } - public DbSet Roles { get; set; } - public DbSet UserRoles { get; set; } - public DbSet RoleRights { get; set; } - public DbSet UserProfiles { get; set; } - protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); - - modelBuilder.Entity() - .HasKey(ur => new { ur.UserId, ur.RoleId }); - - modelBuilder.Entity() - .HasKey(rr => new { rr.RoleId, rr.RightId }); } } diff --git a/Database/Extensions/ChangeTrackerExtensions.cs b/Database/Extensions/ChangeTrackerExtensions.cs index 7927f16..7d23cbd 100755 --- a/Database/Extensions/ChangeTrackerExtensions.cs +++ b/Database/Extensions/ChangeTrackerExtensions.cs @@ -1,54 +1,5 @@ -using GamificationService.Models.Database; -using GamificationService.Services.CurrentUsers; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.ChangeTracking; - namespace GamificationService.Database.Extensions; public static class ChangeTrackerExtensions { - public static void SetAuditProperties(this ChangeTracker changeTracker, ICurrentUserService currentUserService) - { - changeTracker.DetectChanges(); - IEnumerable entities = - changeTracker - .Entries() - .Where(t => t.Entity is AuditableEntity && - ( - t.State == EntityState.Deleted - || t.State == EntityState.Added - || t.State == EntityState.Modified - )); - - if (entities.Any()) - { - DateTimeOffset timestamp = DateTimeOffset.UtcNow; - - string user = currentUserService.GetCurrentUser().Login ?? "Unknown"; - - foreach (EntityEntry entry in entities) - { - AuditableEntity entity = (AuditableEntity)entry.Entity; - - switch (entry.State) - { - case EntityState.Added: - entity.CreatedOn = timestamp; - entity.CreatedBy = user; - entity.UpdatedOn = timestamp; - entity.UpdatedBy = user; - break; - case EntityState.Modified: - entity.UpdatedOn = timestamp; - entity.UpdatedBy = user; - break; - case EntityState.Deleted: - entity.UpdatedOn = timestamp; - entity.UpdatedBy = user; - entry.State = EntityState.Deleted; - break; - } - } - } - } } diff --git a/Database/Repositories/UnitOfWork.cs b/Database/Repositories/UnitOfWork.cs index be6e308..6232423 100755 --- a/Database/Repositories/UnitOfWork.cs +++ b/Database/Repositories/UnitOfWork.cs @@ -1,4 +1,3 @@ -using GamificationService.Models.Database; using Microsoft.EntityFrameworkCore.Storage; namespace GamificationService.Database.Repositories; @@ -8,12 +7,6 @@ public class UnitOfWork : IDisposable #region fields private ApplicationContext _context; - private GenericRepository _userProfileRepository; - private GenericRepository _roleRepository; - private GenericRepository _rightRepository; - private GenericRepository _refreshTokenRepository; - private GenericRepository _roleRightRepository; - private GenericRepository _userRoleRepository; #endregion @@ -28,78 +21,6 @@ public class UnitOfWork : IDisposable #region Properties - public GenericRepository UserProfileRepository - { - get - { - if (this._userProfileRepository == null) - { - this._userProfileRepository = new GenericRepository(_context); - } - return _userProfileRepository; - } - } - - public GenericRepository RoleRepository - { - get - { - if (this._roleRepository == null) - { - this._roleRepository = new GenericRepository(_context); - } - return _roleRepository; - } - } - - public GenericRepository RightRepository - { - get - { - if (this._rightRepository == null) - { - this._rightRepository = new GenericRepository(_context); - } - return _rightRepository; - } - } - - public GenericRepository RefreshTokenRepository - { - get - { - if (this._refreshTokenRepository == null) - { - this._refreshTokenRepository = new GenericRepository(_context); - } - return _refreshTokenRepository; - } - } - - public GenericRepository RoleRightRepository - { - get - { - if (this._roleRightRepository == null) - { - this._roleRightRepository = new GenericRepository(_context); - } - return _roleRightRepository; - } - } - - public GenericRepository UserRoleRepository - { - get - { - if (this._userRoleRepository == null) - { - this._userRoleRepository = new GenericRepository(_context); - } - return _userRoleRepository; - } - } - #endregion public bool Save() diff --git a/Exceptions/Services/AuthService/AuthServiceException.cs b/Exceptions/Services/AuthService/AuthServiceException.cs deleted file mode 100755 index 6f27e4f..0000000 --- a/Exceptions/Services/AuthService/AuthServiceException.cs +++ /dev/null @@ -1,26 +0,0 @@ -namespace GamificationService.Exceptions.Services.AuthService; - -/// -/// Represents an exception related to authentication service operations. -/// -public class AuthServiceException : Exception -{ - /// - /// Initializes a new instance of the class. - /// - public AuthServiceException() : base() { } - - /// - /// Initializes a new instance of the class with a specified error message. - /// - /// The message that describes the error. - public AuthServiceException(string message) : base(message) { } - - /// - /// Initializes a new instance of the class with a specified error message and a reference to the inner exception that is the cause of this exception. - /// - /// The error message that explains the reason for the exception. - /// The exception that is the cause of the current exception, or a null reference if no inner exception is specified. - public AuthServiceException(string message, Exception innerException) : base(message, innerException) { } - -} diff --git a/Exceptions/Services/JwtService/GenerateRefreshTokenException.cs b/Exceptions/Services/JwtService/GenerateRefreshTokenException.cs deleted file mode 100755 index 9b982e2..0000000 --- a/Exceptions/Services/JwtService/GenerateRefreshTokenException.cs +++ /dev/null @@ -1,26 +0,0 @@ -namespace GamificationService.Exceptions.Services.JwtService; - -/// -/// Represents an exception related to jwt token service operations. -/// -public class GenerateRefreshTokenException : Exception -{ - /// - /// Initializes a new instance of the class. - /// - public GenerateRefreshTokenException() : base() { } - - /// - /// Initializes a new instance of the class with a specified error message. - /// - /// The message that describes the error. - public GenerateRefreshTokenException(string message) : base(message) { } - - /// - /// Initializes a new instance of the class with a specified error message and a reference to the inner exception that is the cause of this exception. - /// - /// The error message that explains the reason for the exception. - /// The exception that is the cause of the current exception, or a null reference if no inner exception is specified. - public GenerateRefreshTokenException(string message, Exception innerException) : base(message, innerException) { } - -} diff --git a/Exceptions/Services/JwtService/JwtServiceException.cs b/Exceptions/Services/JwtService/JwtServiceException.cs deleted file mode 100755 index 3d5d896..0000000 --- a/Exceptions/Services/JwtService/JwtServiceException.cs +++ /dev/null @@ -1,26 +0,0 @@ -namespace GamificationService.Exceptions.Services.JwtService; - -/// -/// Represents an exception related to jwt token service operations. -/// -public class JwtServiceException : Exception -{ - /// - /// Initializes a new instance of the class. - /// - public JwtServiceException() : base() { } - - /// - /// Initializes a new instance of the class with a specified error message. - /// - /// The message that describes the error. - public JwtServiceException(string message) : base(message) { } - - /// - /// Initializes a new instance of the class with a specified error message and a reference to the inner exception that is the cause of this exception. - /// - /// The error message that explains the reason for the exception. - /// The exception that is the cause of the current exception, or a null reference if no inner exception is specified. - public JwtServiceException(string message, Exception innerException) : base(message, innerException) { } - -} diff --git a/Exceptions/UserProfileService/ProfileCreationException.cs b/Exceptions/UserProfileService/ProfileCreationException.cs deleted file mode 100644 index 2bf5a45..0000000 --- a/Exceptions/UserProfileService/ProfileCreationException.cs +++ /dev/null @@ -1,26 +0,0 @@ -namespace GamificationService.Exceptions.Services.ProfileService; - -/// -/// Represents an exception that occurs during profile creation operations. -/// -public class ProfileCreationException : Exception -{ - /// - /// Initializes a new instance of the class. - /// - public ProfileCreationException() : base() { } - - /// - /// Initializes a new instance of the class with a specified error message. - /// - /// The message that describes the error. - public ProfileCreationException(string message) : base(message) { } - - /// - /// Initializes a new instance of the class with a specified error message and a reference to the inner exception that is the cause of this exception. - /// - /// The error message that explains the reason for the exception. - /// The exception that is the cause of the current exception, or a null reference if no inner exception is specified. - public ProfileCreationException(string message, Exception innerException) : base(message, innerException) { } -} - diff --git a/Exceptions/UserProfileService/ProfileDeletionException.cs b/Exceptions/UserProfileService/ProfileDeletionException.cs deleted file mode 100644 index 73f4713..0000000 --- a/Exceptions/UserProfileService/ProfileDeletionException.cs +++ /dev/null @@ -1,26 +0,0 @@ -namespace GamificationService.Exceptions.Services.ProfileService; - -/// -/// Represents an exception that occurs during profile deletion operations. -/// -public class ProfileDeletionException : Exception -{ - /// - /// Initializes a new instance of the class. - /// - public ProfileDeletionException() : base() { } - - /// - /// Initializes a new instance of the class with a specified error message. - /// - /// The message that describes the error. - public ProfileDeletionException(string message) : base(message) { } - - /// - /// Initializes a new instance of the class with a specified error message and a reference to the inner exception that is the cause of this exception. - /// - /// The error message that explains the reason for the exception. - /// The exception that is the cause of the current exception, or a null reference if no inner exception is specified. - public ProfileDeletionException(string message, Exception innerException) : base(message, innerException) { } -} - diff --git a/Exceptions/UserProfileService/ProfileExistsException.cs b/Exceptions/UserProfileService/ProfileExistsException.cs deleted file mode 100644 index a16b7d6..0000000 --- a/Exceptions/UserProfileService/ProfileExistsException.cs +++ /dev/null @@ -1,26 +0,0 @@ -namespace GamificationService.Exceptions.Services.ProfileService; - -/// -/// Represents an exception that occurs when a profile already exists. -/// -public class ProfileExistsException : Exception -{ - /// - /// Initializes a new instance of the class. - /// - public ProfileExistsException() : base() { } - - /// - /// Initializes a new instance of the class with a specified error message. - /// - /// The message that describes the error. - public ProfileExistsException(string message) : base(message) { } - - /// - /// Initializes a new instance of the class with a specified error message and a reference to the inner exception that is the cause of this exception. - /// - /// The error message that explains the reason for the exception. - /// The exception that is the cause of the current exception, or a null reference if no inner exception is specified. - public ProfileExistsException(string message, Exception innerException) : base(message, innerException) { } -} - diff --git a/Exceptions/UserProfileService/ProfileNotFoundException.cs b/Exceptions/UserProfileService/ProfileNotFoundException.cs deleted file mode 100644 index eab50d9..0000000 --- a/Exceptions/UserProfileService/ProfileNotFoundException.cs +++ /dev/null @@ -1,26 +0,0 @@ -namespace GamificationService.Exceptions.Services.ProfileService; - -/// -/// Represents an exception that occurs when a profile is not found. -/// -public class ProfileNotFoundException : Exception -{ - /// - /// Initializes a new instance of the class. - /// - public ProfileNotFoundException() : base() { } - - /// - /// Initializes a new instance of the class with a specified error message. - /// - /// The message that describes the error. - public ProfileNotFoundException(string message) : base(message) { } - - /// - /// Initializes a new instance of the class with a specified error message and a reference to the inner exception that is the cause of this exception. - /// - /// The error message that explains the reason for the exception. - /// The exception that is the cause of the current exception, or a null reference if no inner exception is specified. - public ProfileNotFoundException(string message, Exception innerException) : base(message, innerException) { } -} - diff --git a/Exceptions/UserProfileService/ProfileUpdateException.cs b/Exceptions/UserProfileService/ProfileUpdateException.cs deleted file mode 100644 index 6ae27a3..0000000 --- a/Exceptions/UserProfileService/ProfileUpdateException.cs +++ /dev/null @@ -1,26 +0,0 @@ -namespace GamificationService.Exceptions.Services.ProfileService; - -/// -/// Represents an exception that occurs during profile update operations. -/// -public class ProfileUpdateException : Exception -{ - /// - /// Initializes a new instance of the class. - /// - public ProfileUpdateException() : base() { } - - /// - /// Initializes a new instance of the class with a specified error message. - /// - /// The message that describes the error. - public ProfileUpdateException(string message) : base(message) { } - - /// - /// Initializes a new instance of the class with a specified error message and a reference to the inner exception that is the cause of this exception. - /// - /// The error message that explains the reason for the exception. - /// The exception that is the cause of the current exception, or a null reference if no inner exception is specified. - public ProfileUpdateException(string message, Exception innerException) : base(message, innerException) { } -} - diff --git a/Extensions/DependencyInjectionExtensions.cs b/Extensions/DependencyInjectionExtensions.cs index f9bdac6..343aa64 100755 --- a/Extensions/DependencyInjectionExtensions.cs +++ b/Extensions/DependencyInjectionExtensions.cs @@ -6,14 +6,6 @@ using GamificationService.Database; using GamificationService.Database.Repositories; using GamificationService.Logs; using GamificationService.Mapper; -using GamificationService.Services.Cookies; -using GamificationService.Services.CurrentUsers; -using GamificationService.Services.JWT; -using GamificationService.Services.NotificationService; -using GamificationService.Services.Rights; -using GamificationService.Services.Roles; -using GamificationService.Utils; -using GamificationService.Utils.Factory; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.EntityFrameworkCore; using Microsoft.IdentityModel.Tokens; @@ -135,56 +127,6 @@ public static class UtilServicesExtensions { public static IServiceCollection AddUtilServices(this IServiceCollection services) { - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - return services; - } -} - -public static class NotificationSettings -{ - public static IServiceCollection AddPushNotifications(this IServiceCollection services, IConfiguration configuration) - { - var notificationSettings = configuration.GetSection("NotificationSettings"); - var apiKey = notificationSettings["ApiKey"]; - var token = notificationSettings["Token"]; - var baseUrl = notificationSettings["Url"]; - var projectId = notificationSettings["ProjectId"]; - - HttpClient client = new HttpClient(); - client.BaseAddress = new Uri(baseUrl); - client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}"); - - services.AddSingleton(provider => - { - var logger = provider.GetRequiredService>(); - return new PushNotificationsClient(client, logger, token, projectId); - }); - return services; - } -} -public static class EmailExtensions -{ - public static IServiceCollection AddEmail(this IServiceCollection services, IConfiguration configuration) - { - var smtpSettings = configuration.GetSection("EmailSettings"); - var host = smtpSettings["Host"] ?? "localhost"; - var port = Convert.ToInt32(smtpSettings["Port"] ?? "25"); - var username = smtpSettings["Username"] ?? "username"; - var password = smtpSettings["Password"] ?? "password"; - var email = smtpSettings["EmailFrom"] ?? "email"; - services.AddScoped(sp => new SmtpClient(host) - { - Port = port, - Credentials = new NetworkCredential(username, password), - EnableSsl = true, - }); - - services.AddSingleton(); return services; } } @@ -193,8 +135,6 @@ public static class FactoryExtensions { public static IServiceCollection AddFactories(this IServiceCollection services) { - services.AddSingleton(); - services.AddSingleton(); return services; } } diff --git a/Mapper/MappingProfile.cs b/Mapper/MappingProfile.cs index f69ec5d..b10876a 100755 --- a/Mapper/MappingProfile.cs +++ b/Mapper/MappingProfile.cs @@ -1,7 +1,4 @@ using AutoMapper; -using GamificationService.Models.Database; -using GamificationService.Models.DTO; - namespace GamificationService.Mapper; @@ -9,43 +6,5 @@ public class MappingProfile : Profile { public MappingProfile() { - #region UserProfileMapping - - CreateMap() - .ForMember(x => x.Id, opt => opt.MapFrom(src => src.Id)) - .ForMember(x => x.UserId, opt => opt.MapFrom(src => src.UserId)) - .ForMember(x => x.Name, opt => opt.MapFrom(src => src.Name)) - .ForMember(x => x.Surname, opt => opt.MapFrom(src => src.Surname)) - .ForMember(x => x.Patronymic, opt => opt.MapFrom(src => src.Patronymic)) - .ForMember(x => x.Birthdate, opt => opt.MapFrom(src => src.Birthdate)) - .ForMember(x => x.Gender, opt => opt.MapFrom(src => src.Gender)) - .ForMember(x => x.ContactEmail, opt => opt.MapFrom(src => src.ContactEmail)) - .ForMember(x => x.ContactPhone, opt => opt.MapFrom(src => src.ContactPhone)) - .ForMember(x => x.ProfilePicture, opt => opt.MapFrom(src => src.ProfilePicture)); - - CreateMap() - .ForMember(x => x.Id, opt => opt.MapFrom(src => src.Id)) - .ForMember(x => x.UserId, opt => opt.MapFrom(src => src.UserId)) - .ForMember(x => x.Name, opt => opt.MapFrom(src => src.Name)) - .ForMember(x => x.Surname, opt => opt.MapFrom(src => src.Surname)) - .ForMember(x => x.Patronymic, opt => opt.MapFrom(src => src.Patronymic)) - .ForMember(x => x.Birthdate, opt => opt.MapFrom(src => src.Birthdate)) - .ForMember(x => x.Gender, opt => opt.MapFrom(src => src.Gender)) - .ForMember(x => x.ContactEmail, opt => opt.MapFrom(src => src.ContactEmail)) - .ForMember(x => x.ContactPhone, opt => opt.MapFrom(src => src.ContactPhone)) - .ForMember(x => x.ProfilePicture, opt => opt.MapFrom(src => src.ProfilePicture)); - - CreateMap() - .ForMember(x => x.Name, opt => opt.MapFrom(src => src.Name)) - .ForMember(x => x.Surname, opt => opt.MapFrom(src => src.Surname)) - .ForMember(x => x.Patronymic, opt => opt.MapFrom(src => src.Patronymic)) - .ForMember(x => x.Birthdate, opt => opt.MapFrom(src => src.Birthdate)) - .ForMember(x => x.Gender, opt => opt.MapFrom(src => src.Gender)) - .ForMember(x => x.ContactEmail, opt => opt.MapFrom(src => src.ContactEmail)) - .ForMember(x => x.ContactPhone, opt => opt.MapFrom(src => src.ContactPhone)) - .ForMember(x => x.ProfilePicture, opt => opt.MapFrom(src => src.ProfilePicture)); - - #endregion - } } diff --git a/Models/DTO/AuthDTO.cs b/Models/DTO/AuthDTO.cs deleted file mode 100755 index 5acacfc..0000000 --- a/Models/DTO/AuthDTO.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System.ComponentModel.DataAnnotations; - -namespace GamificationService.Models.DTO; - -public class AuthDTO -{ - [Required] - [StringLength(50, MinimumLength = 3, ErrorMessage = "Username must be between 3 and 50 characters")] - public string Username { get; set; } = null!; - - [Required] - [EmailAddress(ErrorMessage = "Invalid email address")] - public string Email { get; set; } = null!; - - [Required] - [StringLength(100, MinimumLength = 8, ErrorMessage = "Password must be between 8 and 100 characters")] - public string Password { get; set; } = null!; - - [Required] - public bool RememberMe { get; set; } -} diff --git a/Models/DTO/DisableTwoFactorDTO.cs b/Models/DTO/DisableTwoFactorDTO.cs deleted file mode 100644 index fee92cc..0000000 --- a/Models/DTO/DisableTwoFactorDTO.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace GamificationService.Models.DTO; - -public class DisableTwoFactorDTO -{ - public int TwoFactorProvider { get; set; } - public string Code { get; set; } -} diff --git a/Models/DTO/EnableTwoFactorDTO.cs b/Models/DTO/EnableTwoFactorDTO.cs deleted file mode 100644 index 87fc745..0000000 --- a/Models/DTO/EnableTwoFactorDTO.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace GamificationService.Models.DTO; - -public class EnableTwoFactorDTO -{ - public int TwoFactorProvider { get; set; } -} diff --git a/Models/DTO/GetAllRightsResponse.cs b/Models/DTO/GetAllRightsResponse.cs deleted file mode 100644 index 071b87a..0000000 --- a/Models/DTO/GetAllRightsResponse.cs +++ /dev/null @@ -1,11 +0,0 @@ -using GamificationService.Models.Database; - -namespace GamificationService.Models.DTO; - -public class GetAllRightsResponse -{ - public List Rights { get; set; } - public int TotalCount { get; set; } - public int PageNumber { get; set; } - public int PageSize { get; set; } -} diff --git a/Models/DTO/GetAllRolesResponse.cs b/Models/DTO/GetAllRolesResponse.cs deleted file mode 100644 index 4dfa166..0000000 --- a/Models/DTO/GetAllRolesResponse.cs +++ /dev/null @@ -1,11 +0,0 @@ -using GamificationService.Models.Database; - -namespace GamificationService.Models.DTO; - -public class GetAllRolesResponse -{ - public List Roles { get; set; } - public int TotalCount { get; set; } - public int PageNumber { get; set; } - public int PageSize { get; set; } -} diff --git a/Models/DTO/GetTwoFactorDTO.cs b/Models/DTO/GetTwoFactorDTO.cs deleted file mode 100644 index bbafc06..0000000 --- a/Models/DTO/GetTwoFactorDTO.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System.ComponentModel.DataAnnotations; - -namespace GamificationService.Models.DTO; - -public class GetTwoFactorDTO -{ - [Required] - public int TwoFactorProvider { get; set; } - [StringLength(50, MinimumLength = 3, ErrorMessage = "Username must be between 3 and 50 characters")] - public string? Username { get; set; } = null!; -} diff --git a/Models/DTO/LoginResultResponse.cs b/Models/DTO/LoginResultResponse.cs deleted file mode 100755 index b1236ff..0000000 --- a/Models/DTO/LoginResultResponse.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace GamificationService.Models.DTO; - -public class LoginResultResponse -{ - public bool? RequiresTwoFactorAuth { get; set; } - public bool Success { get; set; } - public RefreshTokenDTO? Token { get; set; } - public int? TwoFactorProvider { get; set; } -} diff --git a/Models/DTO/RefreshTokenDTO.cs b/Models/DTO/RefreshTokenDTO.cs deleted file mode 100755 index 28f3b83..0000000 --- a/Models/DTO/RefreshTokenDTO.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace GamificationService.Models.DTO; - -public class RefreshTokenDTO -{ - public string AccessToken { get; set; } = null!; - public string RefreshToken { get; set; } = null!; -} diff --git a/Models/DTO/RightDTO.cs b/Models/DTO/RightDTO.cs deleted file mode 100644 index 433ef5c..0000000 --- a/Models/DTO/RightDTO.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace GamificationService.Models.DTO; - -public class RightDTO -{ - public string Name { get; set; } - public string Description { get; set; } -} diff --git a/Models/DTO/RoleDTO.cs b/Models/DTO/RoleDTO.cs deleted file mode 100644 index c1978ad..0000000 --- a/Models/DTO/RoleDTO.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace GamificationService.Models.DTO; - -public class RoleDTO -{ - public string Name { get; set; } - public string Description { get; set; } -} diff --git a/Models/DTO/TwoFactorDTO.cs b/Models/DTO/TwoFactorDTO.cs deleted file mode 100755 index d5601e8..0000000 --- a/Models/DTO/TwoFactorDTO.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System.ComponentModel.DataAnnotations; - -namespace GamificationService.Models.DTO; - -public class TwoFactorDTO -{ - [Required] - public int TwoFactorProvider { get; set; } - [StringLength(50, MinimumLength = 3, ErrorMessage = "Username must be between 3 and 50 characters")] - public string? Username { get; set; } = null!; - - [Required] - [StringLength(6, MinimumLength = 6, ErrorMessage = "Code must be 6 characters long")] - public string Code { get; set; } = null!; - public bool RememberMe { get; set; } -} diff --git a/Models/DTO/UserProfileCreateDTO.cs b/Models/DTO/UserProfileCreateDTO.cs deleted file mode 100644 index 307f1a2..0000000 --- a/Models/DTO/UserProfileCreateDTO.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System.ComponentModel.DataAnnotations; -using GamificationService.Utils.Enums; - -namespace GamificationService.Models.DTO; - -public class UserProfileCreateDTO -{ - [Required(ErrorMessage = "Name is required")] - [StringLength(100, ErrorMessage = "Name must be less than 100 characters")] - public string Name { get; set; } = null!; - - [Required(ErrorMessage = "Surname is required")] - [StringLength(100, ErrorMessage = "Surname must be less than 100 characters")] - public string Surname { get; set; } = null!; - - [StringLength(50, ErrorMessage = "Patronymic must be less than 50 characters")] - public string? Patronymic { get; set; } - - [Required(ErrorMessage = "Birthdate is required")] - public DateTime Birthdate { get; set; } - - [Required(ErrorMessage = "Gender is required")] - public Gender Gender { get; set; } - - [EmailAddress(ErrorMessage = "Invalid email")] - public string? ContactEmail { get; set; } - - [Phone(ErrorMessage = "Invalid contact phone number")] - public string? ContactPhone { get; set; } - - [Url(ErrorMessage = "Invalid avatar url")] - public string? ProfilePicture { get; set; } -} diff --git a/Models/DTO/UserProfileDTO.cs b/Models/DTO/UserProfileDTO.cs deleted file mode 100755 index d540605..0000000 --- a/Models/DTO/UserProfileDTO.cs +++ /dev/null @@ -1,37 +0,0 @@ -using System.ComponentModel.DataAnnotations; -using GamificationService.Utils.Enums; - -namespace GamificationService.Models.DTO; - -public class UserProfileDTO -{ - public long? Id { get; set; } - - public long? UserId { get; set; } - - [Required(ErrorMessage = "Name is required")] - [StringLength(100, ErrorMessage = "Name must be less than 100 characters")] - public string Name { get; set; } = null!; - - [Required(ErrorMessage = "Surname is required")] - [StringLength(100, ErrorMessage = "Surname must be less than 100 characters")] - public string Surname { get; set; } = null!; - - [StringLength(50, ErrorMessage = "Patronymic must be less than 50 characters")] - public string? Patronymic { get; set; } - - [Required(ErrorMessage = "Birthdate is required")] - public DateTime Birthdate { get; set; } - - [Required(ErrorMessage = "Gender is required")] - public Gender Gender { get; set; } - - [EmailAddress(ErrorMessage = "Invalid email")] - public string? ContactEmail { get; set; } - - [Phone(ErrorMessage = "Invalid contact phone number")] - public string? ContactPhone { get; set; } - - [Url(ErrorMessage = "Invalid avatar url")] - public string? ProfilePicture { get; set; } -} diff --git a/Models/Database/ApplicationRole.cs b/Models/Database/ApplicationRole.cs deleted file mode 100755 index 4d778a8..0000000 --- a/Models/Database/ApplicationRole.cs +++ /dev/null @@ -1,15 +0,0 @@ -using Microsoft.AspNetCore.Identity; -using StackExchange.Redis; - -namespace GamificationService.Models.Database; - - -public class ApplicationRole : IdentityRole -{ - public ApplicationRole() : base() { } - public ApplicationRole(string roleName) : base(roleName) { } - public string? Description { get; set; } - - public List UserRoles { get; set; } = new List(); - public List RoleRights { get; set; } = new List(); -} diff --git a/Models/Database/ApplicationUser.cs b/Models/Database/ApplicationUser.cs deleted file mode 100755 index ab7a7ba..0000000 --- a/Models/Database/ApplicationUser.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System.ComponentModel.DataAnnotations; -using GamificationService.Utils; -using Microsoft.AspNetCore.Identity; - -namespace GamificationService.Models.Database; - -public class ApplicationUser : IdentityUser -{ - [Required(ErrorMessage = "Username is required")] - [StringLength(50, ErrorMessage = "Username must be less than 50 characters")] - public string Username { get; set; } = null!; - public bool TwoFactorEnabled { get; set; } - public string? TwoFactorSecret { get; set; } - public bool EmailConfirmed { get; set; } - public List TwoFactorProviders { get; set; } = new List(); - public List RefreshTokens { get; set; } = new List(); - - public List UserRoles { get; set; } = new List(); -} diff --git a/Models/Database/RefreshToken.cs b/Models/Database/RefreshToken.cs deleted file mode 100755 index d331dd9..0000000 --- a/Models/Database/RefreshToken.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System.ComponentModel.DataAnnotations; - -namespace GamificationService.Models.Database; - -public class RefreshToken -{ - [Key] - public long Id { get; set; } - - public long UserId { get; set; } - public ApplicationUser User { get; set; } = null!; - - [Required] - public string Token { get; set; } = null!; - - public DateTime Expires { get; set; } - public DateTime Created { get; set; } - public bool IsExpired => DateTime.UtcNow >= Expires; - - public bool IsRevoked { get; set; } - public string? RevokedByIp { get; set; } - public DateTime? RevokedOn { get; set; } - - public bool IsActive => !IsRevoked && !IsExpired; -} diff --git a/Models/Database/Right.cs b/Models/Database/Right.cs deleted file mode 100755 index 856f727..0000000 --- a/Models/Database/Right.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System.ComponentModel.DataAnnotations; - -namespace GamificationService.Models.Database; - -public class Right -{ - [Key] - public long Id { get; set; } - - [Required] - [StringLength(50)] - public string Name { get; set; } = null!; - - [StringLength(100)] - public string? Description { get; set; } - - public List RoleRights { get; set; } = new List(); -} diff --git a/Models/Database/RoleRight.cs b/Models/Database/RoleRight.cs deleted file mode 100755 index 80c233e..0000000 --- a/Models/Database/RoleRight.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace GamificationService.Models.Database; - -public class RoleRight -{ - public long RoleId { get; set; } - public ApplicationRole Role { get; set; } = null!; - - public long RightId { get; set; } - public Right Right { get; set; } = null!; -} diff --git a/Models/Database/UserProfile.cs b/Models/Database/UserProfile.cs deleted file mode 100755 index 1806614..0000000 --- a/Models/Database/UserProfile.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System.ComponentModel.DataAnnotations; -using GamificationService.Utils.Enums; - -namespace GamificationService.Models.Database; - -public class UserProfile -{ - [Key] - public long Id { get; set; } - - [Required(ErrorMessage = "User is required")] - public long UserId { get; set; } - public ApplicationUser? User { get; set; } - - [Required(ErrorMessage = "Name is required")] - [StringLength(100, ErrorMessage = "Name must be less than 100 characters")] - public string Name { get; set; } = null!; - - [Required(ErrorMessage = "Surname is required")] - [StringLength(100, ErrorMessage = "Surname must be less than 100 characters")] - public string Surname { get; set; } = null!; - - [StringLength(50, ErrorMessage = "Patronymic must be less than 50 characters")] - public string? Patronymic { get; set; } - - [Required(ErrorMessage = "Gender is required")] - public Gender Gender { get; set; } - - [Required(ErrorMessage = "Birthdate is required")] - public DateTime Birthdate { get; set; } - - [EmailAddress(ErrorMessage = "Invalid email")] - public string? ContactEmail { get; set; } - - [Phone(ErrorMessage = "Invalid contact phone number")] - public string? ContactPhone { get; set; } - - [Url(ErrorMessage = "Invalid avatar url")] - public string? ProfilePicture { get; set; } -} diff --git a/Models/Database/UserRole.cs b/Models/Database/UserRole.cs deleted file mode 100755 index f725bb1..0000000 --- a/Models/Database/UserRole.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace GamificationService.Models.Database; - -public class UserRole -{ - public long UserId { get; set; } - public ApplicationUser User { get; set; } = null!; - - public long RoleId { get; set; } - public ApplicationRole Role { get; set; } = null!; -} diff --git a/Models/Messages/UserProfiles/CreateUserProfileRequest.cs b/Models/Messages/UserProfiles/CreateUserProfileRequest.cs deleted file mode 100644 index aec4dbf..0000000 --- a/Models/Messages/UserProfiles/CreateUserProfileRequest.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace GamificationService.Models.Messages.UserProfiles; - -public class CreateUserProfileRequest -{ - -} diff --git a/Models/Messages/UserProfiles/CreateUserProfileResponse.cs b/Models/Messages/UserProfiles/CreateUserProfileResponse.cs deleted file mode 100644 index ddb8dbd..0000000 --- a/Models/Messages/UserProfiles/CreateUserProfileResponse.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace GamificationService.Models.Messages.UserProfiles; - -public class CreateUserProfileResponse -{ - -} - diff --git a/Models/Messages/UserProfiles/UpdateUserProfileRequest.cs b/Models/Messages/UserProfiles/UpdateUserProfileRequest.cs deleted file mode 100644 index c31c771..0000000 --- a/Models/Messages/UserProfiles/UpdateUserProfileRequest.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace GamificationService.Models.Messages.UserProfiles; - -public class UpdateUserProfileRequest -{ - -} diff --git a/Models/Messages/UserProfiles/UpdateUserProfileResponse.cs b/Models/Messages/UserProfiles/UpdateUserProfileResponse.cs deleted file mode 100644 index cefff9f..0000000 --- a/Models/Messages/UserProfiles/UpdateUserProfileResponse.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace GamificationService.Models.Messages.UserProfiles; - -public class UpdateUserProfileResponse -{ - -} diff --git a/Models/UserSession.cs b/Models/UserSession.cs deleted file mode 100755 index fbbe5c7..0000000 --- a/Models/UserSession.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace GamificationService.Models; - -public class UserSession -{ - public string? Login { get; set; } - public bool IsAuthenticated { get; set; } -} diff --git a/Services/Cookies/CookieService.cs b/Services/Cookies/CookieService.cs deleted file mode 100755 index a7a3bbf..0000000 --- a/Services/Cookies/CookieService.cs +++ /dev/null @@ -1,47 +0,0 @@ -using GamificationService.Exceptions.UtilServices.Cookies; - -namespace GamificationService.Services.Cookies; - -public class CookieService : ICookieService -{ - private readonly IHttpContextAccessor _httpContextAccessor; - private readonly ILogger _logger; - private readonly IConfiguration _configuration; - - public CookieService(IHttpContextAccessor httpContextAccessor, ILogger logger, IConfiguration configuration) - { - _httpContextAccessor = httpContextAccessor; - _logger = logger; - _configuration = configuration; - } - - public Task 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 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); - } - } -} diff --git a/Services/Cookies/ICookieService.cs b/Services/Cookies/ICookieService.cs deleted file mode 100755 index cecfe0d..0000000 --- a/Services/Cookies/ICookieService.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace GamificationService.Services.Cookies; - -public interface ICookieService -{ - Task SetCookie(string key, string value, CookieOptions options); - Task RemoveCookie(string key); -} diff --git a/Services/CurrentUsers/CurrentUserService.cs b/Services/CurrentUsers/CurrentUserService.cs deleted file mode 100644 index e1ab572..0000000 --- a/Services/CurrentUsers/CurrentUserService.cs +++ /dev/null @@ -1,26 +0,0 @@ -using GamificationService.Models; - -namespace GamificationService.Services.CurrentUsers; - -public class CurrentUserService : ICurrentUserService -{ - private readonly IHttpContextAccessor _httpContextAccessor; - private readonly ILogger _logger; - public CurrentUserService(IHttpContextAccessor httpContextAccessor, ILogger 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; - } - -} diff --git a/Services/CurrentUsers/ICurrentUserService.cs b/Services/CurrentUsers/ICurrentUserService.cs deleted file mode 100644 index a71c6de..0000000 --- a/Services/CurrentUsers/ICurrentUserService.cs +++ /dev/null @@ -1,8 +0,0 @@ -using GamificationService.Models; - -namespace GamificationService.Services.CurrentUsers; - -public interface ICurrentUserService -{ - UserSession GetCurrentUser(); -} diff --git a/Services/JWT/IJWTService.cs b/Services/JWT/IJWTService.cs deleted file mode 100755 index 917397e..0000000 --- a/Services/JWT/IJWTService.cs +++ /dev/null @@ -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 GenerateRefreshTokenAsync(ApplicationUser user); - Task RevokeRefreshTokenAsync(long userId, string refreshToken, string remoteIpAddress); -} diff --git a/Services/JWT/JWTService.cs b/Services/JWT/JWTService.cs deleted file mode 100755 index 0e85597..0000000 --- a/Services/JWT/JWTService.cs +++ /dev/null @@ -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 _logger; - private readonly UnitOfWork _unitOfWork; - - #endregion - - - public JwtService(IConfiguration configuration, ILogger 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 - { - 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 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(); - } - } -} diff --git a/Services/Notification/INotificationService.cs b/Services/Notification/INotificationService.cs deleted file mode 100755 index d1f8942..0000000 --- a/Services/Notification/INotificationService.cs +++ /dev/null @@ -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); -} diff --git a/Services/Notification/NotificationService.cs b/Services/Notification/NotificationService.cs deleted file mode 100755 index 7c56a4e..0000000 --- a/Services/Notification/NotificationService.cs +++ /dev/null @@ -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 _logger; - private readonly PushNotificationsClient _pushNotificationsClient; - - #endregion - - #region Constructor - - public NotificationService(EmailClient emailClient, PushNotificationsClient pushNotificationsClient, ILogger 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; - } - } -} diff --git a/Services/Rights/IRightsService.cs b/Services/Rights/IRightsService.cs deleted file mode 100755 index dde6c7e..0000000 --- a/Services/Rights/IRightsService.cs +++ /dev/null @@ -1,12 +0,0 @@ -using GamificationService.Models.Database; - -namespace GamificationService.Services.Rights; - -public interface IRightsService -{ - Task CreateRightAsync(string rightName, string description); - Task UpdateRightAsync(long rightId, string newRightName, string newDescription); - Task DeleteRightAsync(long rightId); - Task GetRightByIdAsync(long rightId); - Task<(List Rights, int TotalCount)> GetAllRightsAsync(int pageNumber = 1, int pageSize = 10); -} diff --git a/Services/Rights/RightsService.cs b/Services/Rights/RightsService.cs deleted file mode 100755 index 28b94dc..0000000 --- a/Services/Rights/RightsService.cs +++ /dev/null @@ -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 _logger; - - #endregion - - #region Constructor - - public RightsService(UnitOfWork unitOfWork, ILogger logger) - { - _unitOfWork = unitOfWork; - _logger = logger; - } - - #endregion - - #region Methods - - public async Task 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 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 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 GetRightByIdAsync(long rightId) - { - return await _unitOfWork.RightRepository.GetByIDAsync(rightId); - } - - public async Task<(List 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 -} diff --git a/Services/Roles/IRolesService.cs b/Services/Roles/IRolesService.cs deleted file mode 100755 index 0a03960..0000000 --- a/Services/Roles/IRolesService.cs +++ /dev/null @@ -1,14 +0,0 @@ -using GamificationService.Models.Database; - -namespace GamificationService.Services.Roles; - -public interface IRolesService -{ - Task CreateRoleAsync(string roleName, string description); - Task UpdateRoleAsync(long roleId, string newRoleName, string newDescription); - Task DeleteRoleAsync(long roleId); - Task AddRightToRoleAsync(long roleId, long rightId); - Task RemoveRightFromRoleAsync(long roleId, long rightId); - Task GetRoleByIdAsync(long roleId); - Task<(List Roles, int TotalCount)> GetAllRolesAsync(int pageNumber = 1, int pageSize = 10); -} diff --git a/Services/Roles/RolesService.cs b/Services/Roles/RolesService.cs deleted file mode 100755 index f316f98..0000000 --- a/Services/Roles/RolesService.cs +++ /dev/null @@ -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 _logger; - private readonly UnitOfWork _unitOfWork; - - #endregion - - #region Constructor - - public RolesService(ILogger 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 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 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 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 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 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 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 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 - -} diff --git a/Services/UsersProfile/IUserProfileService.cs b/Services/UsersProfile/IUserProfileService.cs deleted file mode 100755 index cf0fe86..0000000 --- a/Services/UsersProfile/IUserProfileService.cs +++ /dev/null @@ -1,15 +0,0 @@ -using GamificationService.Models.Database; -using GamificationService.Models.DTO; - -namespace GamificationService.Services.UsersProfile; - -public interface IUserProfileService -{ - public Task AddUserProfile(long userId, UserProfileCreateDTO userProfile); - public Task AddUserProfile(UserProfile userProfile); - public UserProfile? GetUserProfileByUserId(long id); - public UserProfile? GetUserProfileById(long id); - public Task UpdateUserProfileByUserId(long userId, UserProfileCreateDTO userProfile); - public Task UpdateUserProfile(UserProfile userProfile); - public bool DeleteUserProfile(long id); -} diff --git a/Services/UsersProfile/UserProfileService.cs b/Services/UsersProfile/UserProfileService.cs deleted file mode 100755 index bf87bf2..0000000 --- a/Services/UsersProfile/UserProfileService.cs +++ /dev/null @@ -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 _logger; - private readonly IMapper _mapper; - - #endregion - - - #region Constructor - - public UserProfileService(UnitOfWork unitOfWork, ILogger logger, IMapper mapper) - { - _unitOfWork = unitOfWork; - _logger = logger; - _mapper = mapper; - } - - #endregion - - # region Methods - - public async Task AddUserProfile(long userId, UserProfileCreateDTO userProfile) - { - UserProfile userProfileEntity = _mapper.Map(userProfile); - userProfileEntity.UserId = userId; - return _mapper.Map(await AddUserProfile(userProfileEntity)); - } - - public async Task 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 UpdateUserProfileByUserId(long userId, UserProfileCreateDTO userProfile) - { - var userProfileEntityUpdated = _mapper.Map(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 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 -} diff --git a/Utils/Clients/EmailClient.cs b/Utils/Clients/EmailClient.cs deleted file mode 100755 index 1142046..0000000 --- a/Utils/Clients/EmailClient.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System.Net.Mail; -using GamificationService.Exceptions.UtilServices.Email; - -namespace GamificationService.Utils; - -public class EmailClient(SmtpClient smtpClient, string emailFrom, ILogger logger) -{ - #region Fields - - private readonly string _emailFrom = emailFrom; - private readonly ILogger _logger = logger; - - #endregion - - #region Methods - - /// - /// Sends the email using the SmtpClient instance. - /// - /// Email to send. - /// If the SmtpClient instance fails to send the email. - /// Task that represents the asynchronous operation. - public async Task SendEmail(MailMessage email, string emailTo) - { - try - { - email.To.Add(new MailAddress(emailTo)); - await smtpClient.SendMailAsync(email); - } - catch (Exception ex) - { - _logger.LogError(ex, ex.Message); - throw new SendEmailException("Failed to send email", ex); - } - } - - #endregion -} diff --git a/Utils/Clients/PushNotificationsClient.cs b/Utils/Clients/PushNotificationsClient.cs deleted file mode 100755 index 855ab18..0000000 --- a/Utils/Clients/PushNotificationsClient.cs +++ /dev/null @@ -1,150 +0,0 @@ -using System.Net; -using System.Net.Http.Headers; -using System.Text; -using System.Text.Json; -using GamificationService.Exceptions.UtilServices.Api; - -namespace GamificationService.Utils; - -public class PushNotificationsClient -{ - #region Fields - - private readonly HttpClient _httpClient; - private readonly ILogger _logger; - private readonly string _applicationToken; - private readonly string _projectId; - #endregion - - #region Constructor - - public PushNotificationsClient(HttpClient httpClient, ILogger logger, string applicationToken, string projectId) - { - _httpClient = httpClient; - _logger = logger; - _applicationToken = applicationToken; - _projectId = projectId; - } - - #endregion - - #region Methods - - public async Task SendPushNotification(PushNotification pushNotification) - { - try - { - var payload = new - { - message = new - { - token = _applicationToken, - notification = new - { - body = pushNotification.Message, - title = pushNotification.Title, - image = pushNotification.Image - }, - android = new - { - notification = new - { - body = pushNotification.Message, - title = pushNotification.Title, - image = pushNotification.Image, - click_action = pushNotification.ClickAction, - click_action_type = pushNotification.ClickActionType - } - } - } - }; - - var jsonPayload = JsonSerializer.Serialize(payload); - - var request = new HttpRequestMessage(HttpMethod.Post,$"/{_projectId}/messages") - { - Content = new StringContent(jsonPayload, Encoding.UTF8, "application/json") - }; - - var response = await _httpClient.SendAsync(request); - - if (response.StatusCode == HttpStatusCode.BadRequest) - { - var responseContent = await response.Content.ReadAsStringAsync(); - _logger.LogError($"Failed to send push notification. Status Code: {response.StatusCode}, Response: {responseContent}"); - throw new BadRequestException($"Failed to send push notification: {response.StatusCode}"); - } - else if (response.StatusCode == HttpStatusCode.Forbidden) - { - var responseContent = await response.Content.ReadAsStringAsync(); - _logger.LogError($"Failed to send push notification: {response.StatusCode}, Response: {responseContent}"); - throw new ForbiddenException($"Failed to send push notification: {response.StatusCode}"); - } - else - { - var responseContent = await response.Content.ReadAsStringAsync(); - _logger.LogError($"Failed to send push notification: {response.StatusCode}, Response: {responseContent}"); - throw new Exception($"Failed to send push notification: {response.StatusCode}"); - } - } - catch (Exception e) - { - _logger.LogError(e, e.Message); - throw; - } - } - - public async Task SendPushNotification(PushNotification pushNotification, string topic) - { - try - { - var payload = new - { - message = new - { - notification = new - { - body = pushNotification.Message, - title = pushNotification.Title, - image = pushNotification.Image - } - } - }; - - var jsonPayload = JsonSerializer.Serialize(payload); - - var request = new HttpRequestMessage(HttpMethod.Post,$"/{_projectId}/topics/{topic}/publish") - { - Content = new StringContent(jsonPayload, Encoding.UTF8, "application/json") - }; - - var response = await _httpClient.SendAsync(request); - - if (response.StatusCode == HttpStatusCode.BadRequest) - { - var responseContent = await response.Content.ReadAsStringAsync(); - _logger.LogError($"Failed to send push notification. Status Code: {response.StatusCode}, Response: {responseContent}"); - throw new BadRequestException($"Failed to send push notification: {response.StatusCode}"); - } - else if (response.StatusCode == HttpStatusCode.Forbidden) - { - var responseContent = await response.Content.ReadAsStringAsync(); - _logger.LogError($"Failed to send push notification: {response.StatusCode}, Response: {responseContent}"); - throw new ForbiddenException($"Failed to send push notification: {response.StatusCode}"); - } - else - { - var responseContent = await response.Content.ReadAsStringAsync(); - _logger.LogError($"Failed to send push notification: {response.StatusCode}, Response: {responseContent}"); - throw new Exception($"Failed to send push notification: {response.StatusCode}"); - } - } - catch (Exception e) - { - _logger.LogError(e, e.Message); - throw; - } - } - - #endregion -} diff --git a/Utils/Enums/Gender.cs b/Utils/Enums/Gender.cs deleted file mode 100644 index 00e18d2..0000000 --- a/Utils/Enums/Gender.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace GamificationService.Utils.Enums; - -public enum Gender -{ - Male, - Female -} diff --git a/Utils/Enums/InstructionTestScoreCalcMethod.cs b/Utils/Enums/InstructionTestScoreCalcMethod.cs deleted file mode 100644 index 51184d9..0000000 --- a/Utils/Enums/InstructionTestScoreCalcMethod.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace GamificationService.Utils.Enums -{ - public enum InstructionTestScoreCalcMethod - { - AverageGrade, - MaxGrade - } -} diff --git a/Utils/Enums/NotificationInformationType.cs b/Utils/Enums/NotificationInformationType.cs deleted file mode 100755 index 0c78444..0000000 --- a/Utils/Enums/NotificationInformationType.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace GamificationService.Utils; - -public enum NotificationInformationType -{ - AUTH, - INFO, - WARNING, - ERROR -} diff --git a/Utils/Factory/MailNotificationsFactory.cs b/Utils/Factory/MailNotificationsFactory.cs deleted file mode 100755 index e123071..0000000 --- a/Utils/Factory/MailNotificationsFactory.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System.Net.Mail; - -namespace GamificationService.Utils.Factory; - -public class MailNotificationsFactory -{ - public static Notification CreateNotification(NotificationInformationType type, - string title, - string message,List attachments) - { - return new MailNotification(type, title, message, attachments); - } - public Notification CreateNotification(NotificationInformationType type, - string title, - string message) - { - - return new MailNotification(type, title, message); - - } -} diff --git a/Utils/Factory/PushNotificationsFactory.cs b/Utils/Factory/PushNotificationsFactory.cs deleted file mode 100755 index 5290e58..0000000 --- a/Utils/Factory/PushNotificationsFactory.cs +++ /dev/null @@ -1,28 +0,0 @@ -namespace GamificationService.Utils.Factory; - -public class PushNotificationsFactory -{ - public Notification CreateNotification(NotificationInformationType type, - string title, - string message) - { - return new PushNotification(type, title, message); - } - - public Notification CreateNotification(NotificationInformationType type, - string title, - string message, - string image) - { - return new PushNotification(type, title, message, image); - } - public Notification CreateNotification(NotificationInformationType type, - string title, - string message, - string? image, - string clickAction, - ClickActionType clickActionType) - { - return new PushNotification(type, title, message, image, clickAction, clickActionType); - } -} diff --git a/Utils/MailNotification.cs b/Utils/MailNotification.cs deleted file mode 100755 index a035fa2..0000000 --- a/Utils/MailNotification.cs +++ /dev/null @@ -1,111 +0,0 @@ -using System.Net.Mail; - -namespace GamificationService.Utils; - -public class MailNotification : Notification -{ - #region Fields - - private List _attachments; - - #endregion - - #region Properties - public List Attachments { get => _attachments; } - - #endregion - - #region Constructor - - public MailNotification(NotificationInformationType type, string title, string message, List attachments) : base(type, title, message) - { - _attachments = attachments; - } - public MailNotification(NotificationInformationType type, string title, string message) : base(type, title, message) - { - } - #endregion - - #region Methods - - public MailMessage ConvertToMailMessage() - { - var mailMessage = new MailMessage - { - Subject = CreateTitle(), - Body = CreateBody(), - IsBodyHtml = true, - Priority = GetPriority() - }; - if (_attachments != null) - { - mailMessage.Attachments.ToList().AddRange(_attachments); - } - return mailMessage; - } - - #endregion - - #region Private Methods - private string CreateTitle() - { - switch (Type) - { - case NotificationInformationType.AUTH: - return "Авторизация " + Title; - case NotificationInformationType.INFO: - return "Информация "+ Title; - case NotificationInformationType.WARNING: - return "Предупреждение "+ Title; - case NotificationInformationType.ERROR: - return "Ошибка "+ Title; - default: - return "Информация "+ Title; - } - } - - private string CreateBody() - { - string formattedMessage; - - switch (Type) - { - case NotificationInformationType.AUTH: - formattedMessage = "Вы успешно авторизовались."; - break; - case NotificationInformationType.INFO: - formattedMessage = "Это информационное сообщение."; - break; - case NotificationInformationType.WARNING: - formattedMessage = "Внимание! Обратите внимание на это предупреждение."; - break; - case NotificationInformationType.ERROR: - formattedMessage = "Произошла ошибка. Пожалуйста, проверьте детали."; - break; - default: - formattedMessage = "Сообщение не определено."; - break; - } - - return $"

{formattedMessage} {Message}

"; - } - - private MailPriority GetPriority() - { - switch (Type) - { - case NotificationInformationType.AUTH: - return MailPriority.High; - case NotificationInformationType.INFO: - return MailPriority.Normal; - case NotificationInformationType.WARNING: - return MailPriority.Low; - case NotificationInformationType.ERROR: - return MailPriority.High; - default: - return MailPriority.Normal; - } - } - - #endregion -} diff --git a/Utils/Notification.cs b/Utils/Notification.cs deleted file mode 100755 index 0d8e099..0000000 --- a/Utils/Notification.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System.Net.Mail; - -namespace GamificationService.Utils; - -public abstract class Notification -{ - #region Fields - - private string _title; - private string _message; - private NotificationInformationType _type; - - #endregion - - #region Parameters - - public string Title { get => _title;} - public string Message { get => _message; } - public NotificationInformationType Type { get => _type; } - - #endregion - - #region Constructor - - public Notification(NotificationInformationType type, string title, string message) - { - _type = type; - _title = title; - _message = message; - } - - #endregion -} diff --git a/Utils/PushNotification.cs b/Utils/PushNotification.cs deleted file mode 100755 index 034212b..0000000 --- a/Utils/PushNotification.cs +++ /dev/null @@ -1,40 +0,0 @@ -namespace GamificationService.Utils; - -public class PushNotification : Notification -{ - #region Fields - - private readonly string? _image; - private readonly string? _clickAction; - private readonly ClickActionType? _clickActionType; - - #endregion - - #region Properties - - public string? Image { get => _image; } - public string? ClickAction { get => _clickAction; } - public int? ClickActionType { get => (int)_clickActionType; } - - #endregion - - #region Constructor - - public PushNotification(NotificationInformationType type, string title, string message, string image, string clickAction, ClickActionType clickActionType) : base(type, title, message) - { - _image = image; - _clickAction = clickAction; - _clickActionType = clickActionType; - } - - public PushNotification(NotificationInformationType type, string title, string message, string? image) : base(type, title, message) - { - _image = image; - } - - public PushNotification(NotificationInformationType type, string title, string message) : base(type, title, message) - { - } - - #endregion -} diff --git a/Utils/TwoFactorProvider.cs b/Utils/TwoFactorProvider.cs deleted file mode 100644 index 0184174..0000000 --- a/Utils/TwoFactorProvider.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace GamificationService.Utils; - -public enum TwoFactorProvider -{ - NONE, - EMAIL, - PHONE, - PUSH, - AUTHENTICATOR -}