Spaces:
Sleeping
Sleeping
| using LibraryManagement.Shared.Models; | |
| using Microsoft.AspNetCore.Authorization; | |
| using Microsoft.AspNetCore.Mvc; | |
| namespace Backend.Features.Users | |
| { | |
| [] | |
| [] | |
| public class UserController : ControllerBase | |
| { | |
| private readonly IUserService _userService; | |
| public UserController(IUserService userService) | |
| { | |
| _userService = userService; | |
| } | |
| [] | |
| [] | |
| public async Task<ActionResult<IEnumerable<UserDto>>> GetUsers() | |
| { | |
| var users = await _userService.GetAllUsersAsync(); | |
| return Ok(users); | |
| } | |
| [] | |
| [] | |
| public async Task<ActionResult<UserDto>> GetUser(Guid id) | |
| { | |
| var user = await _userService.GetUserByIdAsync(id); | |
| if (user == null) return NotFound(); | |
| return Ok(user); | |
| } | |
| [] | |
| [] | |
| public async Task<ActionResult<UserDto>> CreateUser([FromBody] UserCreateRequest request) | |
| { | |
| try | |
| { | |
| var response = await _userService.CreateUserAsync(request); | |
| return CreatedAtAction(nameof(GetUser), new { id = response.Id }, response); | |
| } | |
| catch (Exception ex) | |
| { | |
| return BadRequest(new { message = ex.Message }); | |
| } | |
| } | |
| [] | |
| [] | |
| public async Task<ActionResult<UserDto>> UpdateUser(Guid id, [FromBody] UserUpdateRequest request) | |
| { | |
| var updatedUser = await _userService.UpdateUserAsync(id, request); | |
| if (updatedUser == null) return NotFound(); | |
| return Ok(updatedUser); | |
| } | |
| [] | |
| [] | |
| public async Task<IActionResult> UpdateUserRole(Guid id, [FromBody] UserRoleUpdateRequest request) | |
| { | |
| var success = await _userService.UpdateUserRoleAsync(id, request.Role); | |
| if (!success) return NotFound(); | |
| return NoContent(); | |
| } | |
| [] | |
| [] | |
| public async Task<IActionResult> DeleteUser(Guid id) | |
| { | |
| var success = await _userService.DeleteUserAsync(id); | |
| if (!success) return NotFound(); | |
| return NoContent(); | |
| } | |
| [] | |
| [] | |
| public async Task<IActionResult> UpdateFcmToken([FromBody] FcmTokenUpdateRequest request) | |
| { | |
| var userIdStr = User.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier)?.Value; | |
| if (string.IsNullOrEmpty(userIdStr) || !Guid.TryParse(userIdStr, out Guid userId)) | |
| { | |
| return Unauthorized(); | |
| } | |
| var success = await _userService.UpdateFcmTokenAsync(userId, request.FcmToken); | |
| if (!success) return NotFound(); | |
| return Ok(new { message = "FCM token updated successfully" }); | |
| } | |
| } | |
| public class FcmTokenUpdateRequest | |
| { | |
| public string FcmToken { get; set; } = null!; | |
| } | |
| } | |