mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
fix: create resource without some attributes (#2354)
This commit is contained in:
@@ -12,18 +12,36 @@ import (
|
||||
)
|
||||
|
||||
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 (?, ?, ?, ?, ?, ?, ?)"
|
||||
result, err := d.db.ExecContext(
|
||||
ctx,
|
||||
stmt,
|
||||
create.Filename,
|
||||
create.Blob,
|
||||
create.ExternalLink,
|
||||
create.Type,
|
||||
create.Size,
|
||||
create.CreatorID,
|
||||
create.InternalPath,
|
||||
)
|
||||
fields := []string{"`filename`", "`blob`", "`external_link`", "`type`", "`size`", "`creator_id`", "`internal_path`"}
|
||||
placeholder := []string{"?", "?", "?", "?", "?", "?", "?"}
|
||||
args := []any{create.Filename, create.Blob, create.ExternalLink, create.Type, create.Size, create.CreatorID, create.InternalPath}
|
||||
|
||||
if create.ID != 0 {
|
||||
fields = append(fields, "`id`")
|
||||
placeholder = append(placeholder, "?")
|
||||
args = append(args, create.ID)
|
||||
}
|
||||
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
|
Reference in New Issue
Block a user