[Dmails] Add route to mark single dmail read

This commit is contained in:
Earlopain 2023-12-17 20:06:10 +01:00
parent 16014a438f
commit f477141c63
No known key found for this signature in database
GPG Key ID: 48860312319ADF61
3 changed files with 18 additions and 1 deletions

View File

@ -1,6 +1,6 @@
class DmailsController < ApplicationController
respond_to :html
respond_to :json, only: %i[index show destroy mark_all_as_read]
respond_to :json, only: %i[index show destroy mark_as_read mark_all_as_read]
before_action :member_only
def new
@ -44,6 +44,12 @@ class DmailsController < ApplicationController
redirect_to dmails_path, :notice => "Message destroyed"
end
def mark_as_read
@dmail = Dmail.find(params[:id])
check_privilege(@dmail)
@dmail.mark_as_read!
end
def mark_all_as_read
Dmail.visible.unread.each do |x|
x.update_column(:is_read, true)

View File

@ -129,6 +129,9 @@ Rails.application.routes.draw do
end
end
resources :dmails, :only => [:new, :create, :index, :show, :destroy] do
member do
post :mark_as_read
end
collection do
post :mark_all_as_read
end

View File

@ -71,6 +71,14 @@ class DmailsControllerTest < ActionDispatch::IntegrationTest
end
end
context "mark as read action" do
should "mark the dmail as read" do
post_auth mark_as_read_dmail_path(@dmail), @dmail.owner, params: { format: :json }
assert_response :success
assert_predicate @dmail.reload, :is_read?
end
end
context "create action" do
setup do
@user_2 = create(:user)