< Summary

Information
Line coverage
0%
Covered lines: 0
Uncovered lines: 68
Coverable lines: 68
Total lines: 125
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 12
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
GetUserAsync()0%620%
GetAllUsersAsync()100%210%
RegisterUserAsync()0%620%
SwapRolesAsync()0%7280%

File(s)

/home/runner/work/SEP4/SEP4/backend/microservices/autorisering/WebApi/Controllers/UserController.cs

#LineLine coverage
 1using System.ComponentModel.DataAnnotations;
 2using Microsoft.AspNetCore.Authorization;
 3using Microsoft.AspNetCore.Mvc;
 4using Microsoft.IdentityModel.Tokens;
 5using WebApi.Models;
 6using WebApi.Services;
 7using System.Data;
 8
 9[ApiController]
 10[Route("[controller]")]
 11public class UserController : ControllerBase
 12{
 13    private readonly IConfiguration config;
 14    private readonly IUserService _userService;
 15
 016    public UserController(IConfiguration config, IUserService userService)
 017    {
 018        this.config = config;
 019        this._userService = userService;
 020    }
 21
 22    [HttpGet("{username}")]
 23    [Authorize(Policy = "MustBeAdmin")]
 24    public async Task<ActionResult<User>> GetUserAsync(string username)
 025    {
 26        try
 027        {
 028            var user = await _userService.GetUserAsync(username);
 029            return Ok(user);
 30        }
 031        catch (Exception ex)
 032        {
 033            if (ex.InnerException is KeyNotFoundException)
 034            {
 035                return NotFound(ex.InnerException.Message);
 36            }
 037            return StatusCode(StatusCodes.Status500InternalServerError, "Error retrieving data from the database.");
 38        }
 039    }
 40
 41    [HttpGet("/GetAllUsers")]
 42    [Authorize(Policy = "MustBeAdmin")]
 43    public async Task<ActionResult<List<User>>> GetAllUsersAsync()
 044    {
 45        try
 046        {
 047            var users = await _userService.GetAllUsersAsync();
 048            return Ok(users);
 49        }
 050        catch (Exception ex)
 051        {
 052            Console.WriteLine($"An error occurred: {ex.ToString()}");  // Log full exception stack trace
 053            return StatusCode(StatusCodes.Status500InternalServerError, "Error retrieving data from the database.");
 54        }
 055    }
 56
 57    [HttpPost("register")]
 58    public async Task<ActionResult<User>> RegisterUserAsync([FromBody] UserCreationDTO userCreationDTO)
 059    {
 060        if (!ModelState.IsValid)
 061        {
 062            return BadRequest(ModelState);
 63        }
 64
 65        try
 066        {
 067            await _userService.RegisterUserAsync(userCreationDTO);
 068            return Ok();
 69        }
 070        catch (DuplicateNameException ex)
 071        {
 072            return BadRequest(ex.Message);
 73        }
 074        catch (ValidationException ex)
 075        {
 076            return BadRequest(ex.Message);
 77        }
 078        catch (Exception ex)
 079        {
 080            return BadRequest(new { error = "Something went wrong." });
 81        }
 082    }
 83
 84    [HttpPost("swapRoles")]
 85    [Authorize(Policy = "MustBeAdmin")]
 86    public async Task<ActionResult<List<User>>> SwapRolesAsync([FromBody] List<User> users)
 087    {
 088        if (users.IsNullOrEmpty())
 089        {
 090            return BadRequest("No users provided.");
 91        }
 92
 93        try
 094        {
 095            var processedUsers = new List<User>();
 096            foreach (var user in users)
 097            {
 098                if (user.Role == "SuperUser")
 099                {
 0100                    user.Role = "User";
 0101                }
 0102                else if (user.Role == "User")
 0103                {
 0104                    user.Role = "SuperUser";
 0105                }
 106                else
 0107                {
 0108                    return BadRequest("Role must be either User or SuperUser!");
 109                }
 110
 0111                await _userService.UpdateUserAsync(user);
 112
 0113                processedUsers.Add(user);
 0114            }
 115
 0116            return Ok(processedUsers);
 117        }
 0118        catch (Exception ex)
 0119        {
 0120            return StatusCode(StatusCodes.Status500InternalServerError, $"Error processing users: {ex.Message}");
 121        }
 122
 0123    }
 124
 125}