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-13 14:26:19 +07:00
|
|
|
func (api *API) SubredditCreate(ctx context.Context, subreddit *models.SubredditSetter) (err error) {
|
2024-08-11 10:23:20 +07:00
|
|
|
// TODO: add check to Reddit API to see if subreddit exists.
|
|
|
|
|
|
|
|
api.lockf(func() {
|
2024-08-13 14:26:19 +07:00
|
|
|
_, err = models.Subreddits.Insert(ctx, api.Executor, subreddit)
|
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.
|
|
|
|
Wrapw(err, "subreddit already exists", "input", subreddit).
|
|
|
|
Code(connect.CodeAlreadyExists)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errs.Wrap(err, "failed to insert subreddit")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|