From b2b8d1e076df997b856efcaa33d1904eabc749aa Mon Sep 17 00:00:00 2001 From: martinshoob Date: Sun, 10 Aug 2025 16:23:35 +0200 Subject: [PATCH] Refactor admin endpoint and logic Moves the ChangeUserAdminStatusRequest to the ApiModels folder. Updates the admin controller route to "admin" and the admin status endpoint to "adminStatus". Makes the ChangeUserAdminStatus method asynchronous. Uses NotFoundException instead of KeyNotFoundException. --- .../UserProfile}/ChangeUserAdminStatusRequest.cs | 2 +- DrinkRateAPI/Controllers/AdminController.cs | 8 ++++---- DrinkRateAPI/DrinkRateAPI.csproj | 1 + DrinkRateAPI/Services/UserProfileService.cs | 6 +++--- 4 files changed, 9 insertions(+), 8 deletions(-) rename DrinkRateAPI/{Requests => ApiModels/UserProfile}/ChangeUserAdminStatusRequest.cs (75%) diff --git a/DrinkRateAPI/Requests/ChangeUserAdminStatusRequest.cs b/DrinkRateAPI/ApiModels/UserProfile/ChangeUserAdminStatusRequest.cs similarity index 75% rename from DrinkRateAPI/Requests/ChangeUserAdminStatusRequest.cs rename to DrinkRateAPI/ApiModels/UserProfile/ChangeUserAdminStatusRequest.cs index 03ac6f2..e28228d 100644 --- a/DrinkRateAPI/Requests/ChangeUserAdminStatusRequest.cs +++ b/DrinkRateAPI/ApiModels/UserProfile/ChangeUserAdminStatusRequest.cs @@ -1,4 +1,4 @@ -namespace DrinkRateAPI.Requests; +namespace DrinkRateAPI.ApiModels.UserProfile; public class ChangeUserAdminStatusRequest { diff --git a/DrinkRateAPI/Controllers/AdminController.cs b/DrinkRateAPI/Controllers/AdminController.cs index 6c42c67..59223f5 100644 --- a/DrinkRateAPI/Controllers/AdminController.cs +++ b/DrinkRateAPI/Controllers/AdminController.cs @@ -1,11 +1,11 @@ -using DrinkRateAPI.Requests; +using DrinkRateAPI.ApiModels.UserProfile; using DrinkRateAPI.Services; using Microsoft.AspNetCore.Mvc; namespace DrinkRateAPI.Controllers; [ApiController] -[Route("[controller]")] +[Route("admin")] public class AdminController : ControllerBase { private readonly ILogger _logger; @@ -21,7 +21,7 @@ public class AdminController : ControllerBase } [HttpPut] - [Route("[action]")] + [Route("adminStatus")] [Produces("application/json")] public async Task PutUserAdminStatus([FromBody] ChangeUserAdminStatusRequest request) { @@ -31,7 +31,7 @@ public class AdminController : ControllerBase return Unauthorized(); } - var changedProfile = _userProfileService.ChangeUserAdminStatus(request.UserId, request.ChangeStatusTo); + var changedProfile = await _userProfileService.ChangeUserAdminStatusAsync(request.UserId, request.ChangeStatusTo); return Ok(changedProfile); } diff --git a/DrinkRateAPI/DrinkRateAPI.csproj b/DrinkRateAPI/DrinkRateAPI.csproj index dacaac5..7cdbba1 100644 --- a/DrinkRateAPI/DrinkRateAPI.csproj +++ b/DrinkRateAPI/DrinkRateAPI.csproj @@ -20,6 +20,7 @@ + diff --git a/DrinkRateAPI/Services/UserProfileService.cs b/DrinkRateAPI/Services/UserProfileService.cs index 7daa187..1eec317 100644 --- a/DrinkRateAPI/Services/UserProfileService.cs +++ b/DrinkRateAPI/Services/UserProfileService.cs @@ -15,12 +15,12 @@ public class UserProfileService(ApplicationDbContext context) return userProfile.IsAdmin; } - public DbUserProfile ChangeUserAdminStatus(string userId, bool changeStatusTo) + public async Task ChangeUserAdminStatusAsync(string userId, bool changeStatusTo) { var userProfile = GetUserProfileById(userId); userProfile.IsAdmin = changeStatusTo; _context.UserProfiles.Update(userProfile); - _context.SaveChanges(); + await _context.SaveChangesAsync(); return userProfile; } @@ -29,6 +29,6 @@ public class UserProfileService(ApplicationDbContext context) { var userProfile = _context.UserProfiles.FirstOrDefault(x => x.Id.ToString() == userId); - return userProfile ?? throw new KeyNotFoundException($"User with ID {userId} not found"); + return userProfile ?? throw new NotFoundException(); } } \ No newline at end of file