Skip to content

Instantly share code, notes, and snippets.

@bartekupartek
Created September 24, 2015 08:15
Show Gist options
  • Save bartekupartek/cc1dec6ddf6a43db7ef7 to your computer and use it in GitHub Desktop.
Save bartekupartek/cc1dec6ddf6a43db7ef7 to your computer and use it in GitHub Desktop.
namespace :artwork_notes do
desc 'Return artwork notes'
get ':artwork_id' do
text_notes = TextNote.all('artwork_notes.es_artwork_id' => params[:artwork_id])
text_notes.map{|tn| tn.artwork_id = params[:artwork_id]}
text_notes
end
desc 'Add a note to an artwork'
params do
requires :content, type: String, desc: 'content MD'
end
post do
authenticate!
params[:note_properties]["id"] << SecureRandom.uuid
note = TextNote.new(content: params[:content],
note_properties: params[:note_properties],
vault: params[:vault],
user_id: current_user.id)
note.note_properties[:references].each do |ref|
if note.note_properties[:type] === "artist"
# req = {
# index: index_name(params[:vault]),
# type: 'artworks',
# fields: '_id',
# body: {
# query: {
# term: {
# :"source.authors._id" => {
# value: ref[:id] #artist_id
# }
# }
# }
# }
# }
# artworks = ElasticsearchConn.search(req)
# if artworks["hits"].any?
# artworks["hits"]["hits"].each do |hit|
# if hit["_id"] === params[:artwork_id]
# note.artwork_notes << ArtworkNote.new(es_artwork_id: hit["_id"])
# end
# end
# end
note.artist_notes << ArtistNote.new(artist_id: ref["id"], es_artwork_id: params[:artwork_id])
# note.artwork_notes << ArtworkNote.new(es_artwork_id: params[:artwork_id])
else
note.artwork_notes << ArtworkNote.new(es_artwork_id: ref["id"])
end
note.artwork_id = ref["id"]
note.ua_name = ref["ua_name"]
end
if note.save
note
else
error!(note.errors, 422)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment