Websites/app/controllers/yiff_rest/api_v2/images_controller.rb

78 lines
2.2 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
module YiffRest
module APIV2
class ImagesController < ApplicationController
before_action :validate_discord
before_action :prepare_user
before_action :admin_only
def index
@sp = search_images_params
@pagy, @images = pagy(APIImage.search(@sp).order(created_at: :desc), size: [1, 2, 2, 1])
end
def new
@image = APIImage.new(create_params)
@categories = APIImage.categories
end
def edit
@image = APIImage.find(params[:id])
@categories = APIImage.categories
end
def create
@service = APIImageUploadService.new(create_params)
@image = @service.start!
@categories = APIImage.categories
if @image.invalid?
flash.now[:alert] = @image.errors.full_messages.join(", ")
return render(:new)
end
redirect_to(yiff_rest_api_v2_manage_images_path(search: { category: @image.category }), notice: "Image added")
end
def update
@image = APIImage.find(params[:id])
@image.update(update_params)
@categories = APIImage.categories
if @image.invalid?
flash.now[:alert] = @image.errors.full_messages.join(", ")
return render(:edit)
end
redirect_to(yiff_rest_api_v2_manage_images_path(search: { category: @image.category }), notice: "Image updated")
end
def destroy
@image = APIImage.find(params[:id])
@image.destroy
redirect_to(yiff_rest_api_v2_manage_images_path(search: { category: @image.category }), notice: "Image deleted")
end
private
def load_category
@category = APIImage.categories.find { |c| c.db == params[:manage_id] }
raise(ActiveRecord::RecordNotFound) if @category.blank?
end
def site_title
"YiffyAPI V2 - Manage Images"
end
def search_images_params
2024-05-07 06:57:00 +00:00
permit_search_params(%i[category md5 original_url])
end
def create_params
params.fetch(:api_image, {}).permit(:category, :original_url, :sources_string, :artists_string, :file)
end
def update_params
params.fetch(:api_image, {}).permit(:category, :sources_string, :artists_string)
end
end
end
end