Implement global exception handling to provide consistent error responses. Registes custom exceptions with corresponding HTTP status codes and descriptions.
39 lines
No EOL
1.3 KiB
C#
39 lines
No EOL
1.3 KiB
C#
using System.Security.Claims;
|
|
using DrinkRateAPI.ApiModels.UserProfile;
|
|
using DrinkRateAPI.Services;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace DrinkRateAPI.Controllers;
|
|
|
|
[ApiController]
|
|
[Route("userProfile")]
|
|
public class UserProfileController : ControllerBase
|
|
{
|
|
private readonly ILogger<UserProfileController> _logger;
|
|
private readonly UserProfileService _userProfileService;
|
|
|
|
public UserProfileController(ILogger<UserProfileController> logger, UserProfileService userProfileService)
|
|
{
|
|
_logger = logger;
|
|
_userProfileService = userProfileService;
|
|
}
|
|
|
|
[HttpPut]
|
|
public UserProfileGet PutUserProfile([FromBody] UserProfilePut userProfile)
|
|
{
|
|
throw new ApplicationException();
|
|
var x = User.FindFirst(ClaimTypes.NameIdentifier)?.Value; //HttpContext.User.Identities.First();
|
|
return new();
|
|
}
|
|
|
|
[HttpPut("{userId}/adminStatus")]
|
|
[Authorize(Policy = "AdminOnly")]
|
|
[Produces("application/json")]
|
|
public async Task<IActionResult> PutUserAdminStatus(string userId, [FromBody] UserProfileAdminStatusPut body)
|
|
{
|
|
var changedProfile = await _userProfileService.PutUserProfileAdminStatusAsync(userId, body.ChangeStatusTo);
|
|
|
|
return Ok(changedProfile);
|
|
}
|
|
} |