Files
astuto/app/controllers/follows_controller.rb
Riccardo Graziosi dad382d2b1 Post follow and updates notifications V1 (#111)
* It is now possible to follow a post in order to receive updates about it
* Notifications are now sent when updates are published
* Post status changes are now tracked
* Update sidebar now shows the post status history
* Mark a comment as a post update using the comment form
* ... more ...
2022-05-28 11:03:36 +02:00

53 lines
1.0 KiB
Ruby

class FollowsController < ApplicationController
before_action :authenticate_user!, only: [:create, :destroy]
def index
unless user_signed_in?
render json: { }
return
end
follow = Follow.find_by(follow_params)
render json: follow
end
def create
follow = Follow.new(follow_params)
if follow.save
render json: {
id: follow.id
}, status: :created
else
render json: {
error: I18n.t('errors.follows.create', message: follow.errors.full_messages)
}, status: :unprocessable_entity
end
end
def destroy
follow = Follow.find_by(follow_params)
id = follow.id
return if follow.nil?
if follow.destroy
render json: {
id: id,
}, status: :accepted
else
render json: {
error: I18n.t('errors.follow.destroy', message: follow.errors.full_messages)
}, status: :unprocessable_entity
end
end
private
def follow_params
{
post_id: params[:post_id],
user_id: current_user.id,
}
end
end