Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
canifa_note
Project
Project
Details
Activity
Releases
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
Vũ Hoàng Anh
canifa_note
Commits
bb10bb20
Commit
bb10bb20
authored
Mar 30, 2024
by
Steven
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: implement search random memos
parent
03c93785
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
36 additions
and
9 deletions
+36
-9
memo_service.go
server/route/api/v2/memo_service.go
+20
-3
memo.go
store/db/mysql/memo.go
+3
-0
memo.go
store/db/postgres/memo.go
+3
-0
memo.go
store/db/sqlite/memo.go
+9
-6
memo.go
store/memo.go
+1
-0
No files found.
server/route/api/v2/memo_service.go
View file @
bb10bb20
...
...
@@ -134,16 +134,17 @@ func (s *APIV2Service) ListMemos(ctx context.Context, request *apiv2pb.ListMemos
}
func
(
s
*
APIV2Service
)
SearchMemos
(
ctx
context
.
Context
,
request
*
apiv2pb
.
SearchMemosRequest
)
(
*
apiv2pb
.
SearchMemosResponse
,
error
)
{
defaultSearchLimit
:=
10
memoFind
:=
&
store
.
FindMemo
{
// Exclude comments by default.
ExcludeComments
:
true
,
Limit
:
&
defaultSearchLimit
,
}
if
err
:=
s
.
buildMemoFindWithFilter
(
ctx
,
memoFind
,
request
.
Filter
);
err
!=
nil
{
err
:=
s
.
buildMemoFindWithFilter
(
ctx
,
memoFind
,
request
.
Filter
)
if
err
!=
nil
{
return
nil
,
status
.
Errorf
(
codes
.
InvalidArgument
,
"failed to build find memos with filter"
)
}
defaultSearchLimit
:=
10
memoFind
.
Limit
=
&
defaultSearchLimit
memos
,
err
:=
s
.
Store
.
ListMemos
(
ctx
,
memoFind
)
if
err
!=
nil
{
return
nil
,
status
.
Errorf
(
codes
.
Internal
,
"failed to search memos"
)
...
...
@@ -696,6 +697,12 @@ func (s *APIV2Service) buildMemoFindWithFilter(ctx context.Context, find *store.
if
filter
.
RowStatus
!=
nil
{
find
.
RowStatus
=
filter
.
RowStatus
}
if
filter
.
Random
{
find
.
Random
=
filter
.
Random
}
if
filter
.
Limit
!=
nil
{
find
.
Limit
=
filter
.
Limit
}
}
// If the user is not authenticated, only public memos are visible.
...
...
@@ -730,6 +737,8 @@ var SearchMemosFilterCELAttributes = []cel.EnvOption{
cel
.
Variable
(
"creator"
,
cel
.
StringType
),
cel
.
Variable
(
"uid"
,
cel
.
StringType
),
cel
.
Variable
(
"row_status"
,
cel
.
StringType
),
cel
.
Variable
(
"random"
,
cel
.
BoolType
),
cel
.
Variable
(
"limit"
,
cel
.
IntType
),
}
type
SearchMemosFilter
struct
{
...
...
@@ -741,6 +750,8 @@ type SearchMemosFilter struct {
Creator
*
string
UID
*
string
RowStatus
*
store
.
RowStatus
Random
bool
Limit
*
int
}
func
parseSearchMemosFilter
(
expression
string
)
(
*
SearchMemosFilter
,
error
)
{
...
...
@@ -798,6 +809,12 @@ func findSearchMemosField(callExpr *expr.Expr_Call, filter *SearchMemosFilter) {
}
else
if
idExpr
.
Name
==
"row_status"
{
rowStatus
:=
store
.
RowStatus
(
callExpr
.
Args
[
1
]
.
GetConstExpr
()
.
GetStringValue
())
filter
.
RowStatus
=
&
rowStatus
}
else
if
idExpr
.
Name
==
"random"
{
value
:=
callExpr
.
Args
[
1
]
.
GetConstExpr
()
.
GetBoolValue
()
filter
.
Random
=
value
}
else
if
idExpr
.
Name
==
"limit"
{
limit
:=
int
(
callExpr
.
Args
[
1
]
.
GetConstExpr
()
.
GetInt64Value
())
filter
.
Limit
=
&
limit
}
return
}
...
...
store/db/mysql/memo.go
View file @
bb10bb20
...
...
@@ -91,6 +91,9 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
orders
=
append
(
orders
,
"`created_ts` DESC"
)
}
orders
=
append
(
orders
,
"`id` DESC"
)
if
find
.
Random
{
orders
=
append
(
orders
,
"RAND()"
)
}
fields
:=
[]
string
{
"`memo`.`id` AS `id`"
,
...
...
store/db/postgres/memo.go
View file @
bb10bb20
...
...
@@ -82,6 +82,9 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
orders
=
append
(
orders
,
"created_ts DESC"
)
}
orders
=
append
(
orders
,
"id DESC"
)
if
find
.
Random
{
orders
=
append
(
orders
,
"RAND()"
)
}
fields
:=
[]
string
{
`memo.id AS id`
,
...
...
store/db/sqlite/memo.go
View file @
bb10bb20
...
...
@@ -71,16 +71,19 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
where
=
append
(
where
,
"`parent_id` IS NULL"
)
}
order
s
:=
[]
string
{}
order
By
:=
[]
string
{}
if
find
.
OrderByPinned
{
order
s
=
append
(
orders
,
"`pinned` DESC"
)
order
By
=
append
(
orderBy
,
"`pinned` DESC"
)
}
if
find
.
OrderByUpdatedTs
{
order
s
=
append
(
orders
,
"`updated_ts` DESC"
)
order
By
=
append
(
orderBy
,
"`updated_ts` DESC"
)
}
else
{
orders
=
append
(
orders
,
"`created_ts` DESC"
)
orderBy
=
append
(
orderBy
,
"`created_ts` DESC"
)
}
orderBy
=
append
(
orderBy
,
"`id` DESC"
)
if
find
.
Random
{
orderBy
=
[]
string
{
"RANDOM()"
}
}
orders
=
append
(
orders
,
"`id` DESC"
)
fields
:=
[]
string
{
"`memo`.`id` AS `id`"
,
...
...
@@ -101,7 +104,7 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
"LEFT JOIN `memo_organizer` ON `memo`.`id` = `memo_organizer`.`memo_id` AND `memo`.`creator_id` = `memo_organizer`.`user_id` "
+
"LEFT JOIN `memo_relation` ON `memo`.`id` = `memo_relation`.`memo_id` AND `memo_relation`.`type` =
\"
COMMENT
\"
"
+
"WHERE "
+
strings
.
Join
(
where
,
" AND "
)
+
" "
+
"ORDER BY "
+
strings
.
Join
(
order
s
,
", "
)
"ORDER BY "
+
strings
.
Join
(
order
By
,
", "
)
if
find
.
Limit
!=
nil
{
query
=
fmt
.
Sprintf
(
"%s LIMIT %d"
,
query
,
*
find
.
Limit
)
if
find
.
Offset
!=
nil
{
...
...
store/memo.go
View file @
bb10bb20
...
...
@@ -69,6 +69,7 @@ type FindMemo struct {
VisibilityList
[]
Visibility
ExcludeContent
bool
ExcludeComments
bool
Random
bool
// Pagination
Limit
*
int
...
...
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