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
cff0e869
Commit
cff0e869
authored
Sep 30, 2022
by
steven
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: add `memo_resource` model
parent
65f7aa79
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
213 additions
and
0 deletions
+213
-0
memo_resource.go
api/memo_resource.go
+24
-0
LATEST__SCHEMA.sql
store/db/migration/dev/LATEST__SCHEMA.sql
+1
-0
memo_resource.go
store/memo_resource.go
+188
-0
No files found.
api/memo_resource.go
0 → 100644
View file @
cff0e869
package
api
type
MemoResource
struct
{
MemoID
int
ResourceID
int
CreatedTs
int64
UpdatedTs
int64
}
type
MemoResourceUpsert
struct
{
MemoID
int
ResourceID
int
UpdatedTs
*
int64
}
type
MemoResourceFind
struct
{
MemoID
*
int
ResourceID
*
int
}
type
MemoResourceDelete
struct
{
MemoID
int
ResourceID
*
int
}
store/db/migration/dev/LATEST__SCHEMA.sql
View file @
cff0e869
...
@@ -164,6 +164,7 @@ CREATE TABLE memo_resource (
...
@@ -164,6 +164,7 @@ CREATE TABLE memo_resource (
memo_id
INTEGER
NOT
NULL
,
memo_id
INTEGER
NOT
NULL
,
resource_id
INTEGER
NOT
NULL
,
resource_id
INTEGER
NOT
NULL
,
created_ts
BIGINT
NOT
NULL
DEFAULT
(
strftime
(
'%s'
,
'now'
)),
created_ts
BIGINT
NOT
NULL
DEFAULT
(
strftime
(
'%s'
,
'now'
)),
updated_ts
BIGINT
NOT
NULL
DEFAULT
(
strftime
(
'%s'
,
'now'
)),
FOREIGN
KEY
(
memo_id
)
REFERENCES
memo
(
id
)
ON
DELETE
CASCADE
,
FOREIGN
KEY
(
memo_id
)
REFERENCES
memo
(
id
)
ON
DELETE
CASCADE
,
FOREIGN
KEY
(
resource_id
)
REFERENCES
resource
(
id
)
ON
DELETE
CASCADE
FOREIGN
KEY
(
resource_id
)
REFERENCES
resource
(
id
)
ON
DELETE
CASCADE
);
);
...
...
store/memo_resource.go
0 → 100644
View file @
cff0e869
package
store
import
(
"context"
"database/sql"
"fmt"
"strings"
"github.com/usememos/memos/api"
"github.com/usememos/memos/common"
)
// memoResourceRaw is the store model for an MemoResource.
// Fields have exactly the same meanings as MemoResource.
type
memoResourceRaw
struct
{
MemoID
int
ResourceID
int
CreatedTs
int64
UpdatedTs
int64
}
func
(
raw
*
memoResourceRaw
)
toMemoResource
()
*
api
.
MemoResource
{
return
&
api
.
MemoResource
{
MemoID
:
raw
.
MemoID
,
ResourceID
:
raw
.
ResourceID
,
CreatedTs
:
raw
.
CreatedTs
,
UpdatedTs
:
raw
.
UpdatedTs
,
}
}
func
(
s
*
Store
)
FindMemoResourceList
(
ctx
context
.
Context
,
find
*
api
.
MemoResourceFind
)
([]
*
api
.
MemoResource
,
error
)
{
tx
,
err
:=
s
.
db
.
BeginTx
(
ctx
,
nil
)
if
err
!=
nil
{
return
nil
,
FormatError
(
err
)
}
defer
tx
.
Rollback
()
memoResourceRawList
,
err
:=
findMemoResourceList
(
ctx
,
tx
,
find
)
if
err
!=
nil
{
return
nil
,
err
}
list
:=
[]
*
api
.
MemoResource
{}
for
_
,
raw
:=
range
memoResourceRawList
{
memoResource
:=
raw
.
toMemoResource
()
list
=
append
(
list
,
memoResource
)
}
return
list
,
nil
}
func
(
s
*
Store
)
UpsertMemoResource
(
ctx
context
.
Context
,
upsert
*
api
.
MemoResourceUpsert
)
(
*
api
.
MemoResource
,
error
)
{
tx
,
err
:=
s
.
db
.
BeginTx
(
ctx
,
nil
)
if
err
!=
nil
{
return
nil
,
FormatError
(
err
)
}
defer
tx
.
Rollback
()
memoResourceRaw
,
err
:=
upsertMemoResource
(
ctx
,
tx
,
upsert
)
if
err
!=
nil
{
return
nil
,
err
}
if
err
:=
tx
.
Commit
();
err
!=
nil
{
return
nil
,
FormatError
(
err
)
}
return
memoResourceRaw
.
toMemoResource
(),
nil
}
func
(
s
*
Store
)
DeleteMemoResource
(
ctx
context
.
Context
,
delete
*
api
.
MemoResourceDelete
)
error
{
tx
,
err
:=
s
.
db
.
BeginTx
(
ctx
,
nil
)
if
err
!=
nil
{
return
FormatError
(
err
)
}
defer
tx
.
Rollback
()
if
err
:=
deleteMemoResource
(
ctx
,
tx
,
delete
);
err
!=
nil
{
return
FormatError
(
err
)
}
if
err
:=
tx
.
Commit
();
err
!=
nil
{
return
FormatError
(
err
)
}
return
nil
}
func
findMemoResourceList
(
ctx
context
.
Context
,
tx
*
sql
.
Tx
,
find
*
api
.
MemoResourceFind
)
([]
*
memoResourceRaw
,
error
)
{
where
,
args
:=
[]
string
{
"1 = 1"
},
[]
interface
{}{}
if
v
:=
find
.
MemoID
;
v
!=
nil
{
where
,
args
=
append
(
where
,
"memo_id = ?"
),
append
(
args
,
*
v
)
}
if
v
:=
find
.
ResourceID
;
v
!=
nil
{
where
,
args
=
append
(
where
,
"resource_id = ?"
),
append
(
args
,
*
v
)
}
query
:=
`
SELECT
memo_id,
resource_id,
created_ts,
updated_ts
FROM memo_resource
WHERE `
+
strings
.
Join
(
where
,
" AND "
)
+
`
ORDER BY updated_ts DESC
`
rows
,
err
:=
tx
.
QueryContext
(
ctx
,
query
,
args
...
)
if
err
!=
nil
{
return
nil
,
FormatError
(
err
)
}
defer
rows
.
Close
()
memoResourceRawList
:=
make
([]
*
memoResourceRaw
,
0
)
for
rows
.
Next
()
{
var
memoResourceRaw
memoResourceRaw
if
err
:=
rows
.
Scan
(
&
memoResourceRaw
.
MemoID
,
&
memoResourceRaw
.
ResourceID
,
&
memoResourceRaw
.
CreatedTs
,
&
memoResourceRaw
.
UpdatedTs
,
);
err
!=
nil
{
return
nil
,
FormatError
(
err
)
}
memoResourceRawList
=
append
(
memoResourceRawList
,
&
memoResourceRaw
)
}
if
err
:=
rows
.
Err
();
err
!=
nil
{
return
nil
,
err
}
return
memoResourceRawList
,
nil
}
func
upsertMemoResource
(
ctx
context
.
Context
,
tx
*
sql
.
Tx
,
upsert
*
api
.
MemoResourceUpsert
)
(
*
memoResourceRaw
,
error
)
{
set
:=
[]
string
{
"memo_id"
,
"resource_id"
}
args
:=
[]
interface
{}{
upsert
.
MemoID
,
upsert
.
ResourceID
}
placeholder
:=
[]
string
{
"?"
,
"?"
}
if
v
:=
upsert
.
UpdatedTs
;
v
!=
nil
{
set
,
args
,
placeholder
=
append
(
set
,
"updated_ts"
),
append
(
args
,
v
),
append
(
placeholder
,
"?"
)
}
query
:=
`
INSERT INTO memo_resource (
`
+
strings
.
Join
(
set
,
", "
)
+
`
)
VALUES (`
+
strings
.
Join
(
placeholder
,
","
)
+
`)
ON CONFLICT(memo_id, resource_id) DO UPDATE
SET
updated_ts = EXCLUDED.updated_ts
RETURNING memo_id, resource_id, created_ts, updated_ts
`
var
memoResourceRaw
memoResourceRaw
if
err
:=
tx
.
QueryRowContext
(
ctx
,
query
,
args
...
)
.
Scan
(
&
memoResourceRaw
.
MemoID
,
&
memoResourceRaw
.
ResourceID
,
&
memoResourceRaw
.
CreatedTs
,
&
memoResourceRaw
.
UpdatedTs
,
);
err
!=
nil
{
return
nil
,
FormatError
(
err
)
}
return
&
memoResourceRaw
,
nil
}
func
deleteMemoResource
(
ctx
context
.
Context
,
tx
*
sql
.
Tx
,
delete
*
api
.
MemoResourceDelete
)
error
{
where
,
args
:=
[]
string
{
"memo_id = ?"
},
[]
interface
{}{
delete
.
MemoID
}
if
v
:=
delete
.
ResourceID
;
v
!=
nil
{
where
,
args
=
append
(
where
,
"resource_id = ?"
),
append
(
args
,
*
v
)
}
result
,
err
:=
tx
.
ExecContext
(
ctx
,
`
DELETE FROM memo WHERE `
+
strings
.
Join
(
where
,
" AND "
),
args
...
)
if
err
!=
nil
{
return
FormatError
(
err
)
}
rows
,
_
:=
result
.
RowsAffected
()
if
rows
==
0
{
return
&
common
.
Error
{
Code
:
common
.
NotFound
,
Err
:
fmt
.
Errorf
(
"memo resource not found"
)}
}
return
nil
}
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