feat: update template
This commit is contained in:
@@ -7,6 +7,7 @@ package sqlc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
const countFiles = `-- name: CountFiles :one
|
||||
@@ -24,7 +25,7 @@ func (q *Queries) CountFiles(ctx context.Context) (int64, error) {
|
||||
const createFile = `-- name: CreateFile :one
|
||||
INSERT INTO files(file_name, file_path, original_name, folder_name, mime_type, file_size, file_url)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
RETURNING id,file_url,file_name
|
||||
RETURNING id,file_url,file_name,file_size
|
||||
`
|
||||
|
||||
type CreateFileParams struct {
|
||||
@@ -41,6 +42,7 @@ type CreateFileRow struct {
|
||||
ID int32 `json:"id"`
|
||||
FileUrl string `json:"file_url"`
|
||||
FileName string `json:"file_name"`
|
||||
FileSize int64 `json:"file_size"`
|
||||
}
|
||||
|
||||
func (q *Queries) CreateFile(ctx context.Context, arg CreateFileParams) (CreateFileRow, error) {
|
||||
@@ -54,14 +56,29 @@ func (q *Queries) CreateFile(ctx context.Context, arg CreateFileParams) (CreateF
|
||||
arg.FileUrl,
|
||||
)
|
||||
var i CreateFileRow
|
||||
err := row.Scan(&i.ID, &i.FileUrl, &i.FileName)
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.FileUrl,
|
||||
&i.FileName,
|
||||
&i.FileSize,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const listFiles = `-- name: ListFiles :many
|
||||
SELECT id, file_name, file_path, file_url, original_name, folder_name, mime_type, file_size, created_at, updated_at
|
||||
FROM files
|
||||
ORDER BY id
|
||||
SELECT f.id, f.file_name, f.file_path, f.file_url, f.original_name, f.folder_name, f.mime_type, f.file_size, f.created_at, f.updated_at,
|
||||
CASE
|
||||
WHEN m.file_id IS NULL THEN NULL
|
||||
ELSE jsonb_build_object(
|
||||
'width', m.width,
|
||||
'height', m.height,
|
||||
'format', m.format
|
||||
)
|
||||
END AS metadata
|
||||
FROM files f
|
||||
LEFT JOIN file_image_metadata m
|
||||
ON f.id = m.file_id
|
||||
ORDER BY f.id DESC
|
||||
LIMIT $1 OFFSET $2
|
||||
`
|
||||
|
||||
@@ -70,15 +87,29 @@ type ListFilesParams struct {
|
||||
Offset int32 `json:"offset"`
|
||||
}
|
||||
|
||||
func (q *Queries) ListFiles(ctx context.Context, arg ListFilesParams) ([]File, error) {
|
||||
type ListFilesRow struct {
|
||||
ID int32 `json:"id"`
|
||||
FileName string `json:"file_name"`
|
||||
FilePath string `json:"file_path"`
|
||||
FileUrl string `json:"file_url"`
|
||||
OriginalName string `json:"original_name"`
|
||||
FolderName string `json:"folder_name"`
|
||||
MimeType string `json:"mime_type"`
|
||||
FileSize int64 `json:"file_size"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt *time.Time `json:"updated_at"`
|
||||
Metadata interface{} `json:"metadata"`
|
||||
}
|
||||
|
||||
func (q *Queries) ListFiles(ctx context.Context, arg ListFilesParams) ([]ListFilesRow, error) {
|
||||
rows, err := q.db.Query(ctx, listFiles, arg.Limit, arg.Offset)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []File{}
|
||||
items := []ListFilesRow{}
|
||||
for rows.Next() {
|
||||
var i File
|
||||
var i ListFilesRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.FileName,
|
||||
@@ -90,6 +121,7 @@ func (q *Queries) ListFiles(ctx context.Context, arg ListFilesParams) ([]File, e
|
||||
&i.FileSize,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.Metadata,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -100,3 +132,34 @@ func (q *Queries) ListFiles(ctx context.Context, arg ListFilesParams) ([]File, e
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listImageFiles = `-- name: ListImageFiles :many
|
||||
SELECT id, file_path
|
||||
FROM files
|
||||
WHERE mime_type LIKE 'image/%'
|
||||
`
|
||||
|
||||
type ListImageFilesRow struct {
|
||||
ID int32 `json:"id"`
|
||||
FilePath string `json:"file_path"`
|
||||
}
|
||||
|
||||
func (q *Queries) ListImageFiles(ctx context.Context) ([]ListImageFilesRow, error) {
|
||||
rows, err := q.db.Query(ctx, listImageFiles)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []ListImageFilesRow{}
|
||||
for rows.Next() {
|
||||
var i ListImageFilesRow
|
||||
if err := rows.Scan(&i.ID, &i.FilePath); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user