- rename Go module, binary, systemd unit, PG_DATABASE default, frontend package and docs from shared-album to galo - add theme-aware halo emblem and favicon (new ui-branding capability) - add make build-release (versioned, stripped, static binary) and run the binary from the repo root
102 lines
2.9 KiB
Go
102 lines
2.9 KiB
Go
package social
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
|
|
"galo/internal/models"
|
|
"galo/internal/sqliteutil"
|
|
)
|
|
|
|
// SQLiteStore is the SQLite implementation of Repository.
|
|
type SQLiteStore struct {
|
|
db *sql.DB
|
|
}
|
|
|
|
func NewSQLiteStore(db *sql.DB) *SQLiteStore { return &SQLiteStore{db: db} }
|
|
|
|
func (s *SQLiteStore) Like(ctx context.Context, photoID, userID string) error {
|
|
_, err := s.db.ExecContext(ctx,
|
|
`INSERT INTO likes (photo_id, user_id) VALUES ($1, $2)
|
|
ON CONFLICT (photo_id, user_id) DO NOTHING`, photoID, userID)
|
|
return err
|
|
}
|
|
|
|
func (s *SQLiteStore) Unlike(ctx context.Context, photoID, userID string) error {
|
|
_, err := s.db.ExecContext(ctx,
|
|
`DELETE FROM likes WHERE photo_id = $1 AND user_id = $2`, photoID, userID)
|
|
return err
|
|
}
|
|
|
|
func (s *SQLiteStore) CreateComment(ctx context.Context, c *models.Comment) error {
|
|
var createdAt, updatedAt string
|
|
err := s.db.QueryRowContext(ctx,
|
|
`INSERT INTO comments (id, photo_id, user_id, text)
|
|
VALUES ($1, $2, $3, $4)
|
|
RETURNING created_at, updated_at`,
|
|
c.ID, c.PhotoID, c.UserID, c.Text,
|
|
).Scan(&createdAt, &updatedAt)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
c.CreatedAt = sqliteutil.ParseTime(createdAt)
|
|
c.UpdatedAt = sqliteutil.ParseTime(updatedAt)
|
|
return nil
|
|
}
|
|
|
|
func (s *SQLiteStore) ListComments(ctx context.Context, photoID string) ([]models.Comment, error) {
|
|
rows, err := s.db.QueryContext(ctx,
|
|
`SELECT c.id, c.photo_id, c.user_id, c.text, c.created_at, c.updated_at,
|
|
u.id, u.name, COALESCE(u.avatar_url, '')
|
|
FROM comments c
|
|
JOIN users u ON u.id = c.user_id
|
|
WHERE c.photo_id = $1
|
|
ORDER BY c.created_at`, photoID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
out := []models.Comment{}
|
|
for rows.Next() {
|
|
var c models.Comment
|
|
var createdAt, updatedAt string
|
|
if err := rows.Scan(&c.ID, &c.PhotoID, &c.UserID, &c.Text, &createdAt, &updatedAt,
|
|
&c.User.ID, &c.User.Name, &c.User.AvatarURL); err != nil {
|
|
return nil, err
|
|
}
|
|
c.CreatedAt = sqliteutil.ParseTime(createdAt)
|
|
c.UpdatedAt = sqliteutil.ParseTime(updatedAt)
|
|
out = append(out, c)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
func (s *SQLiteStore) GetComment(ctx context.Context, id string) (*models.Comment, error) {
|
|
var c models.Comment
|
|
var createdAt, updatedAt string
|
|
err := s.db.QueryRowContext(ctx,
|
|
`SELECT id, photo_id, user_id, text, created_at, updated_at FROM comments WHERE id = $1`, id).
|
|
Scan(&c.ID, &c.PhotoID, &c.UserID, &c.Text, &createdAt, &updatedAt)
|
|
if err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
c.CreatedAt = sqliteutil.ParseTime(createdAt)
|
|
c.UpdatedAt = sqliteutil.ParseTime(updatedAt)
|
|
return &c, nil
|
|
}
|
|
|
|
func (s *SQLiteStore) DeleteComment(ctx context.Context, id string) error {
|
|
res, err := s.db.ExecContext(ctx, `DELETE FROM comments WHERE id = $1`, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if n, _ := res.RowsAffected(); n == 0 {
|
|
return ErrNotFound
|
|
}
|
|
return nil
|
|
}
|