Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions app/controllers/api/v1/documents.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ def ok(user_message)

def auth?(collection_handle, token)
ES.collection_repository.find(collection_handle).token == token
rescue Elasticsearch::Persistence::Repository::DocumentNotFound, Elasticsearch::Transport::Transport::Errors::BadRequest
rescue Elasticsearch::Persistence::Repository::DocumentNotFound,
Elasticsearch::Transport::Transport::Errors::BadRequest
false
end

def document_repository
index_name = DocumentRepository.index_namespace(@collection_handle)
index_name = params[:index_name] || DocumentRepository.index_namespace(@collection_handle)
DocumentRepository.new(index_name: index_name)
end
end
Expand Down Expand Up @@ -128,15 +129,17 @@ def document_repository
post do
id = params.delete(:document_id)
document = Document.new(params.merge(id: id))
if document.invalid?
error!({ developer_message: document.errors.full_messages.join(', '), status: 400 }, 400)
end
error!({ developer_message: document.errors.full_messages.join(', '), status: 400 }, 400) if document.invalid?
document_repository.save(document, op_type: :create)
ok('Your document was successfully created.')
end

desc 'Update a document'
params do
optional :index_name,
type: String,
allow_blank: false,
desc: 'Elasticsearch index name for the document'
optional :title,
type: String,
allow_blank: false,
Expand Down Expand Up @@ -247,9 +250,7 @@ def document_repository
searchgov_custom3
tags])
document.attributes = document.attributes.merge(params)
if document.invalid?
error!({ developer_message: document.errors.full_messages.join(', '), status: 400 }, 400)
end
error!({ developer_message: document.errors.full_messages.join(', '), status: 400 }, 400) if document.invalid?
document_repository.update(document)
ok('Your document was successfully updated.')
end
Expand Down