34 lines
886 B
Go
34 lines
886 B
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"connectrpc.com/connect"
|
|
"github.com/mattn/go-sqlite3"
|
|
"github.com/tigorlazuardi/bluemage/go/gen/models"
|
|
"github.com/tigorlazuardi/bluemage/go/pkg/errs"
|
|
"github.com/tigorlazuardi/bluemage/go/pkg/telemetry"
|
|
)
|
|
|
|
func (api *API) SubredditCreate(ctx context.Context, request *models.SubredditSetter) (err error) {
|
|
ctx, span := tracer.Start(ctx, "SubredditCreate")
|
|
defer func() { telemetry.EndWithStatus(span, err) }()
|
|
|
|
api.lockf(func() {
|
|
_, err = models.Subreddits.Insert(ctx, api.Executor, request)
|
|
})
|
|
if err != nil {
|
|
if sqlite3err := new(sqlite3.Error); errors.As(err, &sqlite3err) {
|
|
if sqlite3err.Code == sqlite3.ErrConstraint {
|
|
return errs.
|
|
Wrapw(err, "subreddit already exists", "input", request).
|
|
Code(connect.CodeAlreadyExists)
|
|
}
|
|
}
|
|
return errs.Wrap(err, "failed to insert subreddit")
|
|
}
|
|
|
|
return nil
|
|
}
|