Skip to content

Commit

Permalink
Added CommentsController tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oemera committed Feb 26, 2012
1 parent 786d65e commit 03f013e
Show file tree
Hide file tree
Showing 13 changed files with 376 additions and 115 deletions.
1 change: 0 additions & 1 deletion .gitignore
@@ -1,5 +1,4 @@
.DS_Store
config/initializers/secret_token.rb
log/
test/
tmp/
24 changes: 13 additions & 11 deletions app/controllers/api/basic_api.rb
@@ -1,14 +1,5 @@
require 'action_controller'
module Api::BasicApi
def parse_body_json
begin
# TODO: change name cause it is to confussing
@params = JSON.parse(request.body.read)
rescue JSON::ParserError => e
head :bad_request
end
end

def not_found
head :not_found
end
Expand All @@ -24,11 +15,11 @@ def internal_server_error
end

def no_parameter_found
respond_with failure_message("Missing parameters"), :status => 422
render :json => failure_message("Missing parameters"), :status => 422
end

def not_loged_in
respond_with failure_message("Please login"), :status => 401
render :json => failure_message("Please login"), :status => 401
end

def success_message(message, info = nil)
Expand All @@ -41,6 +32,17 @@ def failure_message(message)
return {:success => false, :message => message}
end

def check_login
raise "NotLogedIn" unless user_signed_in?
end

def page_num(num = nil)
page = num.to_i
if page.nil? or page < 1 then page = 1 end

return page
end

private
def set_format
request.format = 'json'
Expand Down
37 changes: 24 additions & 13 deletions app/controllers/api/v1/comments_controller.rb
@@ -1,11 +1,11 @@
class Api::V1::CommentsController < Api::ApplicationController
before_filter :authenticate_user!, :except => [:show, :show_post_comments, :show_user_comments]
before_filter :check_login, only: [:vote_up, :create, :edit, :destroy]
before_filter :check_login, only: [:up_vote, :down_vote, :create, :update, :destroy]

# GET /comment/:id
def show
comment = Comment.find(params[:id])
raise ActiveRecord::RecordNotFound if post.nil?
raise ActiveRecord::RecordNotFound if comment.nil?

render :json => comment
end
Expand All @@ -26,25 +26,36 @@ def show_user_comments
render :json => user.comments
end

# PUT /comment/:id/vote
def vote_up
current_user.up_vote(Comment.find(params[:id]))
# PUT /comment/:id/upvote
def up_vote
comment = Comment.find(params[:id])
raise ActiveRecord::RecordNotFound if comment.nil?

current_user.up_vote(comment)

render :json => success_message("Successfully up voted comment")
end

# PUT /comment/:id/unvote
def unvote
# TODO: unvote?
current_user.up_vote(Comment.find(params[:id]))
# PUT /comment/:id/downvote
def down_vote
comment = Comment.find(params[:id])
raise ActiveRecord::RecordNotFound if comment.nil?

current_user.down_vote(comment)

render :json => success_message("Successfully down voted comment")
end

# POST /comment
def create
comment = Comment.new(@params)
return not_loged_in unless user_signed_in?

comment = Comment.new(params["comment"])
comment.user_id = current_user.id

if comment.save
current_user.up_vote!(@comment)
render :json => @comment, :status => :created, :location => @comment
current_user.up_vote!(comment)
render :json => comment, :status => :created
else
render :json => comment.errors, :status => :unprocessable_entity
end
Expand All @@ -56,7 +67,7 @@ def update
raise ActiveRecord::RecordNotFound if comment.nil?
raise "NoPermission" unless is_own_comment?(comment)

if comment.update_attributes(@params)
if comment.update_attributes(params["comment"])
head :ok
else
failure = failure_message("Couldn't update comment", {:errors => comment.errors})
Expand Down
110 changes: 65 additions & 45 deletions app/controllers/api/v1/posts_controller.rb
@@ -1,83 +1,103 @@
class Api::V1::PostsController < Api::ApplicationController
#prepend_before_filter :require_no_authentication, :only => [:show]
before_filter :authenticate_user!, :except => [:show]
#before_filter warden.custom_failure!
before_filter :check_login, only: [:up_vote, :down_vote, :create, :edit, :destroy]

