85 lines
2.5 KiB
Ruby
85 lines
2.5 KiB
Ruby
# 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
|
|
@sp[:id] ||= @sp[:md5]
|
|
@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.deletion_reason = params.dig(:api_image, :deletion_reason)
|
|
@image.destroy
|
|
redirect_to(yiff_rest_api_v2_manage_images_path(search: { category: @image.category }), notice: "Image deleted")
|
|
end
|
|
|
|
def delete_with_reason
|
|
# deletion_reason
|
|
@image = APIImage.find(params[:id])
|
|
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
|
|
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
|