-
Notifications
You must be signed in to change notification settings - Fork 2
Add more apis #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Add more apis #16
Conversation
| api :GET, '/admin/states', 'Get current user\' info' | ||
| def info | ||
| success do | ||
| current_user_info |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
babaf62 to
03ff6ab
Compare
03ff6ab to
81abb9e
Compare
shouya
left a comment
There was a problem hiding this 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 |
There was a problem hiding this comment.
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?
app/models/concerns/likeable.rb
Outdated
| include AccountAPIHelper | ||
|
|
||
| class_methods do | ||
| def add_likeable_for |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
app/models/concerns/likeable.rb
Outdated
| define_method "like" do |user_id| | ||
| unless find_like(user_id) | ||
| Like.create(user_id: user_id, target: self) | ||
| { message: 'success'} |
There was a problem hiding this comment.
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.
app/models/concerns/likeable.rb
Outdated
| end | ||
|
|
||
| define_method "find_like" do |user_id| | ||
| @action ||= Like.where(user_id: user_id, target: self).first |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
@actionis a very common name. it might cause a name collision.
what about @first_like_cached?
app/serializers/post_serializer.rb
Outdated
| :tags, | ||
| :published_at | ||
|
|
||
| def liked |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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' |
There was a problem hiding this comment.
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! |
There was a problem hiding this comment.
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)
app/models/concerns/likeable.rb
Outdated
| { message: 'success'} | ||
| else | ||
| { error: 'already liked' } | ||
| return true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just true is sufficient.
3fbe0aa to
b5d8323
Compare
|
關於相關文章,你可以試試:
|
|
@shouya searchkick 不支持其他的analyzer, ankane/searchkick#924 |
|
那你正好可以用 hooopo 的魔改版啊 https://github.com/hooopo/searchkick |
Added new apis:
GET /api/v1/admin/infoget current user detail, only work if logged in as adminPOST /api/v1/posts/:post_id/likelike specific postPOST /api/v1/posts/:post_id/unlikeunlike specific postPOST /api/v1/posts/:post_id/comments/:comment_id/likeunlike specific commentPOST /api/v1/posts/:post_id/comments/:comment_id/unlikeunlike specific commentPOST /api/v1/admin/posts/:post_id/toggle_recommendedtoggle recommended of specific post