Sha256: 5e56583c1135b3691c351b5e655e9a5043e3548a80d28f79aa1b315796e62639

Contents?: true

Size: 1.73 KB

Versions: 16

Compression:

Stored size: 1.73 KB

Contents

require_dependency 'notee/application_controller'

module Notee
  class PostsController < ApplicationController
    # callbacks
    before_action :set_post, only: [:show, :update, :destroy]

    # GET /posts
    def index
      @posts = Post.where(is_deleted: false).order(updated_at: :desc)
      render json: { status: 'success', posts: @posts }
    end

    # GET /posts/1
    def show
      render json: { status: 'success', post: @post}
    end

    # POST /posts
    def create
      @post = Post.new(post_params)
      @post.set_user_id
      respond_to do |format|
        if @post.save
          format.json { render json: @post, status: 200 }
        else
          format.json { render json: @post.errors, status: :unprocessable_entity }
        end
      end
    end

    # PATCH/PUT /posts/1
    def update
      post_params[:user_id] = @post.user_id
      respond_to do |format|
        if @post.update(post_params)
          format.json { render json: @post, status: 200 }
        else
          format.json { render json: @post.errors, status: :unprocessable_entity }
        end
      end
    end

    # DELETE /posts/1
    def destroy
      respond_to do |format|
        if @post.update(is_deleted: true)
          format.json { render json: @post, status: 200 }
        else
          format.json { render json: @post.errors, status: :internal_server_error }
        end
      end
    end

    private

    def set_post
      @post = Post.find_by(id: params[:id])
    end

    # Only allow a trusted parameter "white list" through.
    def post_params
      params.require(:post).permit(:title, :content, :slug, :status, :user_id, :category_id, :thumbnail_id, :published_at, :seo_keyword, :seo_description, :secret_published_password)
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
notee-1.1.2.4 app/controllers/notee/posts_controller.rb
notee-1.1.2.3 app/controllers/notee/posts_controller.rb
notee-1.1.2.2 app/controllers/notee/posts_controller.rb
notee-1.1.2.1 app/controllers/notee/posts_controller.rb
notee-1.1.2 app/controllers/notee/posts_controller.rb
notee-1.1.1 app/controllers/notee/posts_controller.rb
notee-1.1.0 app/controllers/notee/posts_controller.rb
notee-1.0.8 app/controllers/notee/posts_controller.rb
notee-1.0.7 app/controllers/notee/posts_controller.rb
notee-1.0.6 app/controllers/notee/posts_controller.rb
notee-1.0.5 app/controllers/notee/posts_controller.rb
notee-1.0.4 app/controllers/notee/posts_controller.rb
notee-1.0.3 app/controllers/notee/posts_controller.rb
notee-1.0.2 app/controllers/notee/posts_controller.rb
notee-1.0.1 app/controllers/notee/posts_controller.rb
notee-1.0.0 app/controllers/notee/posts_controller.rb