public IActionResult UpdateCandidate(int CandidateId, UpdateCandidateRequestModel updateCandidate) { try { var admin = HttpContext.User; bool status = false; string message; if (admin.HasClaim(c => c.Type == "TokenType")) { if (admin.Claims.FirstOrDefault(c => c.Type == "TokenType").Value == "Login") { UpdateCandidateResponseModel candidateResponse = _candidateBusiness.UpdateCandidate(CandidateId, updateCandidate); if (candidateResponse != null) { if (candidateResponse.ErrorResponse.ErrorStatus) { message = candidateResponse.ErrorResponse.Message; return(Ok(new { status, message })); } else { status = true; message = "Candidate Has Been Successfully Updated."; CandidateUpdateResponseModel data = candidateResponse.CandidateUpdate; return(Ok(new { status, message, data })); } } message = "Unable to Update the Candidate Details"; return(Ok(new { status, message })); } } message = "Invalid Token"; return(BadRequest(new { status, message })); } catch (Exception e) { return(BadRequest(new { e.Message })); } }
/// <summary> /// It Update the Candidate /// </summary> /// <param name="CandidateId">Candidate Id</param> /// <param name="updateCandidate">Update Candidate Name, ConstituencyId and PartyId</param> /// <returns>Update Candidate Response Model</returns> public UpdateCandidateResponseModel UpdateCandidate(int CandidateId, UpdateCandidateRequestModel updateCandidate) { try { SqlDataReader reader; int statusCode, CandidatePresent; string errorMsg = ""; bool errorFlag = false; UpdateCandidateResponseModel candidateResponse = null; using (SqlConnection connection = new SqlConnection(sqlConnection)) { SqlCommand sqlCommand = new SqlCommand("spCandidate", connection) { CommandType = System.Data.CommandType.StoredProcedure }; sqlCommand.Parameters.AddWithValue("@CandidateId", CandidateId); sqlCommand.Parameters.AddWithValue("@Name", updateCandidate.Name); sqlCommand.Parameters.AddWithValue("@ConstituencyId", -1); sqlCommand.Parameters.AddWithValue("@PartyId", -1); sqlCommand.Parameters.AddWithValue("@ActionType", "Update"); SqlParameter CandidatePresentParameter = sqlCommand.Parameters.Add("@CandidateNamePresentCount", System.Data.SqlDbType.Int); CandidatePresentParameter.Direction = System.Data.ParameterDirection.ReturnValue; SqlParameter cmdExecuteSuccess = sqlCommand.Parameters.Add("@return_value", System.Data.SqlDbType.Int); cmdExecuteSuccess.Direction = System.Data.ParameterDirection.ReturnValue; connection.Open(); reader = sqlCommand.ExecuteReader(); CandidatePresent = Convert.ToInt32(sqlCommand.Parameters["@CandidateNamePresentCount"].Value); statusCode = Convert.ToInt32(sqlCommand.Parameters["@return_Value"].Value); if (CandidatePresent == -1) { errorFlag = true; errorMsg = "This Candidate is Not Present"; } if (errorFlag) { candidateResponse = new UpdateCandidateResponseModel { ErrorResponse = new ErrorResponseModel { ErrorStatus = errorFlag, Message = errorMsg } }; return(candidateResponse); } if (statusCode == 0) { if (reader.HasRows) { candidateResponse = new UpdateCandidateResponseModel(); while (reader.Read()) { candidateResponse.CandidateUpdate = new CandidateUpdateResponseModel { CandidateId = Convert.ToInt32(reader[0]), Name = reader[1].ToString(), ConstituencyId = Convert.ToInt32(reader[2]), PartyId = Convert.ToInt32(reader[3]), CreatedAt = Convert.ToDateTime(reader[4]), ModifiedAt = Convert.ToDateTime(reader[5]) }; candidateResponse.ErrorResponse = new ErrorResponseModel() { ErrorStatus = false }; } } } } return(candidateResponse); } catch (Exception e) { throw new Exception(e.Message); } }