2024-05-29 12:39:58 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Admin::Config::FlagsController < Admin::AdminController
|
|
|
|
def toggle
|
2024-07-03 06:45:37 +08:00
|
|
|
with_service(Flags::ToggleFlag) do
|
2024-05-29 12:39:58 +08:00
|
|
|
on_success do
|
|
|
|
Discourse.request_refresh!
|
|
|
|
render(json: success_json)
|
|
|
|
end
|
|
|
|
on_failure { render(json: failed_json, status: 422) }
|
|
|
|
on_model_not_found(:message) { raise Discourse::NotFound }
|
|
|
|
on_failed_policy(:invalid_access) { raise Discourse::InvalidAccess }
|
|
|
|
on_failed_contract do |contract|
|
|
|
|
render(json: failed_json.merge(errors: contract.errors.full_messages), status: 400)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def index
|
|
|
|
end
|
2024-06-05 11:27:06 +08:00
|
|
|
|
2024-07-03 06:45:37 +08:00
|
|
|
def new
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
with_service(Flags::CreateFlag) do
|
|
|
|
on_success do
|
|
|
|
Discourse.request_refresh!
|
2024-08-13 16:32:11 +08:00
|
|
|
render json: result.flag, serializer: FlagSerializer
|
2024-07-03 06:45:37 +08:00
|
|
|
end
|
|
|
|
on_failure { render(json: failed_json, status: 422) }
|
|
|
|
on_failed_policy(:invalid_access) { raise Discourse::InvalidAccess }
|
|
|
|
on_failed_contract do |contract|
|
|
|
|
render(json: failed_json.merge(errors: contract.errors.full_messages), status: 400)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
with_service(Flags::UpdateFlag) do
|
|
|
|
on_success do
|
|
|
|
Discourse.request_refresh!
|
2024-08-13 16:32:11 +08:00
|
|
|
render json: result.flag, serializer: FlagSerializer
|
2024-07-03 06:45:37 +08:00
|
|
|
end
|
|
|
|
on_failure { render(json: failed_json, status: 422) }
|
|
|
|
on_model_not_found(:message) { raise Discourse::NotFound }
|
|
|
|
on_failed_policy(:not_system) { render_json_error(I18n.t("flags.errors.system")) }
|
|
|
|
on_failed_policy(:not_used) { render_json_error(I18n.t("flags.errors.used")) }
|
|
|
|
on_failed_policy(:invalid_access) { raise Discourse::InvalidAccess }
|
|
|
|
on_failed_contract do |contract|
|
|
|
|
render(json: failed_json.merge(errors: contract.errors.full_messages), status: 400)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-06-05 11:27:06 +08:00
|
|
|
def reorder
|
2024-07-03 06:45:37 +08:00
|
|
|
with_service(Flags::ReorderFlag) do
|
2024-06-05 11:27:06 +08:00
|
|
|
on_success do
|
|
|
|
Discourse.request_refresh!
|
|
|
|
render(json: success_json)
|
|
|
|
end
|
|
|
|
on_failure { render(json: failed_json, status: 422) }
|
|
|
|
on_model_not_found(:message) { raise Discourse::NotFound }
|
|
|
|
on_failed_policy(:invalid_access) { raise Discourse::InvalidAccess }
|
|
|
|
on_failed_policy(:invalid_move) { render_json_error(I18n.t("flags.errors.wrong_move")) }
|
|
|
|
on_failed_contract do |contract|
|
|
|
|
render(json: failed_json.merge(errors: contract.errors.full_messages), status: 400)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2024-07-03 06:45:37 +08:00
|
|
|
|
|
|
|
def destroy
|
|
|
|
with_service(Flags::DestroyFlag) do
|
|
|
|
on_success do
|
|
|
|
Discourse.request_refresh!
|
|
|
|
render(json: success_json)
|
|
|
|
end
|
|
|
|
on_failure { render(json: failed_json, status: 422) }
|
|
|
|
on_failed_policy(:not_system) { render_json_error(I18n.t("flags.errors.system")) }
|
|
|
|
on_failed_policy(:not_used) { render_json_error(I18n.t("flags.errors.used")) }
|
|
|
|
on_failed_policy(:invalid_access) { raise Discourse::InvalidAccess }
|
|
|
|
on_failed_contract do |contract|
|
|
|
|
render(json: failed_json.merge(errors: contract.errors.full_messages), status: 400)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2024-05-29 12:39:58 +08:00
|
|
|
end
|