54 lines
1.4 KiB
Go
54 lines
1.4 KiB
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
|
|
"github.com/TelegramExchange/pkg/transaction"
|
|
"github.com/TelegramExchange/tgex-backend/tg_parser/internal/domain"
|
|
)
|
|
|
|
func (d *Database) UpdateChannel(ctx context.Context, channel domain.Channel) error {
|
|
query := `UPDATE channel
|
|
SET telegram_id = $2,
|
|
username = $3,
|
|
title = $4,
|
|
access_hash = $5,
|
|
pts = $6,
|
|
updated_at = CURRENT_TIMESTAMP
|
|
WHERE id = $1;`
|
|
|
|
txOrPool := transaction.TryExtractTX(ctx)
|
|
|
|
username := strings.TrimSpace(channel.Username)
|
|
usernameValid := username != ""
|
|
|
|
dto := updateChannelDTO{
|
|
ID: pgtype.UUID{Bytes: channel.ID, Valid: true},
|
|
TelegramID: pgtype.Int8{Int64: channel.TelegramID, Valid: true},
|
|
Username: pgtype.Text{String: username, Valid: usernameValid},
|
|
Title: pgtype.Text{String: channel.Title, Valid: true},
|
|
AccessHash: pgtype.Int8{Int64: channel.AccessHash, Valid: true},
|
|
Pts: pgtype.Int4{Int32: int32(channel.Pts), Valid: true},
|
|
}
|
|
|
|
_, err := txOrPool.Exec(ctx, query, dto.ID, dto.TelegramID, dto.Username, dto.Title, dto.AccessHash, dto.Pts)
|
|
if err != nil {
|
|
return fmt.Errorf("txOrPool.Exec: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type updateChannelDTO struct {
|
|
ID pgtype.UUID
|
|
TelegramID pgtype.Int8
|
|
Username pgtype.Text
|
|
Title pgtype.Text
|
|
AccessHash pgtype.Int8
|
|
Pts pgtype.Int4
|
|
}
|