# GET /post/:id
def show
post = Post.find(params[:id])
if post.nil? then raise ActiveRecord::RecordNotFound end
raise ActiveRecord::RecordNotFound if post.nil?

comments = post.comments
render :json => post
end

# PUT /post/:id/vote
def up_vote
post = Post.find(params[:id])
raise ActiveRecord::RecordNotFound if post.nil?

render :json => { :success => true, :post => post }, :status => 200
current_user.up_vote(post)
#post.save

render :json => success_message("Successfully voted up post")
end

# PUT /post/:id/unvote
def down_vote
post = Post.find(params[:id])
raise ActiveRecord::RecordNotFound if post.nil?

current_user.down_vote(post)
#post.save

render :json => success_message("Successfully voted down post")
end

# POST /post
def create
if user_signed_in? then
@post = Post.new(params[:post])
@post.user_id = current_user.id
return not_loged_in unless user_signed_in?

post = Post.new(params["post"])
post.user_id = current_user.id

if post.save
current_user.up_vote!(post)

if @post.save
current_user.up_vote!(@post)
render :json => @post, :status => :created, :location => @post
else
render :json => @post.errors, :status => :unprocessable_entity
end
status = :created #:location => post
response = post
else
render :json => {:success => false, :message => "Please login"}, :status => 401
status = :unprocessable_entity
errors = {:errors => post.errors}
response = failure_message("Couldn't create post", errors)
end
end

# GET /post/:id/comments
def show_comments
@post = Post.find(params[:id])
@parent_comments = @post.comments

render :json => @post.comments
render :json => response, :status => status
end

# PUT /post/:id/vote
def vote
if current_user.nil? then
render :json => {:success => false, :message => "Please login"}, :status => 401
return
end
# PUT /post/:id
def update
post = Post.find(params[:id])
if post.nil? then
render :json => {:success => false, :message => "Couldn't find post with id #{params[:id]}"}, :status => 401
return
raise ActiveRecord::RecordNotFound if post.nil?
raise "NoPermission" unless is_own_post?(post)

if post.update_attributes(params["post"])
#head :ok
render :json => {:post => post, :params => params}
else
failure = failure_message("Couldn't update user", {:errors => post.errors})
render :json => failure, :status => :unprocessable_entity
end
current_user.up_vote(post)
render :json => {:success => true, :message => "Successfully voted up"}, :status => 200
end

# DELETE /post/:id
def destroy
post = Post.find(params[:id])
raise ActiveRecord::RecordNotFound if post.nil?
raise "NoPermission" unless is_own_post?(post)

post.destroy
head :ok
end

# GET /posts/frontpage
# GET /posts/frontpage/page/:page
def frontpage
(params[:page].nil? or params[:page].to_i < 1) ? @page = 1 : @page = params[:page].to_i
offset = ((@page-1)*20)
@posts = Post.find_by_sql ["SELECT * FROM posts ORDER BY ((posts.up_votes - posts.down_votes) -1 )/POW((((UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(posts.created_at)) / 3600 )+2), 1.5) DESC LIMIT ?, 20", offset]
render :json => @posts
render :json => Post.find_frontpage(page_num(params[:page]))
end

# GET /posts/new
# GET /posts/new/page/:page
def new
(params[:page].nil? or params[:page].to_i < 1) ? @page = 1 : @page = params[:page].to_i
offset = ((@page-1)*20)
@posts = Post.find(:all, :order => "created_at DESC", :limit => 20, :offset => offset)
render :json => @posts
render :json => Post.find_new(page_num(params[:page]))
end

