allow re-saving an existing collection without complaint
[arvados.git] / app / controllers / orvos / v1 / collections_controller.rb
index ccdf3843032c10a14da3d8ae30f3453956fb3e24..a1afd1e7155f748e6282771d928d92321ccb1e56 100644 (file)
@@ -1,94 +1,21 @@
 class Orvos::V1::CollectionsController < ApplicationController
-  # GET /collections
-  # GET /collections.json
-  def index
-    @collections = Collection.all
-
-    @collectionlist = {
-      :kind  => "orvos#collectionList",
-      :etag => "",
-      :self_link => "",
-      :next_page_token => "",
-      :next_link => "",
-      :items => @collections.map { |collection| collection }
-    }
-    respond_to do |format|
-      format.json { render json: @collectionlist }
-    end
-  end
-
-  # GET /collections/1
-  # GET /collections/1.json
-  def show
-    @collection = Collection.find(params[:id])
-
-    respond_to do |format|
-      format.html # show.html.erb
-      format.json { render json: @collection }
-    end
-  end
-
-  # GET /collections/new
-  # GET /collections/new.json
-  def new
-    @collection = Collection.new
-
-    respond_to do |format|
-      format.html # new.html.erb
-      format.json { render json: @collection }
-    end
-  end
-
-  # GET /collections/1/edit
-  def edit
-    @collection = Collection.find(params[:id])
-  end
-
-  # POST /collections
-  # POST /collections.json
   def create
-    if params[:collection].class == String
-      @collection = Collection.new(JSON.parse(params[:collection]))
-    else
-      @collection = Collection.new(params[:collection])
-    end
-
-    respond_to do |format|
-      if @collection.save
-        format.html { redirect_to @collection, notice: 'Collection was successfully created.' }
-        format.json { render json: @collection, status: :created, location: @collection }
-      else
-        format.html { render action: "new" }
-        format.json { render json: @collection.errors, status: :unprocessable_entity }
-      end
-    end
-  end
-
-  # PUT /collections/1
-  # PUT /collections/1.json
-  def update
-    @collection = Collection.find(params[:id])
-
-    respond_to do |format|
-      if @collection.update_attributes(params[:collection])
-        format.html { redirect_to @collection, notice: 'Collection was successfully updated.' }
-        format.json { head :ok }
-      else
-        format.html { render action: "edit" }
-        format.json { render json: @collection.errors, status: :unprocessable_entity }
+    # It's not an error for a client to re-register a manifest that we
+    # already know about.
+    @object = model_class.new resource_attrs
+    begin
+      @object.save!
+    rescue ActiveRecord::RecordNotUnique
+      logger.debug resource_attrs.inspect
+      if resource_attrs['manifest_text'] and resource_attrs['uuid']
+        @existing_object = model_class.
+          where('uuid=? and manifest_text=?',
+                resource_attrs['uuid'],
+                resource_attrs['manifest_text']).
+          first
+        @object = @existing_object || @object
       end
     end
-  end
-
-  # DELETE /collections/1
-  # DELETE /collections/1.json
-  def destroy
-    @collection = Collection.find(params[:id])
-    @collection.destroy
-
-    respond_to do |format|
-      format.html { redirect_to collections_url }
-      format.json { head :ok }
-    end
+    show
   end
 end