diff --git a/GirafAPI/Endpoints/ActivityEndpoints.cs b/GirafAPI/Endpoints/ActivityEndpoints.cs index 0ef19b8..102ee18 100644 --- a/GirafAPI/Endpoints/ActivityEndpoints.cs +++ b/GirafAPI/Endpoints/ActivityEndpoints.cs @@ -26,7 +26,7 @@ public static RouteGroupBuilder MapActivityEndpoints(this WebApplication app) return Results.Ok(activities); } - catch (Exception ex) + catch (Exception) { return Results.Problem("An error occurred while retrieving activities.", statusCode: StatusCodes.Status500InternalServerError); } diff --git a/GirafAPI/Endpoints/LoginEndpoints.cs b/GirafAPI/Endpoints/LoginEndpoints.cs index 67db6b3..ef0952e 100644 --- a/GirafAPI/Endpoints/LoginEndpoints.cs +++ b/GirafAPI/Endpoints/LoginEndpoints.cs @@ -32,8 +32,8 @@ public static void MapLoginEndpoint(this WebApplication app) var claims = new List { - new Claim(ClaimTypes.NameIdentifier, user.Id), - new Claim(ClaimTypes.Name, user.UserName) + new Claim(ClaimTypes.NameIdentifier, user.Id ?? throw new ArgumentNullException(nameof(user.Id))), + new Claim(ClaimTypes.Name, user.UserName ?? throw new ArgumentNullException(nameof(user.UserName))) }; claims.AddRange(roles.Select(role => new Claim(ClaimTypes.Role, role))); diff --git a/GirafAPI/Endpoints/OrganizationEndpoints.cs b/GirafAPI/Endpoints/OrganizationEndpoints.cs index 92bf03a..7106494 100644 --- a/GirafAPI/Endpoints/OrganizationEndpoints.cs +++ b/GirafAPI/Endpoints/OrganizationEndpoints.cs @@ -25,14 +25,14 @@ public static RouteGroupBuilder MapOrganizationEndpoints(this WebApplication app return Results.BadRequest("Invalid user id."); } - await dbContext.Entry(user) - .Collection(u => u.Organizations).LoadAsync(); - if (user.Organizations is null) { return Results.NotFound(); } + await dbContext.Entry(user) + .Collection(u => u.Organizations!).LoadAsync(); + var organizations = new List(); foreach (var organization in user.Organizations) { diff --git a/GirafAPI/Endpoints/UserEndpoints.cs b/GirafAPI/Endpoints/UserEndpoints.cs index 1289cb2..7d20e76 100644 --- a/GirafAPI/Endpoints/UserEndpoints.cs +++ b/GirafAPI/Endpoints/UserEndpoints.cs @@ -95,7 +95,10 @@ public static RouteGroupBuilder MapUsersEndpoints(this WebApplication app) group.MapPut("/{id}/change-password", async (string id, UpdateUserPasswordDTO updatePasswordDTO, UserManager userManager) => { var user = await userManager.FindByIdAsync(id); - var result = await userManager.ChangePasswordAsync(user, updatePasswordDTO.oldPassword, updatePasswordDTO.newPassword); + var result = await userManager.ChangePasswordAsync( + user ?? throw new ArgumentNullException(nameof(user)), + updatePasswordDTO.oldPassword, + updatePasswordDTO.newPassword); return result.Succeeded ? Results.Ok() : Results.BadRequest(result.Errors); }) .WithName("ChangeUserPassword") @@ -109,7 +112,9 @@ public static RouteGroupBuilder MapUsersEndpoints(this WebApplication app) group.MapPut("/{id}/change-username", async (string id, UpdateUsernameDTO updateUsernameDTO, UserManager userManager) => { var user = await userManager.FindByIdAsync(id); - var result = await userManager.SetUserNameAsync(user, updateUsernameDTO.Username); + var result = await userManager.SetUserNameAsync( + user ?? throw new ArgumentNullException(nameof(user)), + updateUsernameDTO.Username); return result.Succeeded ? Results.Ok() : Results.BadRequest(result.Errors); }) .WithName("ChangeUsername") @@ -122,7 +127,7 @@ public static RouteGroupBuilder MapUsersEndpoints(this WebApplication app) group.MapDelete("/{id}", async (string id, UserManager userManager) => { var user = await userManager.FindByIdAsync(id); - var result = await userManager.DeleteAsync(user); + var result = await userManager.DeleteAsync(user ?? throw new ArgumentNullException(nameof(user))); return result.Succeeded ? Results.NoContent() : Results.BadRequest(result.Errors); }) .WithName("DeleteUser") diff --git a/GirafAPI/Entities/Users/GirafUser.cs b/GirafAPI/Entities/Users/GirafUser.cs index 987f1a0..40ca1d8 100644 --- a/GirafAPI/Entities/Users/GirafUser.cs +++ b/GirafAPI/Entities/Users/GirafUser.cs @@ -7,9 +7,9 @@ namespace GirafAPI.Entities.Users; // Base class for users that stores data common between user types. public class GirafUser : IdentityUser { - [StringLength(20)] public required string FirstName { get; set; } + [StringLength(20)] [Required] public required string FirstName { get; set; } - [StringLength(50)] public required string LastName { get; set; } + [StringLength(50)] [Required] public required string LastName { get; set; } public ICollection? Organizations { get; set; } } \ No newline at end of file diff --git a/GirafAPI/Extensions/ServiceExtensions.cs b/GirafAPI/Extensions/ServiceExtensions.cs index 93d4c55..7ae5d3f 100644 --- a/GirafAPI/Extensions/ServiceExtensions.cs +++ b/GirafAPI/Extensions/ServiceExtensions.cs @@ -50,7 +50,7 @@ public static IServiceCollection ConfigureJwt(this IServiceCollection services, var jwtSettingsSection = configuration.GetSection("JwtSettings"); services.Configure(jwtSettingsSection); - var jwtSettings = jwtSettingsSection.Get(); + var jwtSettings = jwtSettingsSection.Get() ?? throw new ArgumentNullException(nameof(JwtSettings)); var key = Encoding.UTF8.GetBytes(jwtSettings.SecretKey); services.AddAuthentication(options => diff --git a/GirafAPI/Mapping/UserMapping.cs b/GirafAPI/Mapping/UserMapping.cs index c60d0d2..447d2fd 100644 --- a/GirafAPI/Mapping/UserMapping.cs +++ b/GirafAPI/Mapping/UserMapping.cs @@ -34,10 +34,10 @@ public static GirafUser ToEntity(this UpdateUserDTO user) public static UserDTO ToDTO(this GirafUser user) { return new UserDTO( - user.Id, - user.Email, - user.FirstName, - user.LastName + user.Id ?? throw new ArgumentNullException(nameof(user.Id)), + user.Email ?? throw new ArgumentNullException(nameof(user.Email)), + user.FirstName ?? throw new ArgumentNullException(nameof(user.FirstName)), + user.LastName ?? throw new ArgumentNullException(nameof(user.LastName)) ); } }