2024-08-11 10:23:20 +07:00
|
|
|
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"
|
2024-08-15 20:39:04 +07:00
|
|
|
"github.com/tigorlazuardi/bluemage/go/pkg/telemetry"
|
2024-08-11 10:23:20 +07:00
|
|
|
)
|
|
|
|
|
2024-08-15 20:39:04 +07:00
|
|
|
func (api *API) SubredditCreate(ctx context.Context, request *models.SubredditSetter) (err error) {
|
|
|
|
ctx, span := tracer.Start(ctx, "SubredditCreate")
|
|
|
|
defer func() { telemetry.EndWithStatus(span, err) }()
|
2024-08-11 10:23:20 +07:00
|
|
|
|
|
|
|
api.lockf(func() {
|
2024-08-15 20:39:04 +07:00
|
|
|
_, err = models.Subreddits.Insert(ctx, api.Executor, request)
|
2024-08-11 10:23:20 +07:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if sqlite3err := new(sqlite3.Error); errors.As(err, &sqlite3err) {
|
|
|
|
if sqlite3err.Code == sqlite3.ErrConstraint {
|
|
|
|
return errs.
|
2024-08-15 20:39:04 +07:00
|
|
|
Wrapw(err, "subreddit already exists", "input", request).
|
2024-08-11 10:23:20 +07:00
|
|
|
Code(connect.CodeAlreadyExists)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errs.Wrap(err, "failed to insert subreddit")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|