парсер
This commit is contained in:
79
tg_parser/internal/adapter/database/get_channels.go
Normal file
79
tg_parser/internal/adapter/database/get_channels.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/TelegramExchange/tgex-parser/internal/domain"
|
||||
"github.com/TelegramExchange/tgex-parser/pkg/transaction"
|
||||
)
|
||||
|
||||
func (d *Database) GetChannels(ctx context.Context) []domain.Channel {
|
||||
query := `SELECT
|
||||
id,
|
||||
telegram_id,
|
||||
username,
|
||||
title,
|
||||
access_hash,
|
||||
pts
|
||||
FROM channel
|
||||
WHERE deleted_at IS NULL;`
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func (dto *getChannelsDTO) destination() []any {
|
||||
return []any{
|
||||
&dto.ID,
|
||||
&dto.TelegramID,
|
||||
&dto.Username,
|
||||
&dto.Title,
|
||||
&dto.AccessHash,
|
||||
&dto.Pts,
|
||||
}
|
||||
}
|
||||
|
||||
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),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user