# frozen_string_literal: true module Admin class AnnouncementsController < BaseController before_action :set_announcement, only: %i[show edit update destroy] def index sort_column = params[:sort].presence || "created_at" sort_direction = params[:direction] == "desc" ? :desc : :asc @announcements = Announcement.reorder(sort_column => sort_direction) @breadcrumbs = [ { label: "Announcements" } ] end def show @breadcrumbs = [ { label: "Announcements", path: admin_announcements_path }, { label: @announcement.title } ] end def new @announcement = Announcement.new @breadcrumbs = [ { label: "Announcements", path: admin_announcements_path }, { label: "New Announcement" } ] end def edit @breadcrumbs = [ { label: "Announcements", path: admin_announcements_path }, { label: @announcement.title, path: admin_announcement_path(@announcement) }, { label: "Edit" } ] end def create @announcement = Announcement.new(announcement_params) if @announcement.save redirect_to admin_announcement_path(@announcement), notice: t(".notice") else @breadcrumbs = [ { label: "Announcements", path: admin_announcements_path }, { label: "New Announcement" } ] render :new, status: :unprocessable_content end end def update if @announcement.update(announcement_params) redirect_to admin_announcement_path(@announcement), notice: t(".notice") else @breadcrumbs = [ { label: "Announcements", path: admin_announcements_path }, { label: @announcement.title, path: admin_announcement_path(@announcement) }, { label: "Edit" } ] render :edit, status: :unprocessable_content end end def destroy @announcement.destroy redirect_to admin_announcements_path, notice: t(".notice") end private def set_announcement @announcement = Announcement.find(params.expect(:id)) end def announcement_params params.expect(announcement: %i[title content featured]) end end end