Skip to content

Commit

Permalink
Patched different files
Browse files Browse the repository at this point in the history
Patched some NullPointError warnings, mainly using ?? throw new ArgumentNullException to catch possible null values
  • Loading branch information
Nedim-boop committed Nov 11, 2024
1 parent 9db55e5 commit 48010d2
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion GirafAPI/Endpoints/ActivityEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions GirafAPI/Endpoints/LoginEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public static void MapLoginEndpoint(this WebApplication app)

var claims = new List<Claim>
{
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)));
Expand Down
6 changes: 3 additions & 3 deletions GirafAPI/Endpoints/OrganizationEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<OrganizationNameOnlyDTO>();
foreach (var organization in user.Organizations)
{
Expand Down
11 changes: 8 additions & 3 deletions GirafAPI/Endpoints/UserEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ public static RouteGroupBuilder MapUsersEndpoints(this WebApplication app)
group.MapPut("/{id}/change-password", async (string id, UpdateUserPasswordDTO updatePasswordDTO, UserManager<GirafUser> 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")
Expand All @@ -109,7 +112,9 @@ public static RouteGroupBuilder MapUsersEndpoints(this WebApplication app)
group.MapPut("/{id}/change-username", async (string id, UpdateUsernameDTO updateUsernameDTO, UserManager<GirafUser> 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")
Expand All @@ -122,7 +127,7 @@ public static RouteGroupBuilder MapUsersEndpoints(this WebApplication app)
group.MapDelete("/{id}", async (string id, UserManager<GirafUser> 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")
Expand Down
4 changes: 2 additions & 2 deletions GirafAPI/Entities/Users/GirafUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Organization>? Organizations { get; set; }
}
2 changes: 1 addition & 1 deletion GirafAPI/Extensions/ServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static IServiceCollection ConfigureJwt(this IServiceCollection services,
var jwtSettingsSection = configuration.GetSection("JwtSettings");
services.Configure<JwtSettings>(jwtSettingsSection);

var jwtSettings = jwtSettingsSection.Get<JwtSettings>();
var jwtSettings = jwtSettingsSection.Get<JwtSettings>() ?? throw new ArgumentNullException(nameof(JwtSettings));
var key = Encoding.UTF8.GetBytes(jwtSettings.SecretKey);

services.AddAuthentication(options =>
Expand Down
8 changes: 4 additions & 4 deletions GirafAPI/Mapping/UserMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
);
}
}

0 comments on commit 48010d2

Please sign in to comment.