/// <summary> /// It add Candidate /// </summary> /// <param name="addCandidate">Candidate Name and Constituency Id and PartyId</param> /// <returns>AddCandidateResponseModel</returns> public AddCandidateResponseModel AddCandidate(AddCandidateRequestModel addCandidate) { try { if (addCandidate == null) { return(null); } else { return(_candidateRepository.AddCandidate(addCandidate)); } } catch (Exception e) { throw new Exception(e.Message); } }
public IActionResult AddCandidate(AddCandidateRequestModel addCandidate) { 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") { AddCandidateResponseModel candidateResponse = _candidateBusiness.AddCandidate(addCandidate); if (candidateResponse != null) { if (candidateResponse.ErrorResponse.ErrorStatus) { message = candidateResponse.ErrorResponse.Message; return(Ok(new { status, message })); } else { status = true; message = "Candidate Has Been Successfully Added to City."; CandidateAddResponseModel data = candidateResponse.CandidateAdd; return(Ok(new { status, message, data })); } } message = "Unable to Add Candidate."; return(Ok(new { status, message })); } } message = "Invalid Token"; return(BadRequest(new { status, message })); } catch (Exception e) { return(BadRequest(new { e.Message })); } }
/// <summary> /// It Add Candidate /// </summary> /// <param name="addCandidate">Candidate Name and ConstituencyId and PartyId</param> /// <returns>Add Candidate Response Model</returns> public AddCandidateResponseModel AddCandidate(AddCandidateRequestModel addCandidate) { try { SqlDataReader reader; int statusCode, CandidatePresent; string errorMsg = ""; bool errorFlag = false; AddCandidateResponseModel candidateResponse = null; using (SqlConnection connection = new SqlConnection(sqlConnection)) { SqlCommand sqlCommand = new SqlCommand("spCandidate", connection) { CommandType = System.Data.CommandType.StoredProcedure }; sqlCommand.Parameters.AddWithValue("@CandidateId", -1); sqlCommand.Parameters.AddWithValue("@Name", addCandidate.Name); sqlCommand.Parameters.AddWithValue("@ConstituencyId", addCandidate.ConstituencyId); sqlCommand.Parameters.AddWithValue("@PartyId", addCandidate.PartyId); sqlCommand.Parameters.AddWithValue("@ActionType", "Create"); SqlParameter CandidatePresentParameter = sqlCommand.Parameters.Add("@CandidatePresentCount", 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["@CandidatePresentCount"].Value); statusCode = Convert.ToInt32(sqlCommand.Parameters["@return_Value"].Value); if (CandidatePresent == -1) { errorFlag = true; errorMsg = "This Constituency is Not Present"; } else if (CandidatePresent == -2) { errorFlag = true; errorMsg = "This Party is Not Present"; } else if (CandidatePresent == -3) { errorFlag = true; errorMsg = "A Candidate is already present from this Constituency with the same Party"; } if (errorFlag) { candidateResponse = new AddCandidateResponseModel { ErrorResponse = new ErrorResponseModel { ErrorStatus = errorFlag, Message = errorMsg } }; return(candidateResponse); } if (statusCode == 0) { if (reader.HasRows) { candidateResponse = new AddCandidateResponseModel(); while (reader.Read()) { candidateResponse.CandidateAdd = new CandidateAddResponseModel { 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); } }