api: implemented update subreddits endpoint

This commit is contained in:
Tigor Hutasuhut 2024-08-15 16:17:25 +07:00
parent dbb754199f
commit b220cf1b2c
4 changed files with 47 additions and 3 deletions

View file

@ -0,0 +1,33 @@
package api
import (
"context"
"github.com/tigorlazuardi/bluemage/go/gen/models"
"github.com/tigorlazuardi/bluemage/go/pkg/errs"
"github.com/tigorlazuardi/bluemage/go/pkg/log"
"github.com/tigorlazuardi/bluemage/go/pkg/telemetry"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)
func (api *API) UpdateSubreddit(ctx context.Context, name string, set *models.SubredditSetter) (err error) {
ctx, span := tracer.Start(ctx, "UpdateSubreddit", trace.WithAttributes(attribute.String("name", name)))
defer func() { telemetry.EndWithStatus(span, err) }()
ctx, coll := log.WithQueryCollector(ctx)
api.lockf(func() {
err = models.Subreddits.Update(ctx, api.Executor, set, &models.Subreddit{Name: name})
})
if err != nil {
err = errs.Wrapw(
err, "failed to update subreddit",
"name", name,
"set", set,
"query", coll,
)
return err
}
return nil
}

View file

@ -48,6 +48,9 @@ type SubredditConverter interface {
// goverter:useZeroValueOnPointerInconsistency
ProtoListSubredditsRequestToAPIListSubredditsRequest(*subreddits.ListSubredditsRequest) api.ListSubredditsRequest
// goverter:ignore Name Type CoverImageID CreatedAt UpdatedAt
ProtoSubredditSetterToBobSubredditSetter(*subreddits.SubredditSetter) *models.SubredditSetter
}
func SubredditTypeToString(subType subreddits.SubredditType) string {

View file

@ -86,8 +86,16 @@ func (su *SubredditHandler) ListSubreddits(ctx context.Context, request *connect
// Only the fields that are set in the request will be updated.
//
// Returns error with connect.CodeNotFound if subreddit does not exist.
func (su *SubredditHandler) UpdateSubreddit(_ context.Context, _ *connect.Request[subreddits.UpdateSubredditRequest]) (*connect.Response[subreddits.UpdateSubredditResponse], error) {
panic("not implemented") // TODO: Implement
func (su *SubredditHandler) UpdateSubreddit(ctx context.Context, request *connect.Request[subreddits.UpdateSubredditRequest]) (*connect.Response[subreddits.UpdateSubredditResponse], error) {
set := subredditConverter.ProtoSubredditSetterToBobSubredditSetter(request.Msg.Set)
err := su.API.UpdateSubreddit(ctx, request.Msg.Name, set)
if err != nil {
return nil, errs.IntoConnectError(err)
}
resp := &subreddits.UpdateSubredditResponse{
Name: request.Msg.Name,
}
return connect.NewResponse(resp), nil
}
// DeleteSubreddit deletes a subreddit.

View file

@ -13,7 +13,7 @@ message UpdateSubredditRequest {
}
message SubredditSetter {
optional bool disable_scheduler = 1;
optional bool disabled = 1;
optional string schedule = 2 [(buf.validate.field).string.min_len = 1];
optional int64 countback = 3 [(buf.validate.field).int64.gt = 0];
}