diff --git a/app/controllers/v1/learning_objects/attachment_controller.rb b/app/controllers/v1/learning_objects/attachment_controller.rb index b75dd85bfd4956688d2d876ae292389d5c3ce0c3..7bdee9b46196df1afdc45f4cee3b548d25bf4b19 100644 --- a/app/controllers/v1/learning_objects/attachment_controller.rb +++ b/app/controllers/v1/learning_objects/attachment_controller.rb @@ -18,11 +18,19 @@ # along with portalmec. If not, see <http://www.gnu.org/licenses/>. class V1::LearningObjects::AttachmentController < ApplicationController + include ::Paginator + before_action :set_objects - before_action :authenticate_user!, only: :update + before_action :authenticate_user!, only: [:update, :index] before_action :authorize!, only: :destroy before_action :authorize_update_attachment!, only: :update + # GET /learning_objects/:learning_object_id/attachment + def index + @attachment = paginate LearningObject::Attachment.includes(:attachment) + render json: @attachment + end + # DELETE /learning_objects/:learning_object_id/attachments/:id def destroy return render status: :not_found if @learning_object.nil? || @attachment.nil? @@ -32,12 +40,12 @@ class V1::LearningObjects::AttachmentController < ApplicationController render status: :ok end - + # PUT /learning_objects/:learning_object_id/attachments/:id def update return render status: :not_found if @learning_object.nil? || @attachment.nil? - if @attachment.update(infohash: infohash_params[:infohash]) - render status: :ok + if @attachment.update(update_params) + render json: @attachment, status: :ok else render status: :unprocessable_entity end @@ -49,13 +57,17 @@ class V1::LearningObjects::AttachmentController < ApplicationController params.permit(:learning_object_id, :id) end - def infohash_params - params.permit(:infohash) + def update_params + params.permit(:attachment_id, :infohash) end def set_objects @learning_object = LearningObject.find(attachment_params[:learning_object_id]) - @attachment = LearningObject::Attachment.find(attachment_params[:id]) + if ( attachment_params[:id].blank? ) + @attachment = @learning_object.attachment + else + @attachment = LearningObject::Attachment.find(attachment_params[:id]) + end end def authorize! @@ -68,6 +80,7 @@ class V1::LearningObjects::AttachmentController < ApplicationController return render status: :unauthorized unless @learning_object.attachments.include? @attachment authorize(@learning_object, :add_infohash?) + authorize(@learning_object, :add_attachment_id?) end end diff --git a/app/models/learning_object/attachment.rb b/app/models/learning_object/attachment.rb index dae5e242603b786bdfe65d563fe8127d8272d3a5..9f57093d4448e4ed255b84fbe3f98fdf428af6dd 100644 --- a/app/models/learning_object/attachment.rb +++ b/app/models/learning_object/attachment.rb @@ -45,6 +45,10 @@ class LearningObject::Attachment < ApplicationRecord include ::Thumbnailable belongs_to :learning_object scope :unknown_mime_type, ->() { where(format: 'Unknown') } + has_one :attachment, class_name: 'LearningObject::Attachment' + belongs_to :attachment, class_name: 'LearningObject::Attachment' + # has_and_belongs_to_many + # belongs_to def retrieve_url "#{DspaceService.link}/rest#{retrieve_link}" diff --git a/app/policies/learning_object_policy.rb b/app/policies/learning_object_policy.rb index 1b06e4dd5b188748a706c480fc2475fa9308870b..29e3943154ac7377f42d4d25aad8909e41d3388a 100644 --- a/app/policies/learning_object_policy.rb +++ b/app/policies/learning_object_policy.rb @@ -65,6 +65,10 @@ class LearningObjectPolicy < ApplicationPolicy record if user.is_admin? end + def add_attachment_id? + record if user.is_admin? + end + def show? return record if record.published? || ( !user.nil? && user_can_edit? ) return record if user == record.publisher diff --git a/app/serializers/learning_object/attachment_serializer.rb b/app/serializers/learning_object/attachment_serializer.rb index 63c078b23f099b4485414e12a1138f4dec685d35..4d192a099124204a71747713b40756e9c5e4daba 100644 --- a/app/serializers/learning_object/attachment_serializer.rb +++ b/app/serializers/learning_object/attachment_serializer.rb @@ -19,5 +19,20 @@ class LearningObject::AttachmentSerializer < ActiveModel::Serializer cache key: 'attachment', expires_in: 24.hours - attributes :id, :name, :retrieve_link, :description, :format, :mime_type, :size, :bundle_name, :learning_object_id, :created_at, :updated_at, :id_dspace, :thumbnail, :cache_link + attributes \ + :id, + :name, + :retrieve_link, + :description, + :format, + :mime_type, + :size, + :bundle_name, + :learning_object_id, + :created_at, + :updated_at, + :id_dspace, + :thumbnail, + :cache_link, + :attachment_id end diff --git a/config/routes.rb b/config/routes.rb index 1f36d091234713db1e3c154c267557b9f769889c..9e6acb6386e8fb3ea34dfa2a0f440c178a6b1103 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -168,7 +168,7 @@ Rails.application.routes.draw do collection do get 'validate' end - resources :attachment, module: 'learning_objects', only: [:destroy, :update], on: :member + resources :attachment, module: 'learning_objects', only: [:destroy, :update, :index], on: :member end resources :institutions, concerns: :deletable do diff --git a/db/migrate/20170703175829_add_submitter_request_to_users.rb b/db/migrate/20170703175829_add_submitter_request_to_users.rb index 305eab65145b3761c83c26daafa1b5b2292fa2c1..614a3b5fb9f895875502c9bf99387310764c64c5 100644 --- a/db/migrate/20170703175829_add_submitter_request_to_users.rb +++ b/db/migrate/20170703175829_add_submitter_request_to_users.rb @@ -1,3 +1,22 @@ + +# 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 AddSubmitterRequestToUsers < ActiveRecord::Migration[5.0] def change add_column :users, :submitter_request, :integer, default: 0 diff --git a/db/migrate/20170808132744_add_info_hash_to_learning_object_attachments.rb b/db/migrate/20170808132744_add_info_hash_to_learning_object_attachments.rb index 0b0725c9987f26666f11f66fa740eeefb490362d..25312c48cc93eaa5c70cd3f051db6a623cccbe70 100644 --- a/db/migrate/20170808132744_add_info_hash_to_learning_object_attachments.rb +++ b/db/migrate/20170808132744_add_info_hash_to_learning_object_attachments.rb @@ -1,3 +1,22 @@ + +# 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 AddInfoHashToLearningObjectAttachments < ActiveRecord::Migration[5.0] def change add_column :learning_object_attachments, :infohash, :string diff --git a/db/migrate/20170825105946_add_attachment_for_learning_object_attachments.rb b/db/migrate/20170825105946_add_attachment_for_learning_object_attachments.rb new file mode 100644 index 0000000000000000000000000000000000000000..312afe3dbfee188db19dba761da4614ac44f2ea4 --- /dev/null +++ b/db/migrate/20170825105946_add_attachment_for_learning_object_attachments.rb @@ -0,0 +1,26 @@ + +# 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 AddAttachmentForLearningObjectAttachments < ActiveRecord::Migration[5.0] + def change + add_column :learning_object_attachments, :attachment_id, :integer + add_foreign_key :learning_object_attachments, :learning_object_attachments, column: :attachment_id + add_index :learning_object_attachments, :attachment_id, unique: true + end +end