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.
This commit is contained in:
martinshoob 2025-08-10 16:23:35 +02:00
parent c0860b05d1
commit b2b8d1e076
4 changed files with 9 additions and 8 deletions

View file

@ -1,4 +1,4 @@
namespace DrinkRateAPI.Requests; namespace DrinkRateAPI.ApiModels.UserProfile;
public class ChangeUserAdminStatusRequest public class ChangeUserAdminStatusRequest
{ {

View file

@ -1,11 +1,11 @@
using DrinkRateAPI.Requests; using DrinkRateAPI.ApiModels.UserProfile;
using DrinkRateAPI.Services; using DrinkRateAPI.Services;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
namespace DrinkRateAPI.Controllers; namespace DrinkRateAPI.Controllers;
[ApiController] [ApiController]
[Route("[controller]")] [Route("admin")]
public class AdminController : ControllerBase public class AdminController : ControllerBase
{ {
private readonly ILogger<AdminController> _logger; private readonly ILogger<AdminController> _logger;
@ -21,7 +21,7 @@ public class AdminController : ControllerBase
} }
[HttpPut] [HttpPut]
[Route("[action]")] [Route("adminStatus")]
[Produces("application/json")] [Produces("application/json")]
public async Task<IActionResult> PutUserAdminStatus([FromBody] ChangeUserAdminStatusRequest request) public async Task<IActionResult> PutUserAdminStatus([FromBody] ChangeUserAdminStatusRequest request)
{ {
@ -31,7 +31,7 @@ public class AdminController : ControllerBase
return Unauthorized(); return Unauthorized();
} }
var changedProfile = _userProfileService.ChangeUserAdminStatus(request.UserId, request.ChangeStatusTo); var changedProfile = await _userProfileService.ChangeUserAdminStatusAsync(request.UserId, request.ChangeStatusTo);
return Ok(changedProfile); return Ok(changedProfile);
} }

View file

@ -20,6 +20,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="ApiModels\" />
<Folder Include="Migrations\" /> <Folder Include="Migrations\" />
</ItemGroup> </ItemGroup>

View file

@ -15,12 +15,12 @@ public class UserProfileService(ApplicationDbContext context)
return userProfile.IsAdmin; return userProfile.IsAdmin;
} }
public DbUserProfile ChangeUserAdminStatus(string userId, bool changeStatusTo) public async Task<DbUserProfile> ChangeUserAdminStatusAsync(string userId, bool changeStatusTo)
{ {
var userProfile = GetUserProfileById(userId); var userProfile = GetUserProfileById(userId);
userProfile.IsAdmin = changeStatusTo; userProfile.IsAdmin = changeStatusTo;
_context.UserProfiles.Update(userProfile); _context.UserProfiles.Update(userProfile);
_context.SaveChanges(); await _context.SaveChangesAsync();
return userProfile; return userProfile;
} }
@ -29,6 +29,6 @@ public class UserProfileService(ApplicationDbContext context)
{ {
var userProfile = _context.UserProfiles.FirstOrDefault(x => x.Id.ToString() == userId); 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();
} }
} }