Class: Api::V1::CommentsController

Inherits:
BaseController show all
Defined in:
app/controllers/api/v1/comments_controller.rb

Instance Attribute Summary

Attributes included from OpenidConnect::ProtectedResourceEndpoint

#current_token

Instance Method Summary collapse

Methods included from OpenidConnect::ProtectedResourceEndpoint

#access_token?, #require_access_token

Instance Method Details

#createObject


22
23
24
25
26
27
28
29
# File 'app/controllers/api/v1/comments_controller.rb', line 22

def create
  find_post
  comment = comment_service.create(params.require(:post_id), params.require(:body))
rescue ActiveRecord::RecordNotFound
  render_error 404, "Post with provided guid could not be found"
else
  render json: comment_as_json(comment), status: :created
end

#destroyObject


41
42
43
44
45
46
47
48
49
# File 'app/controllers/api/v1/comments_controller.rb', line 41

def destroy
  find_post
  if comment_and_post_validate(params.require(:post_id), params[:id])
    comment_service.destroy!(params[:id])
    head :no_content
  end
rescue ActiveRecord::RecordInvalid
  render_error 403, "User not allowed to delete the comment"
end

#indexObject


31
32
33
34
35
36
37
38
39
# File 'app/controllers/api/v1/comments_controller.rb', line 31

def index
  find_post
  comments_query = comment_service.find_for_post(params.require(:post_id))
  params[:after] = Time.utc(1900).iso8601 if params.permit(:before, :after).empty?

  comments_page = time_pager(comments_query).response
  comments_page[:data] = comments_page[:data].map {|x| comment_as_json(x) }
  render_paged_api_response comments_page
end

#reportObject


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/controllers/api/v1/comments_controller.rb', line 51

def report
  find_post
  post_guid = params.require(:post_id)
  comment_guid = params.require(:comment_id)
  return unless comment_and_post_validate(post_guid, comment_guid)

  reason = params.require(:reason)
  comment = comment_service.find!(comment_guid)
  report = current_user.reports.new(
    item_id:   comment.id,
    item_type: "Comment",
    text:      reason
  )
  if report.save
    head :no_content
  else
    render_error 409, "This item already has been reported by this user"
  end
end