Skip to content

Commit

Permalink
#21: Implement ResetEmail and ResetPassword for ApplicantService
Browse files Browse the repository at this point in the history
  • Loading branch information
choutianxius committed Sep 4, 2024
1 parent 213382e commit 9b0f7a7
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions AggieRent/Services/ApplicantService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class ApplicantService(IApplicantRepository applicantRepository) : IAppli

public Applicant? GetApplicantById(string id)
{
return _applicantRepository.Get(id);
return _applicantRepository.GetVerbose(id);
}

public IEnumerable<Applicant> GetApplicants()
Expand Down Expand Up @@ -85,14 +85,41 @@ public void UpdateApplicant(

public void ResetApplicantEmail(string id, string newEmail)
{
// TODO: Implement ResetApplicantEmail
throw new Exception("Method not implemented yet!");
var applicant =
_applicantRepository.Get(id)
?? throw new ArgumentException("Applicant ID not found");

if (!AuthUtils.ValidateEmail(newEmail))
throw new ArgumentException("Invalid email format");

var normalizedEmail = AuthUtils.NormalizeEmail(newEmail);

if (normalizedEmail.Equals(applicant.Email))
return;

var existingApplicant = _applicantRepository
.GetAll()
.FirstOrDefault(a => a.Email == normalizedEmail);
if (existingApplicant != null)
throw new ArgumentException("Email already in use");

applicant.Email = normalizedEmail;
_applicantRepository.Update(applicant);
}

public void ResetApplicantPassword(string id, string newPassword)
{
// TODO: Implement ResetApplicantPassword
throw new Exception("Method not implemented yet!");
var applicant =
_applicantRepository.Get(id)
?? throw new ArgumentException("Applicant ID not found");

if (!AuthUtils.ValidatePassword(newPassword))
throw new ArgumentException(
"Invalid password! Password must be at least 8 symbols long, with at least 1 lower case character, 1 upper case character, 1 symbol and 1 number"
);

applicant.HashedPassword = BC.HashPassword(newPassword);
_applicantRepository.Update(applicant);
}

public void DeleteApplicant(string id)
Expand Down

0 comments on commit 9b0f7a7

Please sign in to comment.