Skip to content

Commit

Permalink
Merge pull request #6 from dhienan/feature/3-api-posts
Browse files Browse the repository at this point in the history
Feature/3 api posts
  • Loading branch information
Duong Hien An committed Sep 17, 2015
2 parents aac6288 + c792722 commit bc41b1d
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 0 deletions.
44 changes: 44 additions & 0 deletions app/controllers/v1/posts_controller.rb
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 json: template_respone(200, "Create post successfully", @post)
end

def update
@post.update_attributes(post_params)
render json: template_respone(200, "Update post successfully", @post)
end

def destroy
@post.destroy
render json: 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
3 changes: 3 additions & 0 deletions app/models/post.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Post < ActiveRecord::Base
validates :title, :content, presence: true
end
1 change: 1 addition & 0 deletions config/routes.rb
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
10 changes: 10 additions & 0 deletions db/migrate/20150917150440_create_posts.rb
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
23 changes: 23 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20150917150440) do

create_table "posts", force: :cascade do |t|
t.string "title", limit: 255
t.string "content", limit: 255
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

end
9 changes: 9 additions & 0 deletions test/fixtures/posts.yml
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
9 changes: 9 additions & 0 deletions test/functional/v1/posts_controller_test.rb
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
9 changes: 9 additions & 0 deletions test/integration/v1/posts_controller_test.rb
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
7 changes: 7 additions & 0 deletions test/models/post_test.rb
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

0 comments on commit bc41b1d

Please sign in to comment.