From a246764e44953ce75fefce26cde8b0d9f2d3255f Mon Sep 17 00:00:00 2001 From: Martin Velebil Date: Sun, 10 Aug 2025 18:33:36 +0200 Subject: [PATCH] Movesadmin status update endpoint Relocates the endpoint for updating user admin status from the dedicated AdminController to the UserProfileController. This consolidates user profile management under a single controller and leverages existing authorization policies. --- DrinkRateAPI/Controllers/AdminController.cs | 30 ------------------- .../Controllers/UserProfileController.cs | 23 +++++++++++++- DrinkRateAPI/Services/UserProfileService.cs | 5 ++-- 3 files changed, 24 insertions(+), 34 deletions(-) delete mode 100644 DrinkRateAPI/Controllers/AdminController.cs diff --git a/DrinkRateAPI/Controllers/AdminController.cs b/DrinkRateAPI/Controllers/AdminController.cs deleted file mode 100644 index 4bbc46d..0000000 --- a/DrinkRateAPI/Controllers/AdminController.cs +++ /dev/null @@ -1,30 +0,0 @@ -using DrinkRateAPI.ApiModels.UserProfile; -using DrinkRateAPI.Services; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Mvc; - -namespace DrinkRateAPI.Controllers; - -[ApiController] -[Route("admin")] -[Authorize(Policy = "AdminOnly")] -public class AdminController : ControllerBase -{ - private readonly ILogger _logger; - private readonly UserProfileService _userProfileService; - - public AdminController(ILogger logger, UserProfileService userProfileService) - { - _logger = logger; - _userProfileService = userProfileService; - } - - [HttpPut("users/{userId}/adminStatus")] - [Produces("application/json")] - public async Task PutUserAdminStatus(string userId, [FromBody] ChangeAdminStatusBody body) - { - var changedProfile = await _userProfileService.ChangeUserAdminStatusAsync(userId, body.ChangeStatusTo); - - return Ok(changedProfile); - } -} \ No newline at end of file diff --git a/DrinkRateAPI/Controllers/UserProfileController.cs b/DrinkRateAPI/Controllers/UserProfileController.cs index 3405fd6..afc1bcf 100644 --- a/DrinkRateAPI/Controllers/UserProfileController.cs +++ b/DrinkRateAPI/Controllers/UserProfileController.cs @@ -1,13 +1,24 @@ using System.Security.Claims; using DrinkRateAPI.ApiModels.UserProfile; +using DrinkRateAPI.Services; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace DrinkRateAPI.Controllers; [ApiController] -[Route("user_profile")] +[Route("userProfile")] public class UserProfileController : ControllerBase { + private readonly ILogger _logger; + private readonly UserProfileService _userProfileService; + + public UserProfileController(ILogger logger, UserProfileService userProfileService) + { + _logger = logger; + _userProfileService = userProfileService; + } + [HttpPut(Name = "user_profile")] public UserProfileGet PutUserProfile(UserProfilePut userProfile) { @@ -15,4 +26,14 @@ public class UserProfileController : ControllerBase var x = User.FindFirst(ClaimTypes.NameIdentifier)?.Value; //HttpContext.User.Identities.First(); return new(); } + + [HttpPut("{userId}/adminStatus")] + [Authorize(Policy = "AdminOnly")] + [Produces("application/json")] + public async Task PutUserAdminStatus(string userId, [FromBody] ChangeAdminStatusBody body) + { + var changedProfile = await _userProfileService.PutUserProfileAdminStatusAsync(userId, body.ChangeStatusTo); + + return Ok(changedProfile); + } } \ No newline at end of file diff --git a/DrinkRateAPI/Services/UserProfileService.cs b/DrinkRateAPI/Services/UserProfileService.cs index 96b1091..8726f93 100644 --- a/DrinkRateAPI/Services/UserProfileService.cs +++ b/DrinkRateAPI/Services/UserProfileService.cs @@ -11,8 +11,7 @@ using Microsoft.AspNetCore.Mvc; namespace DrinkRateAPI.Services; -public class UserProfileService(ApplicationDbContext context, - ApplicationUserService applicationUserService) +public class UserProfileService(ApplicationDbContext context, ApplicationUserService applicationUserService) { private ApplicationDbContext _context = context; private ApplicationUserService _applicationUserService = applicationUserService; @@ -22,7 +21,7 @@ public class UserProfileService(ApplicationDbContext context, return userProfile.IsAdmin; } - public async Task ChangeUserAdminStatusAsync(string userId, bool changeStatusTo) + public async Task PutUserProfileAdminStatusAsync(string userId, bool changeStatusTo) { var userProfile = GetUserProfileById(userId); userProfile.IsAdmin = changeStatusTo;