init
This commit is contained in:
commit
d69342b2e9
160 changed files with 28681 additions and 0 deletions
80
internal/tenant/assets.go
Normal file
80
internal/tenant/assets.go
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
package tenant
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func (q *Queries) ListAssets(ctx context.Context) ([]Asset, error) {
|
||||
rows, err := q.db.QueryContext(ctx, `SELECT id, filename, r2_key, content_type, size, width, height, created_at
|
||||
FROM assets ORDER BY created_at DESC`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var assets []Asset
|
||||
for rows.Next() {
|
||||
a, err := scanAsset(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
assets = append(assets, a)
|
||||
}
|
||||
return assets, rows.Err()
|
||||
}
|
||||
|
||||
func (q *Queries) GetAsset(ctx context.Context, id string) (*Asset, error) {
|
||||
row := q.db.QueryRowContext(ctx, `SELECT id, filename, r2_key, content_type, size, width, height, created_at
|
||||
FROM assets WHERE id = ?`, id)
|
||||
|
||||
a, err := scanAsset(row)
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &a, nil
|
||||
}
|
||||
|
||||
func (q *Queries) CreateAsset(ctx context.Context, a *Asset) error {
|
||||
if a.ID == "" {
|
||||
a.ID = uuid.NewString()
|
||||
}
|
||||
now := time.Now().UTC().Format(time.RFC3339)
|
||||
_, err := q.db.ExecContext(ctx, `INSERT INTO assets (id, filename, r2_key, content_type, size, width, height, created_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
a.ID, a.Filename, a.R2Key, nullStr(a.ContentType), nullInt64(a.Size), nullInt64(int64(a.Width)), nullInt64(int64(a.Height)), now)
|
||||
return err
|
||||
}
|
||||
|
||||
func (q *Queries) DeleteAsset(ctx context.Context, id string) error {
|
||||
_, err := q.db.ExecContext(ctx, `DELETE FROM assets WHERE id = ?`, id)
|
||||
return err
|
||||
}
|
||||
|
||||
func scanAsset(s scanner) (Asset, error) {
|
||||
var a Asset
|
||||
var contentType, createdAt sql.NullString
|
||||
var size, width, height sql.NullInt64
|
||||
|
||||
err := s.Scan(&a.ID, &a.Filename, &a.R2Key, &contentType, &size, &width, &height, &createdAt)
|
||||
if err != nil {
|
||||
return a, err
|
||||
}
|
||||
|
||||
a.ContentType = contentType.String
|
||||
a.Size = size.Int64
|
||||
a.Width = int(width.Int64)
|
||||
a.Height = int(height.Int64)
|
||||
a.CreatedAt = parseTime(createdAt.String)
|
||||
return a, nil
|
||||
}
|
||||
|
||||
func nullInt64(v int64) sql.NullInt64 {
|
||||
return sql.NullInt64{Int64: v, Valid: v != 0}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue