Files
astuto/app/controllers/likes_controller.rb

39 lines
790 B
Ruby
Raw Normal View History

2019-09-27 12:32:30 +02:00
class LikesController < ApplicationController
before_action :authenticate_user!
def create
like = Like.new(like_params)
if like.save
render json: like, status: :created
else
render json: {
error: I18n.t('errors.likes.create', message: like.errors.full_messages)
}, status: :unprocessable_entity
end
end
def destroy
2019-09-27 16:57:23 +02:00
like = Like.find_by(like_params)
return if like.nil?
2019-09-27 12:32:30 +02:00
if like.destroy
2019-09-27 16:57:23 +02:00
render json: {}, status: :accepted
2019-09-27 12:32:30 +02:00
else
render json: {
error: I18n.t('errors.likes.destroy', message: like.errors.full_messages)
}, status: :unprocessable_entity
end
end
private
def like_params
2019-09-27 16:57:23 +02:00
{
post_id: params[:post_id],
user_id: current_user.id,
}
2019-09-27 12:32:30 +02:00
end
end