Implement unpinning answers

This commit is contained in:
Karina Kwiatek 2023-02-07 08:36:29 +01:00
parent b196909b79
commit 410d9b5d8e
4 changed files with 45 additions and 1 deletions

View File

@ -28,4 +28,14 @@ class AnswerController < ApplicationController
format.turbo_stream { render "pin", locals: { answer: } }
end
end
def unpin
answer = Answer.includes(:user).find(params[:id])
UseCase::Answer::Unpin.call(user: current_user, answer:)
respond_to do |format|
format.html { redirect_to(user_path(username: current_user.screen_name)) }
format.turbo_stream { render "pin", locals: { answer: } }
end
end
end

View File

@ -1,5 +1,5 @@
- if answer.pinned?
= button_to pin_answer_path(id: answer.id),
= button_to unpin_answer_path(id: answer.id),
class: "dropdown-item",
method: :delete,
form: { id: "ab-pin-#{answer.id}", data: { turbo_stream: true } } do

View File

@ -147,6 +147,7 @@ Rails.application.routes.draw do
get "/@:username", to: "user#show", as: :user
get "/@:username/a/:id", to: "answer#show", as: :answer
post "/@:username/a/:id/pin", to: "answer#pin", as: :pin_answer
delete "/@:username/a/:id/pin", to: "answer#unpin", as: :unpin_answer
get "/@:username/q/:id", to: "question#show", as: :question
get "/@:username/followers", to: "user#followers", as: :show_user_followers
get "/@:username/followings", to: "user#followings", as: :show_user_followings

View File

@ -0,0 +1,33 @@
# frozen_string_literal: true
module UseCase
module Answer
class Unpin < UseCase::Base
option :user, type: Types.Instance(::User)
option :answer, type: Types.Instance(::Answer)
def call
check_ownership!
check_pinned!
answer.pinned_at = nil
answer.save!
{
status: 200,
resource: nil
}
end
private
def check_ownership!
raise ::Errors::NotAuthorized unless answer.user == user
end
def check_pinned!
raise ::Errors::BadRequest if answer.pinned_at.nil?
end
end
end
end