Skip to content

Commit 48c644a

Browse files
committed
Add parameter validation for pageNumber and pageSize
1 parent 23837bc commit 48c644a

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/Core/Services/RoleService.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ public async Task<int> GetTotalRolesCountAsync()
4242

4343
public async Task<IEnumerable<RoleResponseDto>> GetAllRolesAsync(int pageNumber, int pageSize)
4444
{
45+
if (pageNumber <= 0 || pageSize <= 0)
46+
{
47+
throw new BadRequestException("pageNumber and pageSize must be greater than zero.");
48+
}
4549
IEnumerable<Role> roles = await _roleRepository.GetAllRolesAsync(pageNumber, pageSize).ConfigureAwait(false);
4650
if (!roles.Any())
4751
{

src/Core/Services/UserService.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ public UserService(ITokenService tokenService, IRoleRepository roleRepository, I
2424

2525
public async Task<string?> Login(string email, string password)
2626
{
27+
if (string.IsNullOrWhiteSpace(email) || string.IsNullOrWhiteSpace(password))
28+
{
29+
throw new BadRequestException("Email and password are required.");
30+
}
31+
2732
User? user = await _userRepository.GetUserByEmailAsync(email).ConfigureAwait(false);
2833
if (user == null || !VerifyPassword(password, user.PasswordHash))
2934
{
@@ -66,6 +71,10 @@ public async Task<int> GetTotalUsersCountAsync()
6671

6772
public async Task<IEnumerable<UserResponseDto>> GetAllUsersAsync(int pageNumber, int pageSize)
6873
{
74+
if (pageNumber <= 0 || pageSize <= 0)
75+
{
76+
throw new BadRequestException("pageNumber and pageSize must be greater than zero.");
77+
}
6978
IEnumerable<User> users = await _userRepository.GetAllUsersAsync(pageNumber, pageSize).ConfigureAwait(false);
7079
if (!users.Any())
7180
{
@@ -114,6 +123,12 @@ public async Task<IEnumerable<UserResponseDto>> GetAllUsersAsync(int pageNumber,
114123
throw new AlreadyExistsException($"User with email {userDto.Email} already exists.");
115124
}
116125

126+
Role? role = await _roleRepository.GetRoleByIdAsync(userDto.RoleId).ConfigureAwait(false);
127+
if (role == null)
128+
{
129+
throw new NotFoundException($"Role with ID {userDto.RoleId} not found.");
130+
}
131+
117132
_mapper.Map(userDto, user);
118133
user.SetRole(userDto.RoleId);
119134
User? updatedUser = await _userRepository.UpdateUserAsync(user).ConfigureAwait(false);

0 commit comments

Comments
 (0)