Пример #1
0
        public IActionResult UpdateConstituency(int ConstituencyId, UpdateConstituencyRequestModel updateConstituency)
        {
            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")
                    {
                        UpdateConstituencyResponseModel constituencyResponse = _constituencyBusiness.UpdateConstituency(ConstituencyId, updateConstituency);

                        if (constituencyResponse != null)
                        {
                            if (constituencyResponse.ErrorResponse.ErrorStatus)
                            {
                                message = constituencyResponse.ErrorResponse.Message;
                                return(Ok(new { status, message }));
                            }
                            else
                            {
                                status  = true;
                                message = "Constituency Has Been Successfully Updated.";
                                ConstituencyUpdateResponseModel data = constituencyResponse.ConstituencyUpdate;
                                return(Ok(new { status, message, data }));
                            }
                        }

                        message = "Unable to Update the Constituency 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 Constituency
        /// </summary>
        /// <param name="ConstituencyId">Constituency Id</param>
        /// <param name="updateConstituency">Update Constituency Name and City Id</param>
        /// <returns>Update Constituency Response Model</returns>
        public UpdateConstituencyResponseModel UpdateConstituency(int ConstituencyId, UpdateConstituencyRequestModel updateConstituency)
        {
            try
            {
                SqlDataReader reader;
                int           statusCode, ConstituencyPresent;
                string        errorMsg  = "";
                bool          errorFlag = false;
                UpdateConstituencyResponseModel constituencyResponse = null;

                using (SqlConnection connection = new SqlConnection(sqlConnection))
                {
                    SqlCommand sqlCommand = new SqlCommand("spConstituency", connection)
                    {
                        CommandType = System.Data.CommandType.StoredProcedure
                    };
                    sqlCommand.Parameters.AddWithValue("@ConstituencyId", ConstituencyId);
                    sqlCommand.Parameters.AddWithValue("@Name", updateConstituency.Name);
                    sqlCommand.Parameters.AddWithValue("@CityId", updateConstituency.CityId);
                    sqlCommand.Parameters.AddWithValue("@ActionType", "Update");

                    SqlParameter CityPresentParameter = sqlCommand.Parameters.Add("@ConstituencyNamePresentCount", System.Data.SqlDbType.Int);
                    CityPresentParameter.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();
                    ConstituencyPresent = Convert.ToInt32(sqlCommand.Parameters["@ConstituencyNamePresentCount"].Value);
                    statusCode          = Convert.ToInt32(sqlCommand.Parameters["@return_Value"].Value);

                    if (ConstituencyPresent == -1)
                    {
                        errorFlag = true;
                        errorMsg  = "This City is Not Present";
                    }
                    else if (ConstituencyPresent == -2)
                    {
                        errorFlag = true;
                        errorMsg  = "A City Cannot have Same Constituency Name";
                    }

                    if (errorFlag)
                    {
                        constituencyResponse = new UpdateConstituencyResponseModel
                        {
                            ErrorResponse = new ErrorResponseModel
                            {
                                ErrorStatus = errorFlag,
                                Message     = errorMsg
                            }
                        };

                        return(constituencyResponse);
                    }

                    if (statusCode == 0)
                    {
                        if (reader.HasRows)
                        {
                            constituencyResponse = new UpdateConstituencyResponseModel();

                            while (reader.Read())
                            {
                                constituencyResponse.ConstituencyUpdate = new ConstituencyUpdateResponseModel
                                {
                                    ConstituencyId = Convert.ToInt32(reader[0]),
                                    Name           = reader[1].ToString(),
                                    CityId         = Convert.ToInt32(reader[2]),
                                    CreatedAt      = Convert.ToDateTime(reader[3]),
                                    ModifiedAt     = Convert.ToDateTime(reader[4])
                                };

                                constituencyResponse.ErrorResponse = new ErrorResponseModel()
                                {
                                    ErrorStatus = false
                                };
                            }
                        }
                    }
                }

                return(constituencyResponse);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
 /// <summary>
 /// It update the Constituency
 /// </summary>
 /// <param name="ConstituencyId">Constituency Id</param>
 /// <param name="updateConstituency">Update Constituency Name and City Id</param>
 /// <returns>UpdateConstituencyResponseModel</returns>
 public UpdateConstituencyResponseModel UpdateConstituency(int ConstituencyId, UpdateConstituencyRequestModel updateConstituency)
 {
     try
     {
         if (ConstituencyId <= 0 || updateConstituency == null)
         {
             return(null);
         }
         else
         {
             return(_constituencyRepository.UpdateConstituency(ConstituencyId, updateConstituency));
         }
     }
     catch (Exception e)
     {
         throw new Exception(e.Message);
     }
 }