public JsonResponse <AuthenticateModel> RegisterNewCandidate(NewRegistrationViewModel newRegistrationModel) { JsonResponse <AuthenticateModel> jsonResponse = new JsonResponse <AuthenticateModel>(); var result = ValidateRegistration(newRegistrationModel); if (result.IsValid) { const int passwordGenerationIndex = 3; string username = newRegistrationModel.phone_number.ToString(); string password = GetPassword(newRegistrationModel.first_name, newRegistrationModel.last_name, newRegistrationModel.phone_number, passwordGenerationIndex); string hashedPassword = _SecurityService.HashUserNameAndPassword(username, password); AuthenticateModel infoModel = new AuthenticateModel(); infoModel.user_name = username; infoModel.password = password; User user = new User(); Address userAddress = new Address(); Address candidateAddress = new Address(); Candidate candidate = new Candidate(); CandidateLanguageMap candidateLanguageMap; MapCandidate(candidate, newRegistrationModel); MapUser(user, newRegistrationModel); MapUserAddress(userAddress, newRegistrationModel); MapCandidateAddress(candidateAddress, newRegistrationModel); user.user_name = username; user.password = hashedPassword; user.role_id = UserRoles.Candidate; user.is_user_login_first_time = true; user.date_of_birth = newRegistrationModel.user_birth_date; using (IDbConnection dbConnection = new NpgsqlConnection(_ConnectionStringService.Value)) { dbConnection.Open(); using (var transaction = dbConnection.BeginTransaction()) { try { user.address_id = (Int32)dbConnection.Insert <Address>(userAddress, transaction); candidate.address_id = (Int32)dbConnection.Insert <Address>(candidateAddress, transaction); candidate.user_id = (Int32)dbConnection.Insert <User>(user, transaction); Int16 candidate_id = (Int16)dbConnection.Insert <Candidate>(candidate, transaction); for (int i = 0; i < newRegistrationModel.language.Length; i++) { candidateLanguageMap = new CandidateLanguageMap(); candidateLanguageMap.candidate_id = candidate_id; candidateLanguageMap.language_id = newRegistrationModel.language[i]; dbConnection.Insert <CandidateLanguageMap>(candidateLanguageMap, transaction); } transaction.Commit(); jsonResponse.Data = infoModel; jsonResponse.IsSuccess = true; jsonResponse.Message = "Registration Successful"; } catch (Exception ex) { if (ex.Message.Contains("user_table_phone_number_unique")) { jsonResponse.Message = "User phone number already exists"; } else { jsonResponse.Message = "Some error occured. Please contact administrator."; } transaction.Rollback(); jsonResponse.IsSuccess = false; } } } } else { jsonResponse.IsSuccess = false; jsonResponse.Message = result.Messages(); } return(jsonResponse); }
public JsonResponse <String> Editcandidate(EditCandidateViewModel editCandidateViewModel) { JsonResponse <String> jsonResponse = new JsonResponse <String>(); var result = ValidateEditCandidate(editCandidateViewModel); if (result.IsValid) { Address candidateAddress = new Address(); Candidate candidate = new Candidate(); CandidateLanguageMap candidateLanguageMap; MapEditCandidate(candidate, editCandidateViewModel); MapEditCandidateAddress(candidateAddress, editCandidateViewModel); using (IDbConnection dbConnection = new NpgsqlConnection(_ConnectionStringService.Value)) { dbConnection.Open(); using (var transaction = dbConnection.BeginTransaction()) { try { dbConnection.Update <Address>(candidateAddress, transaction); dbConnection.Update <Candidate>(candidate, transaction); dbConnection.Query("delete from candidate_language_map where candidate_id = @p0", new { p0 = candidate.id }); for (int i = 0; i < editCandidateViewModel.language.Length; i++) { candidateLanguageMap = new CandidateLanguageMap(); candidateLanguageMap.candidate_id = candidate.id; candidateLanguageMap.language_id = editCandidateViewModel.language[i]; dbConnection.Insert <CandidateLanguageMap>(candidateLanguageMap, transaction); } transaction.Commit(); jsonResponse.Data = "Updated successfully"; jsonResponse.IsSuccess = true; jsonResponse.Message = "Candidate Updated Successful"; } catch (Exception ex) { if (ex.Message.Contains("user_table_phone_number_unique")) { jsonResponse.Message = "User phone number already exists"; } else { jsonResponse.Message = "Some error occured. Please contact administrator."; } transaction.Rollback(); jsonResponse.IsSuccess = false; } } } } else { jsonResponse.IsSuccess = false; jsonResponse.Message = result.Messages(); } return(jsonResponse); }