Files
tgex-backend/tg_parser/internal/adapter/database/get_channels.go
Artem Tsyrulnikov f23f489b22 fix парсер
2026-01-28 18:29:56 +03:00

89 lines
1.8 KiB
Go

package database
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
"github.com/rs/zerolog/log"
"github.com/TelegramExchange/pkg/transaction"
"github.com/TelegramExchange/tgex-backend/tg_parser/internal/domain"
)
func (d *Database) GetChannels(ctx context.Context) []domain.Channel {
query := `SELECT
id,
telegram_id,
username,
title,
access_hash,
pts,
invite_link,
is_accessible
FROM channel
WHERE deleted_at IS NULL
AND is_accessible = true;`
txOrPool := transaction.TryExtractTX(ctx)
rows, err := txOrPool.Query(ctx, query)
if err != nil {
log.Error().Err(err).Msg("txOrPool.Query failed")
return []domain.Channel{}
}
defer rows.Close()
channels := make([]domain.Channel, 0)
for rows.Next() {
var dto getChannelsDTO
err = rows.Scan(dto.destination()...)
if err != nil {
log.Error().Err(err).Msg("rows.Scan failed")
return []domain.Channel{}
}
channels = append(channels, dto.toDomain())
}
return channels
}
type getChannelsDTO struct {
ID pgtype.UUID
TelegramID pgtype.Int8
Username pgtype.Text
Title pgtype.Text
AccessHash pgtype.Int8
Pts pgtype.Int4
InviteLink pgtype.Text
IsAccessible pgtype.Bool
}
func (dto *getChannelsDTO) destination() []any {
return []any{
&dto.ID,
&dto.TelegramID,
&dto.Username,
&dto.Title,
&dto.AccessHash,
&dto.Pts,
&dto.InviteLink,
&dto.IsAccessible,
}
}
func (dto *getChannelsDTO) toDomain() domain.Channel {
return domain.Channel{
ID: dto.ID.Bytes,
TelegramID: domain.NormalizeChatID(dto.TelegramID.Int64),
Username: dto.Username.String,
Title: dto.Title.String,
AccessHash: dto.AccessHash.Int64,
Pts: int(dto.Pts.Int32),
InviteLink: dto.InviteLink.String,
IsAccessible: dto.IsAccessible.Bool,
}
}