diff --git a/app/controllers/v1/posts_controller.rb b/app/controllers/v1/posts_controller.rb new file mode 100644 index 0000000..138c680 --- /dev/null +++ b/app/controllers/v1/posts_controller.rb @@ -0,0 +1,44 @@ +class V1::PostsController < V1::BaseController + before_action :load_post, only: [:update, :show, :destroy] + + def index + @posts = Post.all + render json: template_respone(200, "Get list post successfully", @posts) + end + + def show + render json: template_respone(200, "Get post successfully", @post) + end + + def create + @post = Post.create!(post_params) + render template_respone(200, "Create post successfully", @post) + end + + def update + @post.update_attributes(post_params) + render template_respone(200, "Update post successfully", @post) + end + + def destroy + @post.destroy + render template_respone(200, "Destroy post successfully", @post) + end + + private + def load_post + @post = Post.find(params[:id]) + end + + def post_params + params.permit(:title, :content) + end + + def template_respone(code, message,result) + { + code: code, + message: message, + result: result + } + end +end diff --git a/app/models/post.rb b/app/models/post.rb new file mode 100644 index 0000000..3d0c9e1 --- /dev/null +++ b/app/models/post.rb @@ -0,0 +1,3 @@ +class Post < ActiveRecord::Base + validates :title, :content, presence: true +end diff --git a/config/routes.rb b/config/routes.rb index 4ac2454..953c324 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,6 @@ Rails.application.routes.draw do api_version(:module => "V1", :path => {:value => "v1"}) do + resources :posts end end diff --git a/db/migrate/20150917150440_create_posts.rb b/db/migrate/20150917150440_create_posts.rb new file mode 100644 index 0000000..683ca19 --- /dev/null +++ b/db/migrate/20150917150440_create_posts.rb @@ -0,0 +1,10 @@ +class CreatePosts < ActiveRecord::Migration + def change + create_table :posts do |t| + t.string :title + t.string :content + + t.timestamps null: false + end + end +end diff --git a/test/fixtures/posts.yml b/test/fixtures/posts.yml new file mode 100644 index 0000000..98a9c20 --- /dev/null +++ b/test/fixtures/posts.yml @@ -0,0 +1,9 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + title: MyString + content: MyString + +two: + title: MyString + content: MyString diff --git a/test/functional/v1/posts_controller_test.rb b/test/functional/v1/posts_controller_test.rb new file mode 100644 index 0000000..63b95dc --- /dev/null +++ b/test/functional/v1/posts_controller_test.rb @@ -0,0 +1,9 @@ +require 'test_helper' + +class V1::PostsControllerTest < ActionController::TestCase + + # Replace this with your real tests. + test "the truth" do + assert true + end +end diff --git a/test/integration/v1/posts_controller_test.rb b/test/integration/v1/posts_controller_test.rb new file mode 100644 index 0000000..4281411 --- /dev/null +++ b/test/integration/v1/posts_controller_test.rb @@ -0,0 +1,9 @@ +require 'test_helper' + +class V1::PostsControllerTest < ActionDispatch::IntegrationTest + + # Replace this with your real tests. + test "the truth" do + assert true + end +end diff --git a/test/models/post_test.rb b/test/models/post_test.rb new file mode 100644 index 0000000..6d9d463 --- /dev/null +++ b/test/models/post_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class PostTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end