package store import ( "context" "time" "github.com/google/uuid" ) type Image struct { ID uuid.UUID Filename string ContentType string Size int Data []byte // only populated by ImageData CreatedAt time.Time } func (bs *BlogStore) CreateImage(ctx context.Context, filename, contentType string, data []byte) (*Image, error) { img := &Image{ID: uuid.New(), Filename: filename, ContentType: contentType, Size: len(data)} err := bs.db.QueryRow(ctx, `INSERT INTO images (id, filename, content_type, size, data) VALUES ($1,$2,$3,$4,$5) RETURNING created_at`, img.ID, filename, contentType, len(data), data).Scan(&img.CreatedAt) return img, err } func (bs *BlogStore) ListImages(ctx context.Context) ([]Image, error) { rows, err := bs.db.Query(ctx, `SELECT id, filename, content_type, size, created_at FROM images ORDER BY created_at DESC`) if err != nil { return nil, err } defer rows.Close() var out []Image for rows.Next() { var i Image if err := rows.Scan(&i.ID, &i.Filename, &i.ContentType, &i.Size, &i.CreatedAt); err != nil { return nil, err } out = append(out, i) } return out, rows.Err() } // ImageData loads an image including its bytes. func (bs *BlogStore) ImageData(ctx context.Context, id uuid.UUID) (*Image, error) { var i Image err := bs.db.QueryRow(ctx, `SELECT id, filename, content_type, size, data, created_at FROM images WHERE id=$1`, id). Scan(&i.ID, &i.Filename, &i.ContentType, &i.Size, &i.Data, &i.CreatedAt) if err != nil { return nil, wrap(err) } return &i, nil } func (bs *BlogStore) DeleteImage(ctx context.Context, id uuid.UUID) error { _, err := bs.db.Exec(ctx, `DELETE FROM images WHERE id=$1`, id) return err }