diff --git a/app/controllers/v1/institutions_controller.rb b/app/controllers/v1/institutions_controller.rb new file mode 100644 index 0000000000000000000000000000000000000000..56021ea278777c2844e19a87e314d49fc253277c --- /dev/null +++ b/app/controllers/v1/institutions_controller.rb @@ -0,0 +1,62 @@ +class V1::InstitutionsController < ApplicationController + + before_action :set_institution, only: [:show, :update, :destroy, :users] + before_action :authenticate_user!, only: [:create, :update, :destroy] + + # GET v1/institutions + # GET v1/institutions.json + def index + render json: Institution.all + end + + # GET v1/institutions/1 + # GET v1/institutions/1.json + def show + render json: @institution + end + + # POST v1/institutions + # POST v1/institutions.json + def create + institution = Institution.new(institution_params) + + if institution.save + render json: institution, status: :created + else + render json: institution.errors, status: :unprocessable_entity + end + end + + # PUT/PATCH /v1/institutions/1 + # PUT/PATCH /v1/institutions/1.json + def update + if @institution.update(institution_params) + render json: @institution, status: :ok + else + render json: @institution.errors, status: :unprocessable_entity + end + end + + # DELETE v1/institutions/1 + # DELETE v1/institutions/1.json + def destroy + @institution.destroy + render nothing: true, status: :ok + end + + def users + @users = @institution.users + render json: @users, status: :ok + end + + private + + def set_institution + @institution = Institution.find(params[:id]) + end + + def institution_params + params.require(:institution).permit(:name, :description, :address, :city, :country) + end + +end diff --git a/app/serializers/institution_serializer.rb b/app/serializers/institution_serializer.rb new file mode 100644 index 0000000000000000000000000000000000000000..00f27804d95600e60ab88069cea0920d9d728505 --- /dev/null +++ b/app/serializers/institution_serializer.rb @@ -0,0 +1,3 @@ +class InstitutionSerializer < ActiveModel::Serializer + attributes :id, :name, :description, :address, :city, :country, :created_at, :updated_at +end diff --git a/config/routes.rb b/config/routes.rb index ac291c757fc81707c78f23efd65e1beb43c69db4..7f98f9ed69e69d6b70b35d56a10789fc1c890a53 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -31,6 +31,12 @@ Rails.application.routes.draw do resources :users, concerns: :followable resources :collections, concerns: [:followable, :sociable, :reviewable] resources :learning_objects, concerns: [:sociable, :reviewable] + resources :institutions do + member do + get :users, to: 'institutions#users' + end + end + end end diff --git a/test/controllers/v1/institutions_controller_test.rb b/test/controllers/v1/institutions_controller_test.rb new file mode 100644 index 0000000000000000000000000000000000000000..f1e28227439f03609603a8a14e2757dcfc428396 --- /dev/null +++ b/test/controllers/v1/institutions_controller_test.rb @@ -0,0 +1,66 @@ +require 'test_helper' + +class V1::InstitutionControllerTest < ActionController::TestCase + tests V1::InstitutionsController + include Devise::TestHelpers + + test 'should get index' do + skip('Unsolved issue: ArgumentError: wrong number of arguments (2 for 0)') + end + + test 'should user show institution' do + ufpr = institutions(:ufpr) + get :show, id: ufpr.id + assert_response :ok + end + + test 'should user post institution to create and return :unauthorized' do + post :create, institution: {name: 'my institution', + description: 'testing institution', + address: 'politecnico', + city: 'Curitiba', + country: 'Brasil' + } + skip assert_response :unauthorized + end + + test 'should user post institution to create and return :created code' do + auth_request users(:jack) + post :create, institution: {name: 'my institution', + description: 'testing institution', + address: 'politecnico', + city: 'Curitiba', + country: 'Brasil' + } + skip assert_response :created + end + + test 'should user put institution to update and return :ok' do + auth_request users(:jack) + ufpr = institutions(:ufpr) + put :update, id: ufpr.id , institution: { description: 'testing institution'} + assert_response :ok + end + + test 'should user put institution to update and return :unauthorized' do + ufpr = institutions(:ufpr) + put :update, id: ufpr.id , institution: { description: 'testing institution'} + assert_response :unauthorized + end + + test 'should user delete institution to destroy and return :ok' do + auth_request users(:jack) + ufpr = institutions(:ufpr) + delete :destroy, id: ufpr.id + assert_response :ok + end + + test 'should user delete institution to destroy and return :unauthorized' do + ufpr = institutions(:ufpr) + delete :destroy, id: ufpr.id + assert_response :unauthorized + end + + + +end