routes
Rails.application.routes.draw do
# apipie
root 'home#index'
devise_for :users
resources :articles, :defaults => {:format => :json}
post '/create_user', to: 'registrations#create'
namespace :api, :defaults => {:format => :json} do
namespace :v1 do
devise_scope :user do
post "/sign_in", :to => 'sessions#create'
post "/sign_up", :to => 'registrations#create'
delete "/sign_out:id", :to => 'sessions#destroy'
get "/users" , to: 'users#index'
post "/users/update/:id" , to: 'users#update'
get "/users/:id", :to => 'users#edit'
post "/users/new" , to: 'users#create'
get "/users/:id/destroy" , to: 'users#destroy'
get "/users/:id/show" , to: 'users#show'
get "/articles" , to: 'articles#index'
post "/articles/new", :to => 'articles#create'
post "/articles/update/:id" , to: 'articles#update'
get "/articles/:id/destroy" , to: 'articles#destroy'
get "/articles/:id/show" , to: 'articles#show'
get "/search_article/:search" , to: 'articles#search_article'
end
end
end
# For details on the DSL available within thi1s file, see http://guides.rubyonrails.org/routing.html
end
----------------------------------------------------------------------------------------------------------------------
articles_controller.rb
class Api::V1::ArticlesController < Api::V1::ApiController
skip_before_action :verify_authenticity_token
# before_action :authenticate_article!
def index
begin
@articles = Article.all
return render json: {status: 200, data: {articles: @articles}, message: "all articles list"}
rescue
return render json: {status: 500, data: {articles: nil}, message: "Something Went Wrong"}
end
end
def articles_as_json(data)
@articles = data[:articles].map{ |m| m.as_json() }
end
def create
article = Article.new(article_params)
if article.save!
return render json: {status: 200, data: {article: article}, :message =>"Article was successfully created"}
else
warden.custom_failure!
return render json: {status: 401, data: {article: nil, errors: article.errors}}
end
end
def edit
@article = Article.find(params[:id])
render json: article
end
def show
@article = Article.find_by_id(params[:id])
if @article.present?
render json: {status: 200, data: {article: @article}, message: "Article Details"}
else
render json: { errors: "Please enter correct id" },status: :unprocessable_entity
end
end
def update
if params[:id].present?
if Article.all.map(&:id).include?(params[:id].to_i)
@article = Article.find(params[:id])
if @article.update(article_params)
render json: {status: 200, data: {article: @article}, message: "Successfully Updated"}
else
render json: { errors: @article.errors.full_messages }, status: :unprocessable_entity
end
else
render json: { error: 'Could not be found for this id.' }
end
else
render json: { error: 'Please Add parameter id.' }
end
end
def search_article
if params[:search].present?
@article = Article.where("lower(title) LIKE :prefix OR lower(description) LIKE :prefix", prefix: "%#{params[:search].downcase}%")
render json: {status: 200, data: {article: @article}, message: "Search Article"}
else
render json: { error: 'Could not be found for this input.' }
end
end
def destroy
@article = Article.find_by_id(params[:id])
if @article.present?
@article.delete
render json: {status: 200, message: "Successfully Deleted"}
else
render json: { error: 'Could not be found for this id.' }
end
end
private
def rescue_section
return render json: {status: 500, data: {news: nil}, message: "Something Went Wrong"}
end
def article_params
params.require(:article).permit(:title, :description, :start_date, :end_date, :image, :user_id)
end
end
-----------------------------------------------------------------------------------
body
{
"article": {
"title": "test",
"description": "tesst2"
}
}
Comments
Post a Comment