Class: Api::V1::LikesController

Inherits:
BaseController show all
Defined in:
app/controllers/api/v1/likes_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


32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/controllers/api/v1/likes_controller.rb', line 32

def create
  post = post_service.find!(params.require(:post_id))
  raise ActiveRecord::RecordInvalid unless post.public? || private_read?

  like_service.create(params[:post_id])
rescue ActiveRecord::RecordInvalid => e
  if e.message == "Validation failed: Target has already been taken"
    return render_error 409, "Like already exists"
  end

  raise
else
  head :no_content
end

#destroyObject

Raises:

  • (ActiveRecord::RecordInvalid)

47
48
49
50
51
52
53
54
55
56
57
# File 'app/controllers/api/v1/likes_controller.rb', line 47

def destroy
  post = post_service.find!(params.require(:post_id))
  raise ActiveRecord::RecordInvalid unless post.public? || private_read?

  success = like_service.unlike_post(params[:post_id])
  if success
    head :no_content
  else
    render_error 410, "Like doesn’t exist"
  end
end

#showObject

Raises:

  • (ActiveRecord::RecordInvalid)

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

def show
  post = post_service.find!(params.require(:post_id))
  raise ActiveRecord::RecordInvalid unless post.public? || private_read?

  likes_query = like_service.find_for_post(params[:post_id])
  likes_page = index_pager(likes_query).response
  likes_page[:data] = likes_page[:data].map {|x| like_json(x) }
  render_paged_api_response likes_page
end