Skip to content

Commit

Permalink
refactoring of helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
KristianS93 committed Nov 28, 2023
1 parent 00e95ed commit feafe27
Show file tree
Hide file tree
Showing 10 changed files with 364 additions and 225 deletions.
32 changes: 1 addition & 31 deletions GirafAPI/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ public class AccountController : Controller
private readonly SignInManager<GirafUser> _signInManager;
private readonly IUserService _userService;
private readonly IOptions<JwtConfig> _configuration;
private readonly IAuthenticationService _authentication;
private readonly IGirafUserRepository _userRepository;
private readonly IDepartmentRepository _departmentRepository;
private readonly IGirafRoleRepository _girafRoleRepository;
private readonly ISettingRepository _settingRepository;

/// <summary>
Expand All @@ -57,15 +55,13 @@ public AccountController(
IOptions<JwtConfig> configuration,
IGirafUserRepository userRepository,
IDepartmentRepository departmentRepository,
IGirafRoleRepository girafRoleRepository,
ISettingRepository settingRepository)
{
_signInManager = signInManager;
_userService = userService;
_configuration = configuration;
_userRepository = userRepository;
_departmentRepository = departmentRepository;
_girafRoleRepository = girafRoleRepository;
_settingRepository = settingRepository;
}

Expand Down Expand Up @@ -137,7 +133,7 @@ public async Task<ActionResult> Register([FromBody] RegisterDTO model)
if (String.IsNullOrEmpty(model.Username) || String.IsNullOrEmpty(model.Password) || String.IsNullOrEmpty(model.DisplayName))
return BadRequest(new ErrorResponse(ErrorCode.InvalidCredentials, "Missing username, password or displayName"));

var UserRoleStr = GirafRoleFromEnumToString(model.Role);
var UserRoleStr = _userService.GirafRoleFromEnumToString(model.Role);
if (UserRoleStr == null)
return BadRequest(new ErrorResponse(ErrorCode.RoleNotFound, "The provided role is not valid"));

Expand Down Expand Up @@ -354,32 +350,6 @@ private async Task<string> GenerateJwtToken(GirafUser user)

return new JwtSecurityTokenHandler().WriteToken(token);
}

/// <summary>
/// Simple helper method for converting a role as enum to a role as string
/// </summary>
/// <returns>The role as a string</returns>
/// <param name="role">A given role as enum that should be converted to a string</param>
private string GirafRoleFromEnumToString(GirafRoles role)
{
switch (role)
{
case GirafRoles.Citizen:
return GirafRole.Citizen;
case GirafRoles.Guardian:
return GirafRole.Guardian;
case GirafRoles.Trustee:
return GirafRole.Trustee;
case GirafRoles.Department:
return GirafRole.Department;
case GirafRoles.SuperUser:
return GirafRole.SuperUser;
default:
return null;
}
}


}
}

Loading

0 comments on commit feafe27

Please sign in to comment.