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
d89aa9ee
Commit
d89aa9ee
authored
Jul 16, 2012
by
Rocky Duan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
tests covered all apis & passed
parent
1d1ca019
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
55 additions
and
59 deletions
+55
-59
app.rb
+7
-39
models/user.rb
+3
-3
spec/app_spec.rb
+45
-17
No files found.
app.rb
View file @
d89aa9ee
...
...
@@ -20,38 +20,6 @@ Mongoid.logger.level = Logger::INFO
Dir
[
File
.
dirname
(
__FILE__
)
+
'/models/*.rb'
].
each
{
|
file
|
require
file
}
# DELETE /api/v1/commentables/:commentable_type/:commentable_id
# GET /api/v1/commentables/:commentable_type/:commentable_id/comment_threads
# POST /api/v1/commentables/:commentable_type/:commentable_id/comment_threads
#
# GET /api/v1/comment_threads/:comment_thread_id
# PUT /api/v1/comment_threads/:comment_thread_id
# POST /api/v1/comment_threads/:comment_thread_id/comments
# DELETE /api/v1/comment_threads/:comment_thread_id
#
# GET /api/v1/comments/:comment_id
# PUT /api/v1/comments/:comment_id
# POST /api/v1/comments/:comment_id
# DELETE /api/v1/comments/:comment_id
#
# PUT /api/v1/votes/comments/:comment_id/users/:user_id
# DELETE /api/v1/votes/comments/:comment_id/users/:user_id
#
# PUT /api/v1/votes/comment_threads/:comment_thread_id/users/:user_id
# DELETE /api/v1/votes/comment_threads/:comment_thread_id/users/:user_id
#
# GET /api/v1/users/:user_id/feeds
# POST /api/v1/users/:user_id/follow
# POST /api/v1/users/:user_id/unfollow
# POST /api/v1/users/:user_id/watch/commentable
# POST /api/v1/users/:user_id/unwatch/commentable
# POST /api/v1/users/:user_id/watch/comment_thread
# POST /api/v1/users/:user_id/unwatch/comment_thread
#
#
#
# DELETE /api/v1/commentables/:commentable_type/:commentable_id
# delete the commentable object and all of its associated comment threads and comments
delete
'/api/v1/commentables/:commentable_type/:commentable_id'
do
|
commentable_type
,
commentable_id
|
...
...
@@ -208,7 +176,7 @@ end
post
'/api/v1/users/:user_id/follow'
do
|
user_id
|
user
=
User
.
find_or_create_by
(
external_id:
user_id
)
followed_user
=
User
.
find_or_create_by
(
external_id:
params
[
:user_id
])
followed_user
=
User
.
find_or_create_by
(
external_id:
params
[
"follow_user_id"
])
user
.
follow
(
followed_user
)
user
.
to_hash
.
to_json
end
...
...
@@ -218,7 +186,7 @@ end
post
'/api/v1/users/:user_id/unfollow'
do
|
user_id
|
user
=
User
.
find_or_create_by
(
external_id:
user_id
)
followed_user
=
User
.
find_or_create_by
(
external_id:
params
[
:user_id
])
followed_user
=
User
.
find_or_create_by
(
external_id:
params
[
"follow_user_id"
])
user
.
unfollow
(
followed_user
)
user
.
to_hash
.
to_json
end
...
...
@@ -229,7 +197,7 @@ end
post
'/api/v1/users/:user_id/watch/commentable'
do
|
user_id
|
user
=
User
.
find_or_create_by
(
external_id:
user_id
)
commentable
=
Commentable
.
find_or_create_by
(
commentable_type:
params
[
:commentable_type
],
commentable_id:
para
sm
[
:commentable_id
])
commentable_id:
para
ms
[
:commentable_id
])
user
.
watch_commentable
(
commentable
)
user
.
to_hash
.
to_json
end
...
...
@@ -239,8 +207,8 @@ end
post
'/api/v1/users/:user_id/unwatch/commentable'
do
|
user_id
|
user
=
User
.
find_or_create_by
(
external_id:
user_id
)
commentable
=
Commentable
.
find_or_create_by
(
commentable_type:
params
[
:commentable_type
],
commentable_id:
para
sm
[
:commentable_id
])
commentable
=
Commentable
.
find_or_create_by
(
commentable_type:
params
[
"commentable_type"
],
commentable_id:
para
ms
[
"commentable_id"
])
user
.
unwatch_commentable
(
commentable
)
user
.
to_hash
.
to_json
end
...
...
@@ -250,7 +218,7 @@ end
post
'/api/v1/users/:user_id/watch/comment_thread'
do
|
user_id
|
user
=
User
.
find_or_create_by
(
external_id:
user_id
)
comment_thread
=
CommentThread
.
find
(
params
[
:comment_thread_id
])
comment_thread
=
CommentThread
.
find
(
params
[
"comment_thread_id"
])
user
.
watch_comment_thread
(
comment_thread
)
user
.
to_hash
.
to_json
end
...
...
@@ -260,7 +228,7 @@ end
post
'/api/v1/users/:user_id/unwatch/comment_thread'
do
|
user_id
|
user
=
User
.
find_or_create_by
(
external_id:
user_id
)
comment_thread
=
CommentThread
.
find
(
params
[
:comment_thread_id
])
comment_thread
=
CommentThread
.
find
(
params
[
"comment_thread_id"
])
user
.
unwatch_comment_thread
(
comment_thread
)
user
.
to_hash
.
to_json
end
...
...
models/user.rb
View file @
d89aa9ee
...
...
@@ -19,13 +19,13 @@ class User
end
def
follow
(
user
)
if
id
!=
user
.
id
and
not
following
.
include?
user
following
<<
user
if
id
!=
user
.
id
and
not
following
s
.
include?
user
following
s
<<
user
end
end
def
unfollow
(
user
)
following
.
delete
(
user
)
following
s
.
delete
(
user
)
end
def
self
.
watching
(
class_plural_sym
)
...
...
spec/app_spec.rb
View file @
d89aa9ee
...
...
@@ -82,8 +82,7 @@ def init_with_feeds
user1
=
User
.
create!
(
external_id:
"1"
)
user2
=
User
.
create!
(
external_id:
"2"
)
user1
.
followers
<<
user2
user1
.
save!
user2
.
followings
<<
user1
commentable
=
Commentable
.
new
(
commentable_type:
"questions"
,
commentable_id:
"1"
)
commentable
.
watchers
<<
[
user1
,
user2
]
...
...
@@ -105,12 +104,9 @@ def init_with_feeds
comment2
.
save!
comment_thread
=
commentable
.
comment_threads
.
new
(
title:
"This problem is wrong"
,
body:
"it is unsolvable"
,
course_id:
"2"
)
comment_thread
.
author
=
user
1
comment_thread
.
author
=
user
2
comment_thread
.
save!
user1
.
save!
user2
.
save!
end
describe
"app"
do
...
...
@@ -338,7 +334,7 @@ describe "app" do
describe
"feeds"
do
before
(
:each
)
{
init_with_feeds
}
describe
"GET /api/v1/users/:user_id/feeds"
do
it
"get all subscribed feeds for the user"
do
it
"get all subscribed feeds
on the watched comment threads
for the user"
do
user
=
User
.
find
(
"1"
)
get
"/api/v1/users/
#{
user
.
external_id
}
/feeds"
last_response
.
should
be_ok
...
...
@@ -349,41 +345,73 @@ describe "app" do
feed_so_easy
.
should_not
be_nil
feed_not_for_me_neither
=
feeds
.
select
{
|
f
|
f
[
"feed_type"
]
==
"post_reply"
and
f
[
"info"
][
"comment_id"
]
==
not_for_me_neither
.
id
.
to_s
}.
first
feed_not_for_me_neither
.
should_not
be_nil
feeds
.
each
do
|
feed
|
if
feed
[
"feed_type"
]
==
"post_reply"
feed
[
"info"
][
"comment_body"
]
=
Comment
.
find
(
feed
[
"info"
][
"comment_id"
]).
body
end
end
it
"get all subscribed feeds on the watched commentable for the user"
do
user
=
User
.
find
(
"1"
)
get
"/api/v1/users/
#{
user
.
external_id
}
/feeds"
last_response
.
should
be_ok
feeds
=
parse
last_response
.
body
feeds
.
select
{
|
f
|
f
[
"feed_type"
]
==
"post_topic"
}.
length
.
should
==
1
problem_wrong
=
feeds
.
select
{
|
f
|
f
[
"feed_type"
]
==
"post_topic"
}.
first
problem_wrong
[
"info"
][
"comment_thread_title"
].
should
==
"This problem is wrong"
end
end
describe
"POST /api/v1/users/:user_id/follow"
do
it
"follow user"
do
user1
=
User
.
find
(
"1"
)
user2
=
User
.
find
(
"2"
)
post
"/api/v1/users/
#{
user1
.
external_id
}
/follow"
,
follow_user_id:
user2
.
external_id
last_response
.
should
be_ok
User
.
find
(
"1"
).
followers
.
length
.
should
==
1
User
.
find
(
"1"
).
followers
.
should
include
user2
end
end
describe
"POST /api/v1/users/:user_id/unfollow"
do
it
"unfollow user"
do
user1
=
User
.
find
(
"1"
)
user2
=
User
.
find
(
"2"
)
post
"/api/v1/users/
#{
user2
.
external_id
}
/unfollow"
,
follow_user_id:
user1
.
external_id
last_response
.
should
be_ok
User
.
find
(
"1"
).
followers
.
length
.
should
==
0
end
end
describe
"POST /api/v1/users/:user_id/watch/commentable"
do
it
"watch a commentable"
do
user3
=
User
.
find_or_create_by
(
external_id:
"3"
)
post
"/api/v1/users/
#{
user3
.
external_id
}
/watch/commentable"
,
commentable_type:
"questions"
,
commentable_id:
"1"
last_response
.
should
be_ok
Commentable
.
first
.
watchers
.
length
.
should
==
3
Commentable
.
first
.
watchers
.
should
include
user3
end
end
describe
"POST /api/v1/users/:user_id/unwatch/commentable"
do
it
"unwatch a commentable"
do
user2
=
User
.
find_or_create_by
(
external_id:
"2"
)
post
"/api/v1/users/
#{
user2
.
external_id
}
/unwatch/commentable"
,
commentable_type:
"questions"
,
commentable_id:
"1"
last_response
.
should
be_ok
Commentable
.
first
.
watchers
.
length
.
should
==
1
Commentable
.
first
.
watchers
.
should_not
include
user2
end
end
describe
"POST /api/v1/users/:user_id/watch/comment_thread"
do
it
"watch a comment thread"
do
user1
=
User
.
find_or_create_by
(
external_id:
"1"
)
comment_thread
=
CommentThread
.
where
(
body:
"it is unsolvable"
).
first
post
"/api/v1/users/
#{
user1
.
external_id
}
/watch/comment_thread"
,
comment_thread_id:
comment_thread
.
id
last_response
.
should
be_ok
comment_thread
=
CommentThread
.
where
(
body:
"it is unsolvable"
).
first
comment_thread
.
watchers
.
length
.
should
==
2
comment_thread
.
watchers
.
should
include
user1
end
end
describe
"POST /api/v1/users/:user_id/unwatch/comment_thread"
do
it
"unwatch a comment thread"
do
user2
=
User
.
find_or_create_by
(
external_id:
"2"
)
comment_thread
=
CommentThread
.
where
(
body:
"it is unsolvable"
).
first
post
"/api/v1/users/
#{
user2
.
external_id
}
/unwatch/comment_thread"
,
comment_thread_id:
comment_thread
.
id
last_response
.
should
be_ok
comment_thread
=
CommentThread
.
where
(
body:
"it is unsolvable"
).
first
comment_thread
.
watchers
.
length
.
should
==
0
end
end
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