# Copyright (C) 2015 Centro de Computacao Cientifica e Software Livre # Departamento de Informatica - Universidade Federal do Parana # # This file is part of portalmec. # # portalmec is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # portalmec is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with portalmec. If not, see <http://www.gnu.org/licenses/>. 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, :follow, :unfollow, :follow_toggle] before_action :set_collection, only: [:show, :update, :destroy, :add_object, :delete_object, :subjecting, :unsubjecting, :add_stages, :remove_stages, :follow, :unfollow, :follow_toggle] before_action :set_new_collection, only: :index before_action :authorize!, except: [:create, :tagging, :untagging, :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 items = @collection.collection_items.select(:id) if !items.blank? @collection.delete_items(items) end @collection.destroy response = { 'status': 'deleted' } render status: :ok, json: response end # POST /v1/collections/1/items def add_object return render nothing: true, status: :unprocessable_entity if extra_params.blank? || extra_params[:items].blank? errors = @collection.add_items(extra_params[:items]) render json: {collection: CollectionSerializer.new(@collection, {scope: current_user, scope_name: :current_user}).serializable_hash, errors: errors}, status: :ok end # DELETE /v1/collections/1/items def delete_object return render nothing: true, status: :unprocessable_entity if extra_params.blank? || extra_params[:items].blank? @collection.really_delete_items(extra_params[:items]) render json: @collection, status: :ok end private def deleted_resource; Collection; end def highlights_resource; Collection; end # social concerns methods def followable; set_collection; end def taggable; set_collection; end def sociable; set_collection; end def downloadable; set_collection; end def subjectable; set_collection; end def stageable; set_collection; end def set_collection @collection ||= Collection.where(id: params[:id]).first render status: :not_found if @collection.blank? @collection end def set_new_collection @collection ||= Collection.new end # Never trust parameters from the scary internet, only allow the white list through. def collection_params params.require(:collection).permit(:name, :curator, :description, :owner_id, :owner_type, :privacy, tags: []) end def extra_params return {} if params[:collection].nil? params[:collection].permit(subjects: [], educational_stages: [], items: [:id, :type, :position], tags: [:name]) end def collection_associations(collection) if extra_params[:tags] == [] current_user.untag(collection, with: @collection.tags.map { |t| t['name'] }) elsif !extra_params[:tags].nil? current_user.tag(collection, with: extra_params[:tags].map { |t| t['name'] }) end collection.add_subjects(ids: extra_params[:subjects]) unless extra_params[:subjects].nil? collection.add_educational_stages(ids: extra_params[:educational_stages]) unless extra_params[:educational_stages].nil? collection.add_items(extra_params[:items]) unless extra_params[:items].nil? end def authorize! authorize @collection end end