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
1fbd568d
Unverified
Commit
1fbd568d
authored
Oct 08, 2023
by
Athurg Gooth
Committed by
GitHub
Oct 08, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: create resource without some attributes (#2354)
parent
c0619ef4
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
60 additions
and
36 deletions
+60
-36
resource.go
store/db/mysql/resource.go
+30
-12
resource.go
store/db/sqlite/resource.go
+30
-24
No files found.
store/db/mysql/resource.go
View file @
1fbd568d
...
@@ -12,18 +12,36 @@ import (
...
@@ -12,18 +12,36 @@ import (
)
)
func
(
d
*
DB
)
CreateResource
(
ctx
context
.
Context
,
create
*
store
.
Resource
)
(
*
store
.
Resource
,
error
)
{
func
(
d
*
DB
)
CreateResource
(
ctx
context
.
Context
,
create
*
store
.
Resource
)
(
*
store
.
Resource
,
error
)
{
stmt
:=
"INSERT INTO `resource` (`filename`, `blob`, `external_link`, `type`, `size`, `creator_id`, `internal_path`) VALUES (?, ?, ?, ?, ?, ?, ?)"
fields
:=
[]
string
{
"`filename`"
,
"`blob`"
,
"`external_link`"
,
"`type`"
,
"`size`"
,
"`creator_id`"
,
"`internal_path`"
}
result
,
err
:=
d
.
db
.
ExecContext
(
placeholder
:=
[]
string
{
"?"
,
"?"
,
"?"
,
"?"
,
"?"
,
"?"
,
"?"
}
ctx
,
args
:=
[]
any
{
create
.
Filename
,
create
.
Blob
,
create
.
ExternalLink
,
create
.
Type
,
create
.
Size
,
create
.
CreatorID
,
create
.
InternalPath
}
stmt
,
create
.
Filename
,
if
create
.
ID
!=
0
{
create
.
Blob
,
fields
=
append
(
fields
,
"`id`"
)
create
.
ExternalLink
,
placeholder
=
append
(
placeholder
,
"?"
)
create
.
Type
,
args
=
append
(
args
,
create
.
ID
)
create
.
Size
,
}
create
.
CreatorID
,
create
.
InternalPath
,
if
create
.
CreatedTs
!=
0
{
)
fields
=
append
(
fields
,
"`created_ts`"
)
placeholder
=
append
(
placeholder
,
"FROM_UNIXTIME(?)"
)
args
=
append
(
args
,
create
.
CreatedTs
)
}
if
create
.
UpdatedTs
!=
0
{
fields
=
append
(
fields
,
"`updated_ts`"
)
placeholder
=
append
(
placeholder
,
"FROM_UNIXTIME(?)"
)
args
=
append
(
args
,
create
.
UpdatedTs
)
}
if
create
.
MemoID
!=
nil
{
fields
=
append
(
fields
,
"`memo_id`"
)
placeholder
=
append
(
placeholder
,
"?"
)
args
=
append
(
args
,
*
create
.
MemoID
)
}
stmt
:=
"INSERT INTO `resource` ("
+
strings
.
Join
(
fields
,
", "
)
+
") VALUES ("
+
strings
.
Join
(
placeholder
,
", "
)
+
")"
result
,
err
:=
d
.
db
.
ExecContext
(
ctx
,
stmt
,
args
...
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
...
...
store/db/sqlite/resource.go
View file @
1fbd568d
...
@@ -10,30 +10,36 @@ import (
...
@@ -10,30 +10,36 @@ import (
)
)
func
(
d
*
DB
)
CreateResource
(
ctx
context
.
Context
,
create
*
store
.
Resource
)
(
*
store
.
Resource
,
error
)
{
func
(
d
*
DB
)
CreateResource
(
ctx
context
.
Context
,
create
*
store
.
Resource
)
(
*
store
.
Resource
,
error
)
{
stmt
:=
`
fields
:=
[]
string
{
"`filename`"
,
"`blob`"
,
"`external_link`"
,
"`type`"
,
"`size`"
,
"`creator_id`"
,
"`internal_path`"
}
INSERT INTO resource (
placeholder
:=
[]
string
{
"?"
,
"?"
,
"?"
,
"?"
,
"?"
,
"?"
,
"?"
}
filename,
args
:=
[]
any
{
create
.
Filename
,
create
.
Blob
,
create
.
ExternalLink
,
create
.
Type
,
create
.
Size
,
create
.
CreatorID
,
create
.
InternalPath
}
blob,
external_link,
if
create
.
ID
!=
0
{
type,
fields
=
append
(
fields
,
"`id`"
)
size,
placeholder
=
append
(
placeholder
,
"?"
)
creator_id,
args
=
append
(
args
,
create
.
ID
)
internal_path
}
)
VALUES (?, ?, ?, ?, ?, ?, ?)
if
create
.
CreatedTs
!=
0
{
RETURNING id, created_ts, updated_ts
fields
=
append
(
fields
,
"`created_ts`"
)
`
placeholder
=
append
(
placeholder
,
"?"
)
if
err
:=
d
.
db
.
QueryRowContext
(
args
=
append
(
args
,
create
.
CreatedTs
)
ctx
,
}
stmt
,
create
.
Filename
,
if
create
.
UpdatedTs
!=
0
{
create
.
Blob
,
fields
=
append
(
fields
,
"`updated_ts`"
)
create
.
ExternalLink
,
placeholder
=
append
(
placeholder
,
"?"
)
create
.
Type
,
args
=
append
(
args
,
create
.
UpdatedTs
)
create
.
Size
,
}
create
.
CreatorID
,
create
.
InternalPath
,
if
create
.
MemoID
!=
nil
{
)
.
Scan
(
&
create
.
ID
,
&
create
.
CreatedTs
,
&
create
.
UpdatedTs
);
err
!=
nil
{
fields
=
append
(
fields
,
"`memo_id`"
)
placeholder
=
append
(
placeholder
,
"?"
)
args
=
append
(
args
,
*
create
.
MemoID
)
}
stmt
:=
"INSERT INTO `resource` ("
+
strings
.
Join
(
fields
,
", "
)
+
") VALUES ("
+
strings
.
Join
(
placeholder
,
", "
)
+
") RETURNING `id`, `created_ts`, `updated_ts`"
if
err
:=
d
.
db
.
QueryRowContext
(
ctx
,
stmt
,
args
...
)
.
Scan
(
&
create
.
ID
,
&
create
.
CreatedTs
,
&
create
.
UpdatedTs
);
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
...
...
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