diff --git a/app/controllers/api/playlists_controller.rb b/app/controllers/api/playlists_controller.rb index ac46b33..436de1e 100644 --- a/app/controllers/api/playlists_controller.rb +++ b/app/controllers/api/playlists_controller.rb @@ -4,4 +4,9 @@ class Api::PlaylistsController < Api::ApplicationController def index @playlists = Playlist.all end + + def create + @playlist = current_user.playlists.build(params[:playlist].slice(:name)) + @playlist.save + end end diff --git a/app/views/api/playlists/create.rabl b/app/views/api/playlists/create.rabl new file mode 100644 index 0000000..002dfce --- /dev/null +++ b/app/views/api/playlists/create.rabl @@ -0,0 +1,4 @@ +object @playlist + +attribute :id +attribute :name diff --git a/config/routes.rb b/config/routes.rb index b625e3a..09819fe 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,6 @@ Scube::Application.routes.draw do namespace :api do - resources :playlists, :only => [:index] + resources :playlists, :only => [:index, :create] resources :sessions, :only => [:create] match '*all' => 'application#cor_preflight', :via => :options diff --git a/spec/controllers/api/playlists_controller_spec.rb b/spec/controllers/api/playlists_controller_spec.rb index 977a62f..2bd0e9a 100644 --- a/spec/controllers/api/playlists_controller_spec.rb +++ b/spec/controllers/api/playlists_controller_spec.rb @@ -30,4 +30,23 @@ describe Api::PlaylistsController do do_get.each { |t| t.keys.should include 'name' } end end + + describe 'POST create' do + def do_create + post :create, + :format => :json, + :playlist => Factory.attributes_for(:playlist) + end + + it 'creates a new playlist for the current user' do + expect { + do_create + }.to change(controller.current_user.playlists, :count).by(1) + end + + it 'assigns the playlist' do + do_create + assigns[:playlist].should be_a Playlist + end + end end diff --git a/spec/integration/api/playlists_spec.rb b/spec/integration/api/playlists_spec.rb new file mode 100644 index 0000000..024b922 --- /dev/null +++ b/spec/integration/api/playlists_spec.rb @@ -0,0 +1,22 @@ +require 'spec_helper' + +feature 'API playlists' do + include UserIntegrationHelpers + + background do + api_sign_in + end + + scenario 'creates playlist' do + playlist = Factory.attributes_for :playlist + + post api_playlists_path, + :format => :json, + :playlist => playlist + + json = JSON response.body + + json['id'].should be_a Fixnum + json['name'].should == playlist[:name] + end +end