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
4c465bef
Commit
4c465bef
authored
Sep 30, 2022
by
steven
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: add memo resource apis
parent
3bde9543
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
82 additions
and
1 deletion
+82
-1
resource.go
api/resource.go
+1
-0
memo.go
server/memo.go
+77
-0
memo.go
store/memo.go
+1
-1
resource.go
store/resource.go
+3
-0
No files found.
api/resource.go
View file @
4c465bef
...
@@ -34,6 +34,7 @@ type ResourceFind struct {
...
@@ -34,6 +34,7 @@ type ResourceFind struct {
// Domain specific fields
// Domain specific fields
Filename
*
string
`json:"filename"`
Filename
*
string
`json:"filename"`
MemoID
*
int
}
}
type
ResourceDelete
struct
{
type
ResourceDelete
struct
{
...
...
server/memo.go
View file @
4c465bef
...
@@ -6,6 +6,7 @@ import (
...
@@ -6,6 +6,7 @@ import (
"net/http"
"net/http"
"strconv"
"strconv"
"strings"
"strings"
"time"
"github.com/usememos/memos/api"
"github.com/usememos/memos/api"
"github.com/usememos/memos/common"
"github.com/usememos/memos/common"
...
@@ -294,6 +295,82 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
...
@@ -294,6 +295,82 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
return
nil
return
nil
})
})
g
.
POST
(
"/memo/:memoId/resource"
,
func
(
c
echo
.
Context
)
error
{
ctx
:=
c
.
Request
()
.
Context
()
memoID
,
err
:=
strconv
.
Atoi
(
c
.
Param
(
"memoId"
))
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
fmt
.
Sprintf
(
"ID is not a number: %s"
,
c
.
Param
(
"memoId"
)))
.
SetInternal
(
err
)
}
currentTs
:=
time
.
Now
()
.
Unix
()
memoResourceUpsert
:=
&
api
.
MemoResourceUpsert
{
MemoID
:
memoID
,
UpdatedTs
:
&
currentTs
,
}
if
err
:=
json
.
NewDecoder
(
c
.
Request
()
.
Body
)
.
Decode
(
memoResourceUpsert
);
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Malformatted post memo resource request"
)
.
SetInternal
(
err
)
}
resourceFind
:=
&
api
.
ResourceFind
{
ID
:
&
memoResourceUpsert
.
ResourceID
,
}
resource
,
err
:=
s
.
Store
.
FindResource
(
ctx
,
resourceFind
)
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to fetch resource"
)
.
SetInternal
(
err
)
}
c
.
Response
()
.
Header
()
.
Set
(
echo
.
HeaderContentType
,
echo
.
MIMEApplicationJSONCharsetUTF8
)
if
err
:=
json
.
NewEncoder
(
c
.
Response
()
.
Writer
)
.
Encode
(
composeResponse
(
resource
));
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to encode resource response"
)
.
SetInternal
(
err
)
}
return
nil
})
g
.
GET
(
"/memo/:memoId/resource"
,
func
(
c
echo
.
Context
)
error
{
ctx
:=
c
.
Request
()
.
Context
()
memoID
,
err
:=
strconv
.
Atoi
(
c
.
Param
(
"memoId"
))
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
fmt
.
Sprintf
(
"ID is not a number: %s"
,
c
.
Param
(
"memoId"
)))
.
SetInternal
(
err
)
}
resourceFind
:=
&
api
.
ResourceFind
{
MemoID
:
&
memoID
,
}
resourceList
,
err
:=
s
.
Store
.
FindResourceList
(
ctx
,
resourceFind
)
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to fetch resource list"
)
.
SetInternal
(
err
)
}
c
.
Response
()
.
Header
()
.
Set
(
echo
.
HeaderContentType
,
echo
.
MIMEApplicationJSONCharsetUTF8
)
if
err
:=
json
.
NewEncoder
(
c
.
Response
()
.
Writer
)
.
Encode
(
composeResponse
(
resourceList
));
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to encode resource list response"
)
.
SetInternal
(
err
)
}
return
nil
})
g
.
DELETE
(
"/memo/:memoId/resource/:resourceId"
,
func
(
c
echo
.
Context
)
error
{
ctx
:=
c
.
Request
()
.
Context
()
memoID
,
err
:=
strconv
.
Atoi
(
c
.
Param
(
"memoId"
))
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
fmt
.
Sprintf
(
"Memo ID is not a number: %s"
,
c
.
Param
(
"memoId"
)))
.
SetInternal
(
err
)
}
resourceID
,
err
:=
strconv
.
Atoi
(
c
.
Param
(
"resourceId"
))
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
fmt
.
Sprintf
(
"Resource ID is not a number: %s"
,
c
.
Param
(
"resourceId"
)))
.
SetInternal
(
err
)
}
memoResourceDelete
:=
&
api
.
MemoResourceDelete
{
MemoID
:
memoID
,
ResourceID
:
&
resourceID
,
}
if
err
:=
s
.
Store
.
DeleteMemoResource
(
ctx
,
memoResourceDelete
);
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to fetch resource list"
)
.
SetInternal
(
err
)
}
return
c
.
JSON
(
http
.
StatusOK
,
true
)
})
g
.
DELETE
(
"/memo/:memoId"
,
func
(
c
echo
.
Context
)
error
{
g
.
DELETE
(
"/memo/:memoId"
,
func
(
c
echo
.
Context
)
error
{
ctx
:=
c
.
Request
()
.
Context
()
ctx
:=
c
.
Request
()
.
Context
()
userID
,
ok
:=
c
.
Get
(
getUserIDContextKey
())
.
(
int
)
userID
,
ok
:=
c
.
Get
(
getUserIDContextKey
())
.
(
int
)
...
...
store/memo.go
View file @
4c465bef
...
@@ -286,7 +286,7 @@ func findMemoRawList(ctx context.Context, tx *sql.Tx, find *api.MemoFind) ([]*me
...
@@ -286,7 +286,7 @@ func findMemoRawList(ctx context.Context, tx *sql.Tx, find *api.MemoFind) ([]*me
where
,
args
=
append
(
where
,
"row_status = ?"
),
append
(
args
,
*
v
)
where
,
args
=
append
(
where
,
"row_status = ?"
),
append
(
args
,
*
v
)
}
}
if
v
:=
find
.
Pinned
;
v
!=
nil
{
if
v
:=
find
.
Pinned
;
v
!=
nil
{
where
=
append
(
where
,
"id in (SELECT memo_id FROM memo_organizer WHERE pinned = 1 AND user_id = memo.creator_id
)"
)
where
=
append
(
where
,
"id in (SELECT memo_id FROM memo_organizer WHERE pinned = 1 AND user_id = memo.creator_id)"
)
}
}
if
v
:=
find
.
ContentSearch
;
v
!=
nil
{
if
v
:=
find
.
ContentSearch
;
v
!=
nil
{
where
,
args
=
append
(
where
,
"content LIKE ?"
),
append
(
args
,
"%"
+*
v
+
"%"
)
where
,
args
=
append
(
where
,
"content LIKE ?"
),
append
(
args
,
"%"
+*
v
+
"%"
)
...
...
store/resource.go
View file @
4c465bef
...
@@ -192,6 +192,9 @@ func findResourceList(ctx context.Context, tx *sql.Tx, find *api.ResourceFind) (
...
@@ -192,6 +192,9 @@ func findResourceList(ctx context.Context, tx *sql.Tx, find *api.ResourceFind) (
if
v
:=
find
.
Filename
;
v
!=
nil
{
if
v
:=
find
.
Filename
;
v
!=
nil
{
where
,
args
=
append
(
where
,
"filename = ?"
),
append
(
args
,
*
v
)
where
,
args
=
append
(
where
,
"filename = ?"
),
append
(
args
,
*
v
)
}
}
if
v
:=
find
.
MemoID
;
v
!=
nil
{
where
,
args
=
append
(
where
,
"id in (SELECT resource_id FROM memo_resource WHERE memo_id = ?)"
),
append
(
args
,
*
v
)
}
query
:=
`
query
:=
`
SELECT
SELECT
...
...
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