# GET /posts/ask
# GET /posts/ask/page/:page
def ask
(params[:page].nil? or params[:page].to_i < 1) ? @page = 1 : @page = params[:page].to_i
offset = ((@page-1)*20)
@posts = Post.find_by_sql ["SELECT * FROM posts WHERE posts.link = '' ORDER BY ((posts.up_votes - posts.down_votes) -1 )/POW((((UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(posts.created_at)) / 3600 )+2), 1.5) DESC LIMIT ?, 20", offset]
render :json => @posts
render :json => Post.find_ask(page_num(params[:page]))
end

private
def is_own_post?(post)
return post.user.eql? current_user
end
end
8 changes: 2 additions & 6 deletions app/controllers/api/v1/sessions_controller.rb
Expand Up @@ -6,6 +6,7 @@ class Api::V1::SessionsController < Devise::SessionsController

prepend_before_filter :require_no_authentication, :only => [:create]
before_filter :set_format
before_filter :check_login, only: :destroy
before_filter :parse_body_json, only: :create
before_filter :authenticate_user!

Expand Down Expand Up @@ -43,12 +44,7 @@ def create
end

def destroy
puts "user_signed_in?: #{user_signed_in?}"
unless user_signed_in? then
return render :json => failure_message("Please log in"), :status => 401
end

puts current_user.email
puts current_user.email
current_user.authentication_token = nil
current_user.save
render :json => success_message("Successfully logged out")
Expand Down
16 changes: 8 additions & 8 deletions app/controllers/api/v1/users_controller.rb
@@ -1,18 +1,18 @@
class Api::V1::UsersController < Api::ApplicationController
prepend_before_filter :require_no_authentication, :only => [:signup]
before_filter :parse_body_json, only: [:update, :signup]

# GET /api/v1/users/:name
# GET /api/v1/users/:name(/:page)
def show
page = params[:page].to_i
name = params[:name].to_s
user = User.find_by_name(name)
if user.nil? then raise ActiveRecord::RecordNotFound end
#page = page(params[:page])
user = User.find_by_name(params[:name])
raise ActiveRecord::RecordNotFound if user.nil?

# TODO: find_all_posts
posts = Post.find_ordered(user.id, page)
if page.nil? or page < 1 then page = 1 end

# user_json = {:name => user.name, :posts => posts, :page => page}
user_json = {:name => user.name}
# TODO: Email address only for registred users and loged in ?
user_json = {:name => user.name, :posts => posts, :page => page}
user_json[:email] = user.email if user_signed_in?

render :json => user_json
Expand Down
39 changes: 32 additions & 7 deletions app/models/post.rb
Expand Up @@ -11,17 +11,42 @@ class Post < ActiveRecord::Base

make_voteable

def self.find_ordered(user_id, page = nil)
def self.find_user_posts(user, page = nil)
self.find_ordered self.offset(page), "user_id = #{user}"
end

def self.find_frontpage(page = nil)
self.find_ordered self.offset(page)
end

def self.find_ask(page = nil)
self.find_ordered self.offset(page), "link = ''"
end

def self.find_new(page = nil)
offset = self.offset(page)
Post.find(:all, :order => "created_at DESC", :limit => 20, :offset => offset)
end

private
def self.find_ordered(offset, where = nil)
where = where.nil? ? "" : "WHERE #{where}"
sql = "SELECT * FROM posts
#{where}
ORDER BY #{order_algorithm}
DESC LIMIT ?, 20"

Post.find_by_sql [sql, offset]
end

def self.offset(page = nil)
if page.nil? or page < 1 then page = 1 end
offset = ((page-1)*20)
end

offset = ((page-1)*20)
def self.order_algorithm
date_diff = "((UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(posts.created_at))"
order_algorithm = "((posts.up_votes - posts.down_votes) -1)/
POW((#{date_diff} / 3600)+2), 1.5)"

return Post.find_by_sql ["SELECT * FROM posts
WHERE user_id = #{user_id}
ORDER BY #{order_algorithm}
DESC LIMIT ?, 20", offset]
end
end

0 comments on commit 03f013e

Please sign in to comment.