Friday, September 20, 2024 12:06:44 AM
> settings

Customize


Authenticate

> notifications_controller.rb
# frozen_string_literal: true

class NotificationsController < DashboardController
  before_action :redirect_if_player_mode
  skip_before_action :load_communities, only: %i[create update destroy]

  def index
    @notifications = current_community.notifications
    @servers = current_community.servers.map { |server| OpenStruct.new(id: server.server_id, name: server.server_name) }
  end

  def create
    notification = Notification.new(notification_params.merge(community_id: current_community.id))

    if notification.save
      render json: {notifications: current_community.notifications}
    else
      render json: {message: "I'm sorry, we were unable to create the notification<br>Please try again later"}
    end
  end

  def update
    notification = Notification.where(id: params[:id], community_id: current_community.id).first

    # Make sure it wasn't delete
    if notification.nil?
      return render json: {message: "I'm sorry, we were unable to find the requested notification.", notifications: current_community.notifications}, status: :unprocessable_entity
    end

    if notification.update(notification_params.merge(community_id: current_community.id))
      render json: {notifications: current_community.notifications}
    else
      render json: {message: "I'm sorry, we were unable to update that notification<br>Please try again later"}, status: :unprocessable_entity
    end
  end

  def destroy
    notification = Notification.where(id: params[:id], community_id: current_community.id).first

    # Make sure it wasn't delete
    if notification.nil?
      return render json: {message: "We were unable to find the requested notification.", notifications: current_community.notifications}, status: :unprocessable_entity
    end

    if notification.destroy
      render json: {notifications: current_community.notifications}
    else
      render json: {message: "I'm sorry, we were unable to delete that notification<br>Please try again later"}, status: :unprocessable_entity
    end
  end

  private

  def notification_params
    params.require("notification").permit(:notification_category, :notification_type, :notification_title, :notification_description, :notification_color)
  end
end
All opinions represented herein are my own
- © 2024 itsthedevman
- build 3c15a1b