42 lines
1,021 B
Go
42 lines
1,021 B
Go
package admin
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"strings"
|
|
|
|
"codeberg.org/nextgo/dbots/internal/db"
|
|
"codeberg.org/nextgo/dbots/internal/errorutil"
|
|
"codeberg.org/nextgo/dbots/internal/paginate"
|
|
)
|
|
|
|
type Service struct {
|
|
q *db.Queries
|
|
}
|
|
|
|
func NewService(q *db.Queries) *Service {
|
|
return &Service{q: q}
|
|
}
|
|
|
|
func (s *Service) ListBots(ctx context.Context, status db.BotStatus, p paginate.Params) (paginate.Page[*db.Bot], error) {
|
|
status = db.BotStatus(strings.ToLower(status.String()))
|
|
total, err := s.q.CountBotsByUsername(ctx, db.CountBotsByUsernameParams{
|
|
Status: status,
|
|
})
|
|
if err != nil {
|
|
slog.Error("error counting bots", "err", err)
|
|
return paginate.Page[*db.Bot]{}, errorutil.ErrSearchFailed
|
|
}
|
|
|
|
bots, err := s.q.ListBotsByStatus(ctx, db.ListBotsByStatusParams{
|
|
Status: status,
|
|
Limit: p.Limit,
|
|
Offset: p.Offset,
|
|
})
|
|
if err != nil {
|
|
slog.Error("error listing bots", "err", err)
|
|
return paginate.Page[*db.Bot]{}, errorutil.ErrSearchFailed
|
|
}
|
|
|
|
return paginate.NewPage(bots, int(total), p), nil
|
|
}
|