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:
parent
c0860b05d1
commit
b2b8d1e076
4 changed files with 9 additions and 8 deletions
|
@ -1,4 +1,4 @@
|
|||
namespace DrinkRateAPI.Requests;
|
||||
namespace DrinkRateAPI.ApiModels.UserProfile;
|
||||
|
||||
public class ChangeUserAdminStatusRequest
|
||||
{
|
|
@ -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<AdminController> _logger;
|
||||
|
@ -21,7 +21,7 @@ public class AdminController : ControllerBase
|
|||
}
|
||||
|
||||
[HttpPut]
|
||||
[Route("[action]")]
|
||||
[Route("adminStatus")]
|
||||
[Produces("application/json")]
|
||||
public async Task<IActionResult> 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);
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="ApiModels\" />
|
||||
<Folder Include="Migrations\" />
|
||||
</ItemGroup>
|
||||
|
||||
|
|
|
@ -15,12 +15,12 @@ public class UserProfileService(ApplicationDbContext context)
|
|||
return userProfile.IsAdmin;
|
||||
}
|
||||
|
||||
public DbUserProfile ChangeUserAdminStatus(string userId, bool changeStatusTo)
|
||||
public async Task<DbUserProfile> 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();
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue