Adds functionality to allow administrators to modify the admin status of other users. Introduces an endpoint for changing user admin status, accessible only to existing administrators. This change includes necessary services and request models to handle the logic.
38 lines
1.2 KiB
C#
38 lines
1.2 KiB
C#
using DrinkRateAPI.Requests;
|
|
using DrinkRateAPI.Services;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace DrinkRateAPI.Controllers;
|
|
|
|
[ApiController]
|
|
[Route("[controller]")]
|
|
public class AdminController : ControllerBase
|
|
{
|
|
private readonly ILogger<AdminController> _logger;
|
|
private readonly ApplicationUserService _applicationUserService;
|
|
private readonly UserProfileService _userProfileService;
|
|
|
|
public AdminController(ILogger<AdminController> logger, ApplicationUserService applicationUserService,
|
|
UserProfileService userProfileService)
|
|
{
|
|
_logger = logger;
|
|
_applicationUserService = applicationUserService;
|
|
_userProfileService = userProfileService;
|
|
}
|
|
|
|
[HttpPut]
|
|
[Route("[action]")]
|
|
[Produces("application/json")]
|
|
public async Task<IActionResult> PutUserAdminStatus([FromBody] ChangeUserAdminStatusRequest request)
|
|
{
|
|
var userProfile = await _applicationUserService.UserProfileByApplicationUserAsync(User);
|
|
if (!_userProfileService.IsUserProfileAdmin(userProfile))
|
|
{
|
|
return Unauthorized();
|
|
}
|
|
|
|
var changedProfile = _userProfileService.ChangeUserAdminStatus(request.UserId, request.ChangeStatusTo);
|
|
|
|
return Ok(changedProfile);
|
|
}
|
|
}
|