Skip to content

Conversation

@chen7897499
Copy link
Contributor

@chen7897499 chen7897499 commented May 27, 2017

Added new apis:

  • GET /api/v1/admin/info get current user detail, only work if logged in as admin
  • POST /api/v1/posts/:post_id/like like specific post
  • POST /api/v1/posts/:post_id/unlike unlike specific post
  • POST /api/v1/posts/:post_id/comments/:comment_id/like unlike specific comment
  • POST /api/v1/posts/:post_id/comments/:comment_id/unlike unlike specific comment
  • POST /api/v1/admin/posts/:post_id/toggle_recommended toggle recommended of specific post

api :GET, '/admin/states', 'Get current user\' info'
def info
success do
current_user_info
Copy link
Collaborator

@shouya shouya May 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is too dirty, why don't explicitly specify what attributes to expose?

Copy link
Collaborator

@shouya shouya May 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, if you have a simple value to render using success, simply use success current_user_info. No need for the block stuff.

@chen7897499 chen7897499 force-pushed the add-more-apis branch 3 times, most recently from babaf62 to 03ff6ab Compare June 1, 2017 01:45
Copy link
Collaborator

@shouya shouya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're on the right track, keep going!

api :GET, '/posts/:id', 'Show specific post'
def show
success(@post)
@post.increment
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

increment is a bit ambiguous. What about using add_instance_counter_for :view in the model and invoke incr_view_count here?

include AccountAPIHelper

class_methods do
def add_likeable_for
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this method doesn't have an argument, just name it add_likable

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about adding a has_many likes automatically here?

define_method "like" do |user_id|
unless find_like(user_id)
Like.create(user_id: user_id, target: self)
{ message: 'success'}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A model method should just return true/false here, instead of a message. Let the controllers do the job.

end

define_method "find_like" do |user_id|
@action ||= Like.where(user_id: user_id, target: self).first
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just use Like.find_by(user_id: user_id, target: self) directly.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This instance variable @action has two problems:

  • ambiguous meaning here
  • since you're writing a concern, you won't know where it's gonna be imported. @action is a very common name. it might cause a name collision.

what about @first_like_cached?

:tags,
:published_at

def liked
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this method to your Likeable concern.

@post.increment
success(serializer: ::PostSerializer, user_id: current_user_id) do
@post
end
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not simply:

success(@post,
        serializer: ::PostSerializer,
        user_id: current_user_id)

end
end

api :GET, '/admin/states', 'Get current user\' info'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sure this action gets restricted to admin only.


api :POST, '/admin/posts/:post_id/toggle_recommended', 'toggle推荐文章'
def toggle_recommended
@post.toggle_recommended!
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to write a method for that. just use:

@post.toggle(:recommended)

see: https://apidock.com/rails/ActiveRecord/Base/toggle

{ message: 'success'}
else
{ error: 'already liked' }
return true
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just true is sufficient.

@shouya
Copy link
Collaborator

shouya commented Jun 29, 2017

關於相關文章,你可以試試:

@chen7897499
Copy link
Contributor Author

@shouya searchkick 不支持其他的analyzer, ankane/searchkick#924
我还是用elasticsearch gem 算了

@shouya
Copy link
Collaborator

shouya commented Jun 30, 2017

那你正好可以用 hooopo 的魔改版啊 https://github.com/hooopo/searchkick

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants