Sha256: 9d27e80082ebee89e1e672620b24a91ca314c40efb4499d59f0bad91e27a4d15

Contents?: true

Size: 1.47 KB

Versions: 8

Compression:

Stored size: 1.47 KB

Contents

require_dependency 'notee/application_controller'

module Notee
  class CommentsController < ApplicationController
    before_action :set_comment, only: [:update, :destroy]

    def index
      comments = Comment.where(is_deleted: false).order(updated_at: :desc)
      render json: { status: 'success', comments: comments }
    end

    def show
      @comments = Comment.where(post_id: params[:id])
      render json: { status: 'success', comments: @comments }
    end

    def create
      @comment = Comment.new(comment_params)
      if @comment.save
        render json: { status: 'success' }
      else
        render json: { status: 'failed' }
      end
    end

    def update
      respond_to do |format|
        if @comment.update(is_hidden: !@comment.is_hidden)
          format.json { render json: @comment, status: 200 }
        else
          format.json { render json: @comment.errors, status: :unprocessable_entity }
        end
      end
    end

    def destroy
      respond_to do |format|
        if @comment.update(is_deleted: true)
          format.json { render json: @comment, status: 200 }
        else
          format.json { render json: @comment.errors, status: :internal_server_error }
        end
      end
    end

    private

    def set_comment
      @comment = Comment.find_by(id: params[:id])
    end

    # Only allow a trusted parameter "white list" through.
    def comment_params
      params.require(:comment).permit(:post_id, :content, :name, :email)
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
notee-1.0.7 app/controllers/notee/comments_controller.rb
notee-1.0.6 app/controllers/notee/comments_controller.rb
notee-1.0.5 app/controllers/notee/comments_controller.rb
notee-1.0.4 app/controllers/notee/comments_controller.rb
notee-1.0.3 app/controllers/notee/comments_controller.rb
notee-1.0.2 app/controllers/notee/comments_controller.rb
notee-1.0.1 app/controllers/notee/comments_controller.rb
notee-1.0.0 app/controllers/notee/comments_controller.rb