mirror of https://gitlab.com/ngerakines/tavern.git
66 lines
2.2 KiB
Go
66 lines
2.2 KiB
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/gofrs/uuid"
|
|
|
|
"github.com/ngerakines/tavern/errors"
|
|
)
|
|
|
|
type ThreadStorage interface {
|
|
PartOfThread(ctx context.Context, userID uuid.UUID, conversation string) (bool, error)
|
|
WatchThread(ctx context.Context, userID uuid.UUID, conversation string) error
|
|
|
|
AddToUserFeed(ctx context.Context, userID, activityID uuid.UUID) error
|
|
PaginateUserFeed(ctx context.Context, userID uuid.UUID, limit, offset int) ([]UserFeed, error)
|
|
}
|
|
|
|
type UserFeed struct {
|
|
ID uuid.UUID
|
|
UserID uuid.UUID
|
|
ActivityID uuid.UUID
|
|
CreatedAt time.Time
|
|
}
|
|
|
|
func (s pgStorage) PartOfThread(ctx context.Context, userID uuid.UUID, conversation string) (bool, error) {
|
|
c, err := s.RowCount(ctx, `SELECT COUNT(*) FROM user_threads WHERE user_id = $1 AND thread = $2`, userID, conversation)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return c > 0, nil
|
|
}
|
|
|
|
func (s pgStorage) WatchThread(ctx context.Context, userID uuid.UUID, conversation string) error {
|
|
rowID := NewV4()
|
|
_, err := s.db.ExecContext(ctx, "INSERT INTO user_threads (id, user_id, conversation, created_at) VALUES ($1, $2, $3, $4)", rowID, userID, conversation, s.now())
|
|
return errors.WrapInsertQueryFailedError(err)
|
|
}
|
|
|
|
func (s pgStorage) AddToUserFeed(ctx context.Context, userID, activityID uuid.UUID) error {
|
|
rowID := NewV4()
|
|
_, err := s.db.ExecContext(ctx, "INSERT INTO user_feed (id, user_id, activity_id, created_at) VALUES ($1, $2, $3, $4)", rowID, userID, activityID, s.now())
|
|
return errors.WrapInsertQueryFailedError(err)
|
|
}
|
|
|
|
func (s pgStorage) PaginateUserFeed(ctx context.Context, userID uuid.UUID, limit, offset int) ([]UserFeed, error) {
|
|
query := `SELECT id, user_id, activity_id, created_at FROM user_feed WHERE user_id = $3 ORDER BY created_at ASC LIMIT $1 OFFSET $2`
|
|
var results []UserFeed
|
|
|
|
rows, err := s.db.QueryContext(ctx, query, limit, offset, userID)
|
|
if err != nil {
|
|
return nil, errors.NewSelectQueryFailedError(err)
|
|
}
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
var uf UserFeed
|
|
if err := rows.Scan(&uf.ID, &uf.UserID, &uf.ActivityID, &uf.CreatedAt); err != nil {
|
|
return nil, errors.NewSelectQueryFailedError(err)
|
|
}
|
|
results = append(results, uf)
|
|
}
|
|
|
|
return results, nil
|
|
}
|