drinkrate/DrinkRateAPI/Controllers/AdminController.cs
martinshoob b2b8d1e076 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.
2025-08-10 16:23:35 +02:00

38 lines
1.3 KiB
C#

using DrinkRateAPI.ApiModels.UserProfile;
using DrinkRateAPI.Services;
using Microsoft.AspNetCore.Mvc;
namespace DrinkRateAPI.Controllers;
[ApiController]
[Route("admin")]
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("adminStatus")]
[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 = await _userProfileService.ChangeUserAdminStatusAsync(request.UserId, request.ChangeStatusTo);
return Ok(changedProfile);
}
}