-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
duonghienan
committed
Sep 17, 2015
1 parent
aac6288
commit 72d230f
Showing
8 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
{ | ||
This comment has been minimized.
Sorry, something went wrong. |
||
code: code, | ||
message: message, | ||
result: result | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Post < ActiveRecord::Base | ||
validates :title, :content, presence: true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
Rails.application.routes.draw do | ||
|
||
api_version(:module => "V1", :path => {:value => "v1"}) do | ||
resources :posts | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require 'test_helper' | ||
|
||
class PostTest < ActiveSupport::TestCase | ||
# test "the truth" do | ||
# assert true | ||
# end | ||
end |
template_respone(code, message,result)
===>template_respone(code, message, result)