lib/icarus/mod/firestore.rb in Icarus-Mod-Tools-2.0.4 vs lib/icarus/mod/firestore.rb in Icarus-Mod-Tools-2.1.0

- old
+ new

@@ -45,21 +45,21 @@ end def update(type, payload, merge: false) raise "You must specify a payload to update" if payload&.empty? || payload.nil? - case type.to_sym - when :modinfo, :toolinfo - update_array = (send(type) + [payload]).flatten.uniq - response = @client.doc(collections.send(type)).set({ list: update_array }, merge:) if update_array.any? - when :repositories - response = @client.doc(collections.repositories).set({ list: payload }, merge:) - when :mod, :tool - response = update_or_create(pluralize(type), payload, merge:) - else - raise "Invalid type: #{type}" - end + response = case type.to_sym + when :modinfo, :toolinfo + update_array = (send(type) + [payload]).flatten.uniq + @client.doc(collections.meta.send(type)).set({ list: update_array }, merge:) if update_array.any? + when :repositories + @client.doc(collections.meta.repositories).set({ list: payload }, merge:) + when :mod, :tool + create_or_update(pluralize(type), payload, merge:) + else + raise "Invalid type: #{type}" + end response.is_a?(Google::Cloud::Firestore::DocumentReference) || response.is_a?(Google::Cloud::Firestore::CommitResponse::WriteResult) end def delete(type, payload) @@ -76,21 +76,21 @@ private def list(type) case type.to_sym when :modinfo, :toolinfo, :repositories - @client.doc(collections.send(type)).get[:list] + @client.doc(collections.meta.send(type)).get[:list] when :mods, :tools @client.col(collections.send(type)).get.map do |doc| klass = type == :mods ? Icarus::Mod::Tools::Modinfo : Icarus::Mod::Tools::Toolinfo klass.new(doc.data, id: doc.document_id, created: doc.create_time, updated: doc.update_time) end else raise "Invalid type: #{type}" end end - def update_or_create(type, payload, merge:) + def create_or_update(type, payload, merge:) doc_id = payload.id || find_by_type(type:, name: payload.name, author: payload.author)&.id return @client.doc("#{collections.send(type)}/#{doc_id}").set(payload.to_h, merge:) if doc_id @client.col(collections.send(type)).add(payload.to_h)