From ed64bd6ea222161783101b2bd2b21ee32369f765 Mon Sep 17 00:00:00 2001 From: Igor Zaytsev Date: Tue, 27 May 2025 12:03:17 -0400 Subject: [PATCH] Added optional index name param --- app/controllers/api/v1/documents.rb | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/app/controllers/api/v1/documents.rb b/app/controllers/api/v1/documents.rb index de151984..a5260136 100644 --- a/app/controllers/api/v1/documents.rb +++ b/app/controllers/api/v1/documents.rb @@ -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 @@ -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, @@ -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