big update: added reactions, groups, media share
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
// Media stores metadata of an uploaded file. The file bytes live in the
|
||||
// media storage backend (see internal/media), referenced by StorageKey.
|
||||
type Media struct {
|
||||
ID int64
|
||||
UploaderID int64
|
||||
Filename string
|
||||
ContentType string
|
||||
Size int64
|
||||
StorageKey string
|
||||
ThumbKey string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
// CreateMedia inserts a media record and returns it with its ID.
|
||||
func (s *Store) CreateMedia(ctx context.Context, m Media) (*Media, error) {
|
||||
err := s.pool.QueryRow(ctx, `
|
||||
INSERT INTO media(uploader_id, filename, content_type, size, storage_key, thumb_key)
|
||||
VALUES($1, $2, $3, $4, $5, $6)
|
||||
RETURNING id, created_at
|
||||
`, m.UploaderID, m.Filename, m.ContentType, m.Size, m.StorageKey, nullableString(m.ThumbKey)).Scan(&m.ID, &m.CreatedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &m, nil
|
||||
}
|
||||
|
||||
// GetMediaByID retrieves a media record or returns ErrNotFound.
|
||||
func (s *Store) GetMediaByID(ctx context.Context, id int64) (*Media, error) {
|
||||
var m Media
|
||||
var thumbKey *string
|
||||
err := s.pool.QueryRow(ctx, `
|
||||
SELECT id, uploader_id, filename, content_type, size, storage_key, thumb_key, created_at
|
||||
FROM media WHERE id=$1
|
||||
`, id).Scan(&m.ID, &m.UploaderID, &m.Filename, &m.ContentType, &m.Size, &m.StorageKey, &thumbKey, &m.CreatedAt)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if thumbKey != nil {
|
||||
m.ThumbKey = *thumbKey
|
||||
}
|
||||
return &m, nil
|
||||
}
|
||||
|
||||
func nullableString(s string) *string {
|
||||
if s == "" {
|
||||
return nil
|
||||
}
|
||||
return &s
|
||||
}
|
||||
Reference in New Issue
Block a user