Sha256: 18193d0b106a6d1450546421fe0fa6908294b9e89102dfd8fd67ff07e0f118e8

Contents?: true

Size: 1.37 KB

Versions: 4

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

module Mihari
  module Web
    module Endpoints
      #
      # Tag API endpoint
      #
      class Tags < Grape::API
        class TagDestroyer < Service
          #
          # @param [Integer] id
          #
          def call(id)
            Mihari::Models::Tag.find(id).destroy
          end
        end

        namespace :tags do
          desc "Get tags", {
            is_array: true,
            success: Entities::Tags,
            summary: "Get tags"
          }
          get "/" do
            tags = Mihari::Models::Tag.distinct.pluck(:name)
            present({ tags: tags }, with: Entities::Tags)
          end

          desc "Delete a tag", {
            success: { code: 204, model: Entities::Message },
            failure: [{ code: 404, model: Entities::Message }],
            summary: "Delete a tag"
          }
          params do
            requires :id, type: Integer
          end
          delete "/:id" do
            status 204

            id = params[:id].to_i
            result = TagDestroyer.result(id)
            return present({ message: "" }, with: Entities::Message) if result.success?

            case result.failure
            when ActiveRecord::RecordNotFound
              error!({ message: "ID:#{id} is not found" }, 404)
            end
            raise result.failure
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mihari-6.3.0 lib/mihari/web/endpoints/tags.rb
mihari-6.2.0 lib/mihari/web/endpoints/tags.rb
mihari-6.1.0 lib/mihari/web/endpoints/tags.rb
mihari-6.0.0 lib/mihari/web/endpoints/tags.rb