Newer
Older
class V1::CollectionsController < ApplicationController
Mauricio Giacomini Girardello
committed
include ::SociableController
include ::FollowableController
include ::TaggableController
include ::DeletedObjectsController
include ::HighlightsController
before_action :set_collection, only: [:show, :update, :destroy]
before_action :authenticate_user!, only: [:create, :update, :destroy]
Mauricio Giacomini Girardello
committed
# GET /v1/collections
# GET /v1/collections.json
def index
render json: Collection.all
end
Mauricio Giacomini Girardello
committed
# GET /v1/collections/1
# GET /v1/collections/1.json
Mauricio Giacomini Girardello
committed
render json: @collection
Mauricio Giacomini Girardello
committed
# POST /v1/collection
# POST /v1/collection.json
Mauricio Giacomini Girardello
committed
collection = Collection.new(collection_params)
Mauricio Giacomini Girardello
committed
if collection.save
render json: collection, status: :created
Mauricio Giacomini Girardello
committed
render json: collection.errors, status: :unprocessable_entity
end
end
# PUT/PATCH /v1/users/1
# PUT/PATCH /v1/users/1.json
def update
Mauricio Giacomini Girardello
committed
if @collection.update(collection_params)
render json: @collection, status: :ok
Mauricio Giacomini Girardello
committed
render json: @collection.errors, status: :unprocessable_entity
Mauricio Giacomini Girardello
committed
# DELETE /v1/collections/1
# DELETE /v1/collections/1.json
Mauricio Giacomini Girardello
committed
@collection.destroy
render nothing: true, status: :ok
Lucas Ernesto Kindinger
committed
# POST /v1/collections/!/itens
def add_object
# {
# "collection" : {
# "itens" : [
# { "id": 300, "type" : "LearningObject"},
# { "id": 233, "type" : "Collection"},
# { "id": 028, "type" : "LearningObject"},
# { "id": 070, "type" : "LearningObject"},
# { "id": 110, "type" : "Collection"}
# ]
# }
# }
#= Receba a hash dos parametros
itens = params[:itens]
#= Retorne unprocessable entity Se parametros invalidos
if params.nil?
render: nothing: true, status: :unprocessable_entity
end
Lucas Ernesto Kindinger
committed
#= Ordem recebe, caso último elemento não nulo, ordem do último elemento, senão 1
order = CollectionItem.all.where("collection_id=#{@collection.id}").nil? ? 1 : CollectionItem.first.where("collection_id=#{@collection.id}").order('order DESC')
#= Para cada item da hash
itens.each do |item|
#= Instancie CollectionItem
item = CollectionItem.new(item)
#= Defina a ordem
item.order = order
#= Acrescente 1 ao contador de ordem
item = item+1
#= Salve a instancia
item.save
#=
end
#= Renderize a coleção com os itens, e envie status ok
render @collection, status: :ok
end
def deleted_resource; Collection; end
def highlights_resource; Collection; end
Mauricio Giacomini Girardello
committed
# social concerns methods
def followable; set_collection; end
def taggable; set_collection; end
def sociable; set_collection; end
def set_collection
@collection ||= Collection.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def collection_params
Lucas Ernesto Kindinger
committed
params.require(:collection).permit(:name, :description, :owner_id, :owner_type, :privacy, tags: [])
end
def collection_params
params.require(:collection).permit(itens: [])