Redmage/api/subreddits_list.go

57 lines
1.5 KiB
Go
Raw Normal View History

2024-04-08 23:18:09 +07:00
package api
import (
"context"
"github.com/tigorlazuardi/redmage/db/queries"
"github.com/tigorlazuardi/redmage/pkg/errs"
)
type ListSubredditsParams struct {
Name string `json:"name"`
Limit int64 `json:"limit"`
Offset int64 `json:"offset"`
}
type ListSubredditsResult struct {
Total int64
Data []queries.Subreddit
}
func (api *API) ListSubreddits(ctx context.Context, arg ListSubredditsParams) (result ListSubredditsResult, err error) {
2024-04-13 00:15:31 +07:00
ctx, span := tracer.Start(ctx, "api.ListSubreddits")
defer span.End()
2024-04-08 23:18:09 +07:00
if arg.Name != "" {
2024-04-09 21:49:23 +07:00
result.Data, err = api.queries.SubredditsSearch(ctx, queries.SubredditsSearchParams{
2024-04-08 23:18:09 +07:00
Name: "%" + arg.Name + "%",
Limit: arg.Limit,
Offset: arg.Offset,
})
if err != nil {
return result, errs.Wrapw(err, "failed to search subreddit", "query", arg)
}
2024-04-09 21:49:23 +07:00
result.Total, err = api.queries.SubredditsSearchCount(ctx, queries.SubredditsSearchCountParams{
2024-04-08 23:18:09 +07:00
Name: "%" + arg.Name + "%",
Limit: arg.Limit,
Offset: arg.Offset,
})
if err != nil {
return result, errs.Wrapw(err, "failed to count subreddit search", "query", arg)
}
return result, err
}
2024-04-09 21:49:23 +07:00
result.Data, err = api.queries.SubredditsList(ctx, queries.SubredditsListParams{
2024-04-08 23:18:09 +07:00
Limit: arg.Limit,
Offset: arg.Offset,
})
if err != nil {
return result, errs.Wrapw(err, "failed to list subreddit", "query", arg)
}
2024-04-09 21:49:23 +07:00
result.Total, err = api.queries.SubredditsListCount(ctx)
2024-04-08 23:18:09 +07:00
if err != nil {
return result, errs.Wrapw(err, "failed to count subreddit list", "query", arg)
}
return result, err
}