Skip to content
Snippets Groups Projects
Forked from PortalMEC / portalmec
963 commits behind the upstream repository.
collections_controller.rb 3.82 KiB
class V1::CollectionsController < ApplicationController
  include ::SociableController
  include ::DownloadableController
  include ::FollowableController
  include ::TaggableController
  include ::DeletedObjectsController
  include ::HighlightsController
  include ::Paginator
  include ::SubjectableController
  include ::StageableController

  before_action :authenticate_user!, only: [:create, :update, :destroy, :tagging, :untagging]
  before_action :set_collection, only: [:show, :update, :destroy, :add_object, :delete_object, :subjecting, :unsubjecting, :add_stages, :remove_stages]
  before_action :set_new_collection, only: :index
  before_action :authorize!, except: [:create, :tagging, :untagging, :follow, :unfollow, :download]

  # GET /v1/collections
  # GET /v1/collections.json
  def index
    collections = paginate policy_scope(Collection)
    render json: collections
  end

  # GET /v1/collections/1
  # GET /v1/collections/1.json
  def show
    render json: @collection
  end

  # POST /v1/collection
  # POST /v1/collection.json
  def create
    collection = Collection.new(collection_params)
    collection.owner = current_user if collection.owner.nil?
    authorize collection

    if collection.save
      collection_associations(collection)
      render json: collection, status: :created
    else
      render json: collection.errors, status: :unprocessable_entity
    end
  end

  # PUT/PATCH /v1/users/1
  # PUT/PATCH /v1/users/1.json
  def update
    if @collection.update(collection_params)
      collection_associations(@collection)
      render json: @collection, status: :ok
    else
      render json: @collection.errors, status: :unprocessable_entity
    end
  end

  # DELETE /v1/collections/1
  # DELETE /v1/collections/1.json
  def destroy
    @collection.destroy
    render status: :ok
  end

  # POST /v1/collections/1/items
  def add_object
    return render nothing: true, status: :unprocessable_entity if extra_params.blank? || extra_params[:items].blank?
    @collection.add_items(extra_params[:items])
    render json: @collection, status: :ok
  end

  # DELETE /v1/collections/1/items