Files
tgex-backend/tg_parser/internal/adapter/database/create_post.go
Artem Tsyrulnikov 2c0ae2dfc3 парсер
2026-01-06 21:06:21 +03:00

43 lines
1.1 KiB
Go

package database
import (
"context"
"fmt"
"github.com/jackc/pgx/v5/pgtype"
"github.com/TelegramExchange/tgex-parser/internal/domain"
"github.com/TelegramExchange/tgex-parser/pkg/transaction"
)
func (d *Database) CreatePost(ctx context.Context, post domain.Post) error {
query := `INSERT INTO post (id, channel_id, message_id, text)
VALUES ($1, $2, $3, $4)
ON CONFLICT (channel_id, message_id) DO UPDATE
SET text = EXCLUDED.text,
updated_at = CURRENT_TIMESTAMP;`
txOrPool := transaction.TryExtractTX(ctx)
dto := createPostDTO{
ID: pgtype.UUID{Bytes: post.ID, Valid: true},
ChannelID: pgtype.UUID{Bytes: post.ChannelID, Valid: true},
MessageID: pgtype.Int4{Int32: int32(post.MessageID), Valid: true},
Text: pgtype.Text{String: post.Text, Valid: true},
}
_, err := txOrPool.Exec(ctx, query, dto.ID, dto.ChannelID, dto.MessageID, dto.Text)
if err != nil {
return fmt.Errorf("txOrPool.Exec: %w", err)
}
return nil
}
type createPostDTO struct {
ID pgtype.UUID
ChannelID pgtype.UUID
MessageID pgtype.Int4
Text pgtype.Text
}