Skip to content

Commit

Permalink
Merge pull request #11 from AcademiaY4/feat/user-management
Browse files Browse the repository at this point in the history
feat:addNewUserDone
  • Loading branch information
moshdev2213 authored Oct 6, 2024
2 parents 4dd3bd2 + 87332fb commit 23a6b19
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 12 deletions.
7 changes: 7 additions & 0 deletions Models/Dto/UserDto/CreateUserDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,12 @@ public class CreateUserDto
public string Email { get; set; }
public string Password { get; set; }
public string Role { get; set; }
public string Telephone { get; set; }
public int Age { get; set; }
public string District { get; set; }
public string Province { get; set; }
public string City { get; set; }
public string ZipCode { get; set; }
public string Company { get; set; }

}
6 changes: 6 additions & 0 deletions Models/Dto/UserDto/CreateUserResDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@ public class CreateUserResDto
// Can be 'PENDING', 'ACTIVE', or 'DEACTIVATED'
public string Status { get; set; }
public string DateCreated { get; set; }
public string Telephone { get; set; }
public int Age { get; set; }
public string District { get; set; }
public string Province { get; set; }
public string City { get; set; }

}
64 changes: 52 additions & 12 deletions Models/Validation/UserValidation/CreateUserValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,69 @@ namespace apekade.Models.Validation;

public class CreateUserValidator : AbstractValidator<CreateUserDto>
{
public CreateUserValidator(){
public CreateUserValidator()
{
RuleFor(x => x.FirstName)
.NotEmpty()
.WithMessage("First name is required.");

RuleFor(x => x.LastName)
.NotEmpty()
.WithMessage("First name is required.");
.WithMessage("Last name is required.");

RuleFor(x => x.Email)
RuleFor(x => x.Role)
.NotEmpty()
.EmailAddress()
.WithMessage("A valid email is required.");
.WithMessage("Role should not be empty.")
.Must(IsValidRole)
.WithMessage("Role is not valid.");

RuleFor(x => x.Password)
RuleFor(x => x.Telephone)
.NotEmpty()
.MinimumLength(6)
.WithMessage("Password must be at least 6 characters.");
.WithMessage("Telephone is required.")
.Matches(@"^\d{9}$")
.WithMessage("Telephone must be exactly 9 digits.");

RuleFor(x => x.Role)
RuleFor(x => x.Age)
.NotEmpty()
.WithMessage("role should not be empty")
.Must(IsValidRole)
.WithMessage("role not valid .");
.WithMessage("Age is required.")
.InclusiveBetween(14, 100)
.WithMessage("Age must be between 14 and 100.");

RuleFor(x => x.Province)
.NotEmpty()
.WithMessage("Province is required.")
.Must(IsValidProvince)
.WithMessage("Province is not valid.");

RuleFor(x => x.District)
.NotEmpty()
.WithMessage("District is required.")
.Must(IsValidDistrict)
.WithMessage("District is not valid.");

RuleFor(x => x.City)
.NotEmpty()
.WithMessage("City is required.");

RuleFor(x => x.ZipCode)
.NotEmpty()
.WithMessage("ZipCode is required.");

RuleFor(x => x.Company)
.MaximumLength(100) // Limit length to a reasonable size
.WithMessage("Company name cannot exceed 100 characters.")
.When(x => !string.IsNullOrEmpty(x.Company));
}
private bool IsValidRole(string role)
{
return Enum.TryParse(typeof(Role), role, true, out _);
}
private bool IsValidProvince(string province)
{
return Enum.TryParse(typeof(Province), province, true, out _);
}
private bool IsValidDistrict(string district)
{
return Enum.TryParse(typeof(District), district, true, out _);
}
}
1 change: 1 addition & 0 deletions Services/Impl/AdminService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public async Task<ApiRes> CreateUser(CreateUserDto createUserDto)
var newUser = _mapper.Map<User>(createUserDto);
// auto activate account when admin create users
newUser.IsApproved = true;
newUser.Status= Models.Enums.Status.ACTIVE;

await _adminRepository.CreateNewUser(newUser);
return new ApiRes(201, true, "User created successfully", new { });
Expand Down

0 comments on commit 23a6b19

Please sign in to comment.