Foundation: config, schema/migrations, auth, stable IDs, metadata probing
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
|
||||
"git.noroutes.net/NoRoutes/vibetunes/internal/idgen"
|
||||
)
|
||||
|
||||
// Shares are a schema foundation for Phase 2's Subsonic share endpoints;
|
||||
// Phase 1 doesn't yet expose a sharing UI.
|
||||
|
||||
type Share struct {
|
||||
ID string
|
||||
OwnerID string
|
||||
Token string
|
||||
Description string
|
||||
ExpiresAt sql.NullString
|
||||
}
|
||||
|
||||
func CreateShare(db *sql.DB, ownerID, token, description string) (*Share, error) {
|
||||
id := idgen.New("sh")
|
||||
_, err := db.Exec(`INSERT INTO shares (id, owner_id, token, description) VALUES (?, ?, ?, ?)`, id, ownerID, token, description)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return GetShareByToken(db, token)
|
||||
}
|
||||
|
||||
func GetShareByToken(db *sql.DB, token string) (*Share, error) {
|
||||
var s Share
|
||||
err := db.QueryRow(`SELECT id, owner_id, token, description, expires_at FROM shares WHERE token = ?`, token).
|
||||
Scan(&s.ID, &s.OwnerID, &s.Token, &s.Description, &s.ExpiresAt)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &s, nil
|
||||
}
|
||||
Reference in New Issue
Block a user