Sha256: 2e018b2b145997444904cbcc24dd6ea4988dba386190d537f0575c670cbb31ef

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

module Mihari
  module Web
    module Endpoints
      #
      # Tag API endpoint
      #
      class Tags < Grape::API
        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: Entities::Message,
            failure: [{ code: 404, message: "Not found", model: Entities::Message }],
            summary: "Delete a tag"
          }
          params do
            requires :name, type: String
          end
          delete "/:name" do
            extend Dry::Monads[:result, :try]

            name = params[:name].to_s

            result = Try do
              Mihari::Models::Tag.where(name: name).destroy_all
            end.to_result

            if result.success?
              status 204
              return present({ message: "" }, with: Entities::Message)
            end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mihari-5.7.0 lib/mihari/web/endpoints/tags.rb