version-0.1 (#1)
Squash and merge of version-0.1 Co-authored-by: James Wampler <james@wamp.dev> Co-committed-by: James Wampler <james@wamp.dev>
This commit was merged in pull request #1.
This commit is contained in:
67
src/api/MicCheck.Api/Users/UsersController.cs
Executable file
67
src/api/MicCheck.Api/Users/UsersController.cs
Executable file
@@ -0,0 +1,67 @@
|
||||
using MicCheck.Api.Common.Security.Authorization;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.RateLimiting;
|
||||
|
||||
namespace MicCheck.Api.Users;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/v1/user")]
|
||||
[Authorize(Policy = AuthorizationPolicies.AdminApiAccess)]
|
||||
[EnableRateLimiting("AdminApi")]
|
||||
public class UsersController(UserService userService) : ControllerBase
|
||||
{
|
||||
[HttpGet("{id:int}")]
|
||||
public async Task<ActionResult<UserResponse>> GetById(int id, CancellationToken ct)
|
||||
{
|
||||
var user = await userService.FindByIdAsync(id, ct);
|
||||
return user is null ? NotFound() : Ok(UserResponse.From(user));
|
||||
}
|
||||
|
||||
[HttpGet("me")]
|
||||
public async Task<ActionResult<UserResponse>> GetMe(CancellationToken ct)
|
||||
{
|
||||
var userId = GetCurrentUserId();
|
||||
if (userId is null) return Unauthorized();
|
||||
|
||||
var user = await userService.FindByIdAsync(userId.Value, ct);
|
||||
return user is null ? NotFound() : Ok(UserResponse.From(user));
|
||||
}
|
||||
|
||||
[HttpPut("me")]
|
||||
public async Task<ActionResult<UserResponse>> UpdateMe(UpdateProfileRequest request, CancellationToken ct)
|
||||
{
|
||||
var userId = GetCurrentUserId();
|
||||
if (userId is null) return Unauthorized();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(request.FirstName) ||
|
||||
string.IsNullOrWhiteSpace(request.LastName) ||
|
||||
string.IsNullOrWhiteSpace(request.Email))
|
||||
return BadRequest("First name, last name, and email are required.");
|
||||
|
||||
var (user, emailConflict) = await userService.UpdateProfileAsync(
|
||||
userId.Value, request.FirstName.Trim(), request.LastName.Trim(), request.Email.Trim(), ct);
|
||||
|
||||
if (emailConflict) return Conflict("Email is already in use.");
|
||||
return user is null ? NotFound() : Ok(UserResponse.From(user));
|
||||
}
|
||||
|
||||
[HttpPost("me/change-password")]
|
||||
public async Task<IActionResult> ChangePassword(ChangePasswordRequest request, CancellationToken ct)
|
||||
{
|
||||
var userId = GetCurrentUserId();
|
||||
if (userId is null) return Unauthorized();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(request.CurrentPassword) || string.IsNullOrWhiteSpace(request.NewPassword))
|
||||
return BadRequest("Current and new passwords are required.");
|
||||
|
||||
var success = await userService.ChangePasswordAsync(userId.Value, request.CurrentPassword, request.NewPassword, ct);
|
||||
return success ? NoContent() : BadRequest("Current password is incorrect.");
|
||||
}
|
||||
|
||||
private int? GetCurrentUserId()
|
||||
{
|
||||
var claim = User.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier)?.Value;
|
||||
return int.TryParse(claim, out var id) ? id : null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user