public IActionResult UpdateCity(int CityId, UpdateCityRequestModel updateCity)
        {
            try
            {
                bool   status = false;
                string message;

                UpdateCityResponseModel cityResponse = _cityBusiness.UpdateCity(CityId, updateCity);

                if (cityResponse != null)
                {
                    if (cityResponse.ErrorResponse.ErrorStatus)
                    {
                        message = cityResponse.ErrorResponse.Message;
                        return(Ok(new { status, message }));
                    }
                    else
                    {
                        status  = true;
                        message = "City Has Been Successfully Updated.";
                        CityUpdateResponseModel data = cityResponse.CityUpdate;
                        return(Ok(new { status, message, data }));
                    }
                }

                message = "Unable to Update the City Details";
                return(Ok(new { status, message }));
            }
            catch (Exception e)
            {
                return(BadRequest(new { e.Message }));
            }
        }
 /// <summary>
 /// It update the city
 /// </summary>
 /// <param name="CityId">City Id</param>
 /// <param name="updateCity">Update city Name or StateId</param>
 /// <returns>UpdateCityResponseModel</returns>
 public UpdateCityResponseModel UpdateCity(int CityId, UpdateCityRequestModel updateCity)
 {
     try
     {
         if (CityId <= 0 || updateCity == null)
         {
             return(null);
         }
         else
         {
             return(_cityRepository.UpdateCity(CityId, updateCity));
         }
     }
     catch (Exception e)
     {
         throw new Exception(e.Message);
     }
 }
Beispiel #3
0
        /// <summary>
        /// It Update the City
        /// </summary>
        /// <param name="CityId">City Id</param>
        /// <param name="updateCity">Update City Name or State Id</param>
        /// <returns>UpdateCityResponseModel</returns>
        public UpdateCityResponseModel UpdateCity(int CityId, UpdateCityRequestModel updateCity)
        {
            try
            {
                SqlDataReader           reader;
                int                     statusCode, CityPresent;
                string                  errorMsg     = "";
                bool                    errorFlag    = false;
                UpdateCityResponseModel cityResponse = null;

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

                    SqlParameter CityPresentParameter = sqlCommand.Parameters.Add("@CityNamePresentCount", 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();
                    CityPresent = Convert.ToInt32(sqlCommand.Parameters["@CityNamePresentCount"].Value);
                    statusCode  = Convert.ToInt32(sqlCommand.Parameters["@return_Value"].Value);

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

                    if (errorFlag)
                    {
                        cityResponse = new UpdateCityResponseModel
                        {
                            ErrorResponse = new ErrorResponseModel
                            {
                                ErrorStatus = errorFlag,
                                Message     = errorMsg
                            }
                        };

                        return(cityResponse);
                    }

                    if (statusCode == 0)
                    {
                        if (reader.HasRows)
                        {
                            cityResponse = new UpdateCityResponseModel();

                            while (reader.Read())
                            {
                                cityResponse.CityUpdate = new CityUpdateResponseModel
                                {
                                    CityId     = Convert.ToInt32(reader[0]),
                                    Name       = reader[1].ToString(),
                                    StateId    = Convert.ToInt32(reader[2]),
                                    CreatedAt  = Convert.ToDateTime(reader[3]),
                                    ModifiedAt = Convert.ToDateTime(reader[4])
                                };

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

                return(cityResponse);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }