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 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 InviteLink pgtype.Text } func (dto *getChannelsDTO) destination() []any { return []any{ &dto.ID, &dto.TelegramID, &dto.Username, &dto.Title, &dto.AccessHash, &dto.Pts, &dto.InviteLink, } } 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, } }