Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
cs_comments_service
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
cs_comments_service
Commits
f8f8e800
Commit
f8f8e800
authored
Jul 26, 2012
by
Rocky Duan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use helper method to simplify
parent
8feb52a9
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
50 additions
and
51 deletions
+50
-51
app.rb
+50
-51
No files found.
app.rb
View file @
f8f8e800
...
...
@@ -20,134 +20,141 @@ Mongoid.logger.level = Logger::INFO
Dir
[
File
.
dirname
(
__FILE__
)
+
'/models/*.rb'
].
each
{
|
file
|
require
file
}
Dir
[
File
.
dirname
(
__FILE__
)
+
'/lib/**/*.rb'
].
each
{
|
file
|
require
file
}
helpers
do
def
commentable
@commentable
||=
Commentable
.
find
(
params
[
:commentable_id
])
end
def
user
# TODO handle 404 if integrated user service
@user
||=
(
User
.
find_or_create_by
(
external_id:
params
[
:user_id
])
if
params
[
:user_id
])
end
def
thread
@thread
||=
CommentThread
.
find
(
params
[
:thread_id
])
end
def
comment
@comment
||=
Comment
.
find
(
params
[
:comment_id
])
end
def
source
@source
||=
case
params
[
"source_type"
]
when
"user"
User
.
find_or_create_by
(
external_id:
params
[
"source_id"
])
when
"thread"
CommentThread
.
find
(
params
[
"source_id"
])
when
"other"
Commentable
.
find
(
params
[
"source_id"
])
end
end
def
vote_for
(
obj
)
user
.
vote
(
obj
,
params
[
"value"
].
to_sym
)
obj
.
reload
.
to_hash
.
to_json
end
def
undo_vote_for
(
obj
)
user
.
unvote
(
obj
)
obj
.
reload
.
to_hash
.
to_json
end
end
delete
'/api/v1/:commentable_id/threads'
do
|
commentable_id
|
Commentable
.
find
(
commentable_id
)
.
comment_threads
.
destroy_all
commentable
.
comment_threads
.
destroy_all
{}.
to_json
end
get
'/api/v1/:commentable_id/threads'
do
|
commentable_id
|
Commentable
.
find
(
commentable_id
)
.
comment_threads
.
map
{
|
t
|
t
.
to_hash
(
recursive:
params
[
"recursive"
])}.
to_json
commentable
.
comment_threads
.
map
{
|
t
|
t
.
to_hash
(
recursive:
params
[
"recursive"
])}.
to_json
end
post
'/api/v1/:commentable_id/threads'
do
|
commentable_id
|
thread
=
CommentThread
.
new
(
params
.
slice
(
*
%w[title body course_id]
).
merge
(
commentable_id:
commentable_id
))
author
=
User
.
find_or_create_by
(
external_id:
params
[
"user_id"
])
if
params
[
"user_id"
]
thread
.
author
=
author
thread
.
author
=
user
thread
.
save!
if
params
[
"auto_subscribe"
]
if
params
[
"auto_subscribe"
]
and
author
author
.
subscribe
(
thread
)
end
thread
.
to_hash
.
to_json
end
get
'/api/v1/threads/:thread_id'
do
|
thread_id
|
thread
=
CommentThread
.
find
(
thread_id
)
thread
.
to_hash
(
recursive:
params
[
"recursive"
]).
to_json
end
put
'/api/v1/threads/:thread_id'
do
|
thread_id
|
thread
=
CommentThread
.
find
(
thread_id
)
thread
.
update_attributes!
(
params
.
slice
(
*
%w[title body]
))
thread
.
to_hash
.
to_json
end
post
'/api/v1/threads/:thread_id/comments'
do
|
thread_id
|
thread
=
CommentThread
.
find
(
thread_id
)
comment
=
thread
.
comments
.
new
(
params
.
slice
(
*
%w[body course_id]
))
author
=
User
.
find_or_create_by
(
external_id:
params
[
"user_id"
])
if
params
[
"user_id"
]
comment
.
author
=
author
comment
.
author
=
user
comment
.
save!
if
params
[
"auto_subscribe"
]
if
params
[
"auto_subscribe"
]
and
author
author
.
subscribe
(
thread
)
end
comment
.
to_hash
.
to_json
end
delete
'/api/v1/threads/:thread_id'
do
|
thread_id
|
thread
=
CommentThread
.
find
(
thread_id
)
thread
.
destroy
thread
.
to_hash
.
to_json
end
get
'/api/v1/comments/:comment_id'
do
|
comment_id
|
comment
=
Comment
.
find
(
comment_id
)
comment
.
to_hash
(
recursive:
params
[
"recursive"
]).
to_json
end
put
'/api/v1/comments/:comment_id'
do
|
comment_id
|
comment
=
Comment
.
find
(
comment_id
)
comment
.
update_attributes!
(
params
.
slice
(
*
%w[body endorsed]
))
comment
.
to_hash
.
to_json
end
post
'/api/v1/comments/:comment_id'
do
|
comment_id
|
comment
=
Comment
.
find
(
comment_id
)
sub_comment
=
comment
.
children
.
new
(
params
.
slice
(
*
%w[body course_id]
))
sub_comment
.
author
=
User
.
find_or_create_by
(
external_id:
params
[
"user_id"
])
sub_comment
.
author
=
user
sub_comment
.
comment_thread
=
comment
.
comment_thread
sub_comment
.
save!
sub_comment
.
to_hash
.
to_json
end
delete
'/api/v1/comments/:comment_id'
do
|
comment_id
|
comment
=
Comment
.
find
(
comment_id
)
comment
.
destroy
comment
.
to_hash
.
to_json
end
put
'/api/v1/comments/:comment_id/votes'
do
|
comment_id
|
comment
=
Comment
.
find
(
comment_id
)
vote_for
comment
end
delete
'/api/v1/comments/:comment_id/votes'
do
|
comment_id
|
comment
=
Comment
.
find
(
comment_id
)
undo_vote_for
comment
end
put
'/api/v1/threads/:thread_id/votes'
do
|
thread_id
|
thread
=
CommentThread
.
find
(
thread_id
)
vote_for
thread
end
delete
'/api/v1/threads/:thread_id/votes'
do
|
thread_id
|
thread
=
CommentThread
.
find
(
thread_id
)
undo_vote_for
thread
end
get
'/api/v1/users/:user_id'
do
|
user_id
|
user
=
User
.
find_or_create_by
(
external_id:
user_id
)
user
.
to_hash
(
complete:
params
[
"complete"
]).
to_json
end
get
'/api/v1/users/:user_id/notifications'
do
|
user_id
|
user
=
User
.
find_or_create_by
(
external_id:
user_id
)
user
.
notifications
.
map
(
&
:to_hash
).
to_json
end
post
'/api/v1/users/:user_id/subscriptions'
do
|
user_id
|
user
=
User
.
find_or_create_by
(
external_id:
user_id
)
source
=
case
params
[
"source_type"
]
when
"user"
User
.
find_or_create_by
(
external_id:
params
[
"source_id"
])
when
"thread"
CommentThread
.
find
(
params
[
"source_id"
])
when
"other"
Commentable
.
find
(
params
[
"source_id"
])
end
user
.
subscribe
(
source
).
to_hash
.
to_json
end
delete
'/api/v1/users/:user_id/subscriptions'
do
|
user_id
|
user
=
User
.
find_or_create_by
(
external_id:
user_id
)
source
=
case
params
[
"source_type"
]
when
"user"
User
.
find_or_create_by
(
external_id:
params
[
"source_id"
])
when
"thread"
CommentThread
.
find
(
params
[
"source_id"
])
when
"other"
Commentable
.
find
(
params
[
"source_id"
])
end
user
.
unsubscribe
(
source
).
to_hash
.
to_json
end
...
...
@@ -180,14 +187,6 @@ if env.to_s == "development"
end
end
def
vote_for
(
obj
)
user
=
User
.
find_or_create_by
(
external_id:
params
[
"user_id"
])
user
.
vote
(
obj
,
params
[
"value"
].
to_sym
)
obj
.
reload
.
to_hash
.
to_json
end
def
undo_vote_for
(
obj
)
user
=
User
.
find_or_create_by
(
external_id:
params
[
"user_id"
])
user
.
unvote
(
obj
)
obj
.
reload
.
to_hash
.
to_json
error
BSON
::
InvalidObjectId
do
error
404
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment