Build image / build-and-push (push) Successful in 4s
Co-authored-by: Steve Dudenhoeffer <[email protected]>
135 lines
4.4 KiB
Go
135 lines
4.4 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"gitea.stevedudenhoeffer.com/steve/pansy/internal/domain"
|
|
)
|
|
|
|
// shareColumns lists garden_shares columns in the order scanShare expects.
|
|
const shareColumns = `id, garden_id, user_id, role, created_by, version, created_at, updated_at`
|
|
|
|
func scanShare(s scanner) (*domain.GardenShare, error) {
|
|
var sh domain.GardenShare
|
|
if err := s.Scan(
|
|
&sh.ID, &sh.GardenID, &sh.UserID, &sh.Role, &sh.CreatedBy,
|
|
&sh.Version, &sh.CreatedAt, &sh.UpdatedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
return &sh, nil
|
|
}
|
|
|
|
// GetShareRole returns the role a user holds on a garden via a share row, and
|
|
// whether such a row exists. Owner access is NOT a share row (it's implicit via
|
|
// gardens.owner_id) — this only reports viewer/editor grants. It is the lookup
|
|
// requireGardenRole uses to consult shares.
|
|
func (d *DB) GetShareRole(ctx context.Context, gardenID, userID int64) (role string, found bool, err error) {
|
|
err = d.sql.QueryRowContext(ctx,
|
|
`SELECT role FROM garden_shares WHERE garden_id = ? AND user_id = ?`, gardenID, userID,
|
|
).Scan(&role)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return "", false, nil
|
|
}
|
|
if err != nil {
|
|
return "", false, fmt.Errorf("store: get share role: %w", err)
|
|
}
|
|
return role, true, nil
|
|
}
|
|
|
|
// maxSharesListed bounds a garden's share list defensively (a garden is shared
|
|
// with a handful of people at household scale; this is never hit).
|
|
const maxSharesListed = 1000
|
|
|
|
// ListSharesForGarden returns a garden's shares joined with each recipient's
|
|
// email and display name, for the sharing UI. Always a non-nil slice.
|
|
func (d *DB) ListSharesForGarden(ctx context.Context, gardenID int64) ([]domain.ShareWithUser, error) {
|
|
rows, err := d.sql.QueryContext(ctx,
|
|
`SELECT `+qualifyColumns("s", shareColumns)+`, u.email, u.display_name
|
|
FROM garden_shares s JOIN users u ON u.id = s.user_id
|
|
WHERE s.garden_id = ?
|
|
ORDER BY u.display_name COLLATE NOCASE, s.id
|
|
LIMIT ?`,
|
|
gardenID, maxSharesListed,
|
|
)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("store: list shares: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
shares := []domain.ShareWithUser{}
|
|
for rows.Next() {
|
|
var sh domain.ShareWithUser
|
|
if err := rows.Scan(
|
|
&sh.ID, &sh.GardenID, &sh.UserID, &sh.Role, &sh.CreatedBy,
|
|
&sh.Version, &sh.CreatedAt, &sh.UpdatedAt, &sh.Email, &sh.DisplayName,
|
|
); err != nil {
|
|
return nil, fmt.Errorf("store: scan share: %w", err)
|
|
}
|
|
shares = append(shares, sh)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, fmt.Errorf("store: iterate shares: %w", err)
|
|
}
|
|
return shares, nil
|
|
}
|
|
|
|
// CreateShare inserts a share (fields validated by the service) and returns the
|
|
// stored row. A duplicate (garden_id, user_id) trips the UNIQUE index and maps to
|
|
// domain.ErrShareExists.
|
|
func (d *DB) CreateShare(ctx context.Context, sh *domain.GardenShare) (*domain.GardenShare, error) {
|
|
created, err := scanShare(d.sql.QueryRowContext(ctx,
|
|
`INSERT INTO garden_shares (garden_id, user_id, role, created_by)
|
|
VALUES (?, ?, ?, ?)
|
|
RETURNING `+shareColumns,
|
|
sh.GardenID, sh.UserID, sh.Role, sh.CreatedBy,
|
|
))
|
|
if err != nil {
|
|
if isUniqueViolation(err) {
|
|
return nil, domain.ErrShareExists
|
|
}
|
|
return nil, fmt.Errorf("store: insert share: %w", err)
|
|
}
|
|
return created, nil
|
|
}
|
|
|
|
// UpdateShareRole changes a share's role, returning the updated row or
|
|
// domain.ErrNotFound if no such share exists.
|
|
func (d *DB) UpdateShareRole(ctx context.Context, gardenID, userID int64, role string) (*domain.GardenShare, error) {
|
|
updated, err := scanShare(d.sql.QueryRowContext(ctx,
|
|
`UPDATE garden_shares
|
|
SET role = ?, version = version + 1,
|
|
updated_at = strftime('%Y-%m-%dT%H:%M:%SZ', 'now')
|
|
WHERE garden_id = ? AND user_id = ?
|
|
RETURNING `+shareColumns,
|
|
role, gardenID, userID,
|
|
))
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil, domain.ErrNotFound
|
|
}
|
|
if err != nil {
|
|
return nil, fmt.Errorf("store: update share role: %w", err)
|
|
}
|
|
return updated, nil
|
|
}
|
|
|
|
// DeleteShare removes a share. Returns domain.ErrNotFound if no row was deleted.
|
|
func (d *DB) DeleteShare(ctx context.Context, gardenID, userID int64) error {
|
|
res, err := d.sql.ExecContext(ctx,
|
|
`DELETE FROM garden_shares WHERE garden_id = ? AND user_id = ?`, gardenID, userID)
|
|
if err != nil {
|
|
return fmt.Errorf("store: delete share: %w", err)
|
|
}
|
|
n, err := res.RowsAffected()
|
|
if err != nil {
|
|
return fmt.Errorf("store: share delete rows: %w", err)
|
|
}
|
|
if n == 0 {
|
|
return domain.ErrNotFound
|
|
}
|
|
return nil
|
|
}
|