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
e5471923
Commit
e5471923
authored
Aug 07, 2012
by
Rocky Duan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
turn to last page when page exceeds max page
parent
1d45e28b
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
36 additions
and
15 deletions
+36
-15
app.rb
+18
-15
models/comment_thread.rb
+18
-0
No files found.
app.rb
View file @
e5471923
...
...
@@ -29,6 +29,7 @@ Mongoid.instantiate_observers
api_prefix
=
CommentService
::
API_PREFIX
get
"
#{
api_prefix
}
/search/threads"
do
sort_key_mapper
=
{
...
...
@@ -45,6 +46,7 @@ get "#{api_prefix}/search/threads" do
sort_key
=
sort_key_mapper
[
params
[
"sort_key"
]]
sort_order
=
sort_order_mapper
[
params
[
"sort_order"
]]
sort_keyword_valid
=
(
!
params
[
"sort_key"
]
&&
!
params
[
"sort_order"
]
||
sort_key
&&
sort_order
)
if
(
!
params
[
"text"
]
&&
!
params
[
"tags"
])
||
!
sort_keyword_valid
...
...
@@ -52,33 +54,34 @@ get "#{api_prefix}/search/threads" do
else
page
=
(
params
[
"page"
]
||
1
).
to_i
per_page
=
(
params
[
"per_page"
]
||
20
).
to_i
tags
=
params
[
"tags"
].
split
/,/
if
params
[
"tags"
]
search
=
CommentThread
.
tire
.
search
page:
page
,
per_page:
per_page
do
|
search
|
if
params
[
"text"
]
search
.
query
do
|
query
|
query
.
text
(
:_all
,
params
[
"text"
])
end
search
.
highlight
({
title:
{
number_of_fragments:
0
}
}
,
{
body:
{
number_of_fragments:
0
}
},
options:
{
tag:
"<highlight>"
})
end
search
.
filter
:bool
,
:must
=>
tags
.
map
{
|
tag
|
{
:term
=>
{
:tags_array
=>
tag
}
}
}
if
params
[
"tags"
]
options
=
{
sort_key:
sort_key
,
sort_order:
sort_order
,
page:
page
,
per_page:
per_page
,
}
search
.
filter
(
:term
,
commentable_id:
params
[
"commentable_id"
])
if
params
[
"commentable_id"
]
search
.
filter
(
:term
,
course_id:
params
[
"course_id"
])
if
params
[
"course_id"
]
search
.
sort
{
|
sort
|
sort
.
by
sort_key
,
sort_order
}
if
sort_key
&&
sort_order
#TODO should have search option 'auto sort or sth'
results
=
CommentThread
.
perform_search
(
params
,
options
).
results
if
page
>
results
.
total_pages
#TODO find a better way for this
results
=
CommentThread
.
perform_search
(
params
,
options
.
merge
(
page:
results
.
total_pages
)).
results
end
num_pages
=
search
.
total_pages
num_pages
=
results
.
total_pages
page
=
[
num_pages
,
[
1
,
page
].
max
].
min
{
collection:
search
.
results
.
map
{
|
t
|
CommentThread
.
search_result_to_hash
(
t
,
recursive:
bool_recursive
)},
collection:
results
.
map
{
|
t
|
CommentThread
.
search_result_to_hash
(
t
,
recursive:
bool_recursive
)},
num_pages:
num_pages
,
page:
page
,
}.
to_json
end
end
get
"
#{
api_prefix
}
/search/threads/more_like_this"
do
CommentThread
.
tire
.
search
{
query
{
more_like_this
'mollitia consequatur'
,
fields:
[
"title"
,
"body"
],
min_doc_freq:
1
,
min_term_freq:
1
}
}
end
delete
"
#{
api_prefix
}
/:commentable_id/threads"
do
|
commentable_id
|
commentable
.
comment_threads
.
destroy_all
{}.
to_json
...
...
models/comment_thread.rb
View file @
e5471923
...
...
@@ -76,6 +76,24 @@ class CommentThread < Content
find
(
result
.
id
).
to_hash
(
params
).
merge
(
highlighted_body:
highlighted_body
,
highlighted_title:
highlighted_title
)
end
def
self
.
perform_search
(
params
,
options
=
{})
page
=
[
1
,
options
[
:page
]
||
1
].
max
per_page
=
options
[
:per_page
]
||
20
sort_key
=
options
[
:sort_key
]
sort_order
=
options
[
:sort_order
]
search
=
Tire
::
Search
::
Search
.
new
'comment_threads'
search
.
query
{
|
query
|
query
.
text
:_all
,
params
[
"text"
]}
if
params
[
"text"
]
search
.
highlight
({
title:
{
number_of_fragments:
0
}
}
,
{
body:
{
number_of_fragments:
0
}
},
options:
{
tag:
"<highlight>"
})
search
.
filter
(
:bool
,
:must
=>
params
[
"tags"
].
split
(
/,/
).
map
{
|
tag
|
{
:term
=>
{
:tags_array
=>
tag
}
}
})
if
params
[
"tags"
]
search
.
filter
(
:term
,
commentable_id:
params
[
"commentable_id"
])
if
params
[
"commentable_id"
]
search
.
filter
(
:term
,
course_id:
params
[
"course_id"
])
if
params
[
"course_id"
]
search
.
sort
{
|
sort
|
sort
.
by
sort_key
,
sort_order
}
if
sort_key
&&
sort_order
#TODO should have search option 'auto sort or sth'
search
.
size
per_page
search
.
from
per_page
*
(
page
-
1
)
search
end
def
root_comments
Comment
.
roots
.
where
(
comment_thread_id:
self
.
id
)
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