51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
|
package api
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
|
||
|
. "github.com/go-jet/jet/v2/sqlite"
|
||
|
. "github.com/tigorlazuardi/bluemage/go/gen/jet/table"
|
||
|
"github.com/tigorlazuardi/bluemage/go/pkg/errs"
|
||
|
"github.com/tigorlazuardi/bluemage/go/pkg/telemetry"
|
||
|
)
|
||
|
|
||
|
type CountSubredditsRequest struct {
|
||
|
Search string
|
||
|
Disabled *bool
|
||
|
}
|
||
|
|
||
|
func (c CountSubredditsRequest) Statement() SelectStatement {
|
||
|
cond := Bool(true)
|
||
|
if c.Search != "" {
|
||
|
cond.AND(Subreddits.Name.LIKE(String("%" + c.Search + "%")))
|
||
|
}
|
||
|
if c.Disabled != nil {
|
||
|
disabled := *c.Disabled
|
||
|
if disabled {
|
||
|
cond.AND(Subreddits.Disabled.EQ(Int(1)))
|
||
|
} else {
|
||
|
cond.AND(Subreddits.Disabled.EQ(Int(0)))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return SELECT(COUNT(Subreddits.Name)).WHERE(cond)
|
||
|
}
|
||
|
|
||
|
func (api *API) CountSubreddits(ctx context.Context, request CountSubredditsRequest) (count int64, err error) {
|
||
|
ctx, span := tracer.Start(ctx, "CountSubreddits")
|
||
|
defer func() { telemetry.EndWithStatus(span, err) }()
|
||
|
|
||
|
stmt := request.Statement()
|
||
|
query, args := stmt.Sql()
|
||
|
err = api.DB.QueryRowContext(ctx, query, args...).Scan(&count)
|
||
|
if err != nil {
|
||
|
err = errs.Wrapw(err, "failed to count subreddits",
|
||
|
"request", request,
|
||
|
"query", stmt.DebugSql(),
|
||
|
)
|
||
|
return count, err
|
||
|
}
|
||
|
|
||
|
return count, err
|
||
|
}
|