Files
tgex-backend/tg_parser/internal/adapter/database/create_views_snapshot.go
Artem Tsyrulnikov 62e8f98429 refactor
2026-01-18 14:10:13 +03:00

41 lines
1.1 KiB
Go

package database
import (
"context"
"fmt"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
"github.com/TelegramExchange/pkg/transaction"
"github.com/TelegramExchange/tgex-backend/tg_parser/internal/domain"
)
func (d *Database) CreateViewsSnapshot(ctx context.Context, snapshot domain.ViewsSnapshot) error {
query := `INSERT INTO post_views_history (id, views_count, fetched_at, post_id)
VALUES ($1, $2, $3, $4);`
txOrPool := transaction.TryExtractTX(ctx)
dto := createViewsSnapshotDTO{
ID: pgtype.UUID{Bytes: uuid.New(), Valid: true},
ViewsCount: pgtype.Int4{Int32: int32(snapshot.ViewsCount), Valid: true},
FetchedAt: pgtype.Timestamptz{Time: snapshot.FetchedAt, Valid: true},
PostID: pgtype.UUID{Bytes: snapshot.PostID, Valid: true},
}
_, err := txOrPool.Exec(ctx, query, dto.ID, dto.ViewsCount, dto.FetchedAt, dto.PostID)
if err != nil {
return fmt.Errorf("txOrPool.Exec: %w", err)
}
return nil
}
type createViewsSnapshotDTO struct {
ID pgtype.UUID
ViewsCount pgtype.Int4
FetchedAt pgtype.Timestamptz
PostID pgtype.UUID
}