| | 1 | | using System.ComponentModel.DataAnnotations; |
| | 2 | | using Microsoft.AspNetCore.Authorization; |
| | 3 | | using Microsoft.AspNetCore.Mvc; |
| | 4 | | using Microsoft.IdentityModel.Tokens; |
| | 5 | | using WebApi.Models; |
| | 6 | | using WebApi.Services; |
| | 7 | | using System.Data; |
| | 8 | |
|
| | 9 | | [ApiController] |
| | 10 | | [Route("[controller]")] |
| | 11 | | public class UserController : ControllerBase |
| | 12 | | { |
| | 13 | | private readonly IConfiguration config; |
| | 14 | | private readonly IUserService _userService; |
| | 15 | |
|
| 0 | 16 | | public UserController(IConfiguration config, IUserService userService) |
| 0 | 17 | | { |
| 0 | 18 | | this.config = config; |
| 0 | 19 | | this._userService = userService; |
| 0 | 20 | | } |
| | 21 | |
|
| | 22 | | [HttpGet("{username}")] |
| | 23 | | [Authorize(Policy = "MustBeAdmin")] |
| | 24 | | public async Task<ActionResult<User>> GetUserAsync(string username) |
| 0 | 25 | | { |
| | 26 | | try |
| 0 | 27 | | { |
| 0 | 28 | | var user = await _userService.GetUserAsync(username); |
| 0 | 29 | | return Ok(user); |
| | 30 | | } |
| 0 | 31 | | catch (Exception ex) |
| 0 | 32 | | { |
| 0 | 33 | | if (ex.InnerException is KeyNotFoundException) |
| 0 | 34 | | { |
| 0 | 35 | | return NotFound(ex.InnerException.Message); |
| | 36 | | } |
| 0 | 37 | | return StatusCode(StatusCodes.Status500InternalServerError, "Error retrieving data from the database."); |
| | 38 | | } |
| 0 | 39 | | } |
| | 40 | |
|
| | 41 | | [HttpGet("/GetAllUsers")] |
| | 42 | | [Authorize(Policy = "MustBeAdmin")] |
| | 43 | | public async Task<ActionResult<List<User>>> GetAllUsersAsync() |
| 0 | 44 | | { |
| | 45 | | try |
| 0 | 46 | | { |
| 0 | 47 | | var users = await _userService.GetAllUsersAsync(); |
| 0 | 48 | | return Ok(users); |
| | 49 | | } |
| 0 | 50 | | catch (Exception ex) |
| 0 | 51 | | { |
| 0 | 52 | | Console.WriteLine($"An error occurred: {ex.ToString()}"); // Log full exception stack trace |
| 0 | 53 | | return StatusCode(StatusCodes.Status500InternalServerError, "Error retrieving data from the database."); |
| | 54 | | } |
| 0 | 55 | | } |
| | 56 | |
|
| | 57 | | [HttpPost("register")] |
| | 58 | | public async Task<ActionResult<User>> RegisterUserAsync([FromBody] UserCreationDTO userCreationDTO) |
| 0 | 59 | | { |
| 0 | 60 | | if (!ModelState.IsValid) |
| 0 | 61 | | { |
| 0 | 62 | | return BadRequest(ModelState); |
| | 63 | | } |
| | 64 | |
|
| | 65 | | try |
| 0 | 66 | | { |
| 0 | 67 | | await _userService.RegisterUserAsync(userCreationDTO); |
| 0 | 68 | | return Ok(); |
| | 69 | | } |
| 0 | 70 | | catch (DuplicateNameException ex) |
| 0 | 71 | | { |
| 0 | 72 | | return BadRequest(ex.Message); |
| | 73 | | } |
| 0 | 74 | | catch (ValidationException ex) |
| 0 | 75 | | { |
| 0 | 76 | | return BadRequest(ex.Message); |
| | 77 | | } |
| 0 | 78 | | catch (Exception ex) |
| 0 | 79 | | { |
| 0 | 80 | | return BadRequest(new { error = "Something went wrong." }); |
| | 81 | | } |
| 0 | 82 | | } |
| | 83 | |
|
| | 84 | | [HttpPost("swapRoles")] |
| | 85 | | [Authorize(Policy = "MustBeAdmin")] |
| | 86 | | public async Task<ActionResult<List<User>>> SwapRolesAsync([FromBody] List<User> users) |
| 0 | 87 | | { |
| 0 | 88 | | if (users.IsNullOrEmpty()) |
| 0 | 89 | | { |
| 0 | 90 | | return BadRequest("No users provided."); |
| | 91 | | } |
| | 92 | |
|
| | 93 | | try |
| 0 | 94 | | { |
| 0 | 95 | | var processedUsers = new List<User>(); |
| 0 | 96 | | foreach (var user in users) |
| 0 | 97 | | { |
| 0 | 98 | | if (user.Role == "SuperUser") |
| 0 | 99 | | { |
| 0 | 100 | | user.Role = "User"; |
| 0 | 101 | | } |
| 0 | 102 | | else if (user.Role == "User") |
| 0 | 103 | | { |
| 0 | 104 | | user.Role = "SuperUser"; |
| 0 | 105 | | } |
| | 106 | | else |
| 0 | 107 | | { |
| 0 | 108 | | return BadRequest("Role must be either User or SuperUser!"); |
| | 109 | | } |
| | 110 | |
|
| 0 | 111 | | await _userService.UpdateUserAsync(user); |
| | 112 | |
|
| 0 | 113 | | processedUsers.Add(user); |
| 0 | 114 | | } |
| | 115 | |
|
| 0 | 116 | | return Ok(processedUsers); |
| | 117 | | } |
| 0 | 118 | | catch (Exception ex) |
| 0 | 119 | | { |
| 0 | 120 | | return StatusCode(StatusCodes.Status500InternalServerError, $"Error processing users: {ex.Message}"); |
| | 121 | | } |
| | 122 | |
|
| 0 | 123 | | } |
| | 124 | |
|
| | 125 | | } |