package api import ( "context" "errors" "connectrpc.com/connect" "github.com/tigorlazuardi/bluemage/go/gen/jet/model" "github.com/tigorlazuardi/bluemage/go/pkg/errs" "github.com/tigorlazuardi/bluemage/go/pkg/telemetry" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/metric" "go.opentelemetry.io/otel/trace" "github.com/go-jet/jet/v2/qrm" . "github.com/go-jet/jet/v2/sqlite" . "github.com/tigorlazuardi/bluemage/go/gen/jet/table" ) var getSubredditMetric, _ = meter.Int64Counter("api.subreddits.get", metric.WithDescription("The number of times a subreddit is retrieved"), ) func (api *API) GetSubreddit(ctx context.Context, name string) (sub model.Subreddits, err error) { ctx, span := tracer.Start(ctx, "GetSubreddit", trace.WithAttributes(attribute.String("name", name))) defer func() { telemetry.EndWithStatus(span, err) }() getSubredditMetric.Add(ctx, 1, metric.WithAttributes(attribute.String("name", name))) stmt := SELECT(Subreddits.AllColumns). FROM(Subreddits). WHERE(Subreddits.Name.EQ(String(name))) err = stmt.QueryContext(ctx, api.DB, &sub) if err != nil { if errors.Is(err, qrm.ErrNoRows) { return sub, errs. Wrapf(err, "subreddit '%s' does not exist", name). Details( "name", name, "query", stmt.DebugSql(), ). Code(connect.CodeNotFound) } return sub, errs.Wrapf(err, "failed to get subreddit '%s'", name). Details( "name", name, "query", stmt.DebugSql(), ) } return sub, nil }