api: implemented update subreddits endpoint
This commit is contained in:
parent
dbb754199f
commit
b220cf1b2c
33
go/api/subreddits_update.go
Normal file
33
go/api/subreddits_update.go
Normal 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
|
||||||
|
}
|
|
@ -48,6 +48,9 @@ type SubredditConverter interface {
|
||||||
|
|
||||||
// goverter:useZeroValueOnPointerInconsistency
|
// goverter:useZeroValueOnPointerInconsistency
|
||||||
ProtoListSubredditsRequestToAPIListSubredditsRequest(*subreddits.ListSubredditsRequest) api.ListSubredditsRequest
|
ProtoListSubredditsRequestToAPIListSubredditsRequest(*subreddits.ListSubredditsRequest) api.ListSubredditsRequest
|
||||||
|
|
||||||
|
// goverter:ignore Name Type CoverImageID CreatedAt UpdatedAt
|
||||||
|
ProtoSubredditSetterToBobSubredditSetter(*subreddits.SubredditSetter) *models.SubredditSetter
|
||||||
}
|
}
|
||||||
|
|
||||||
func SubredditTypeToString(subType subreddits.SubredditType) string {
|
func SubredditTypeToString(subType subreddits.SubredditType) string {
|
||||||
|
|
|
@ -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.
|
// Only the fields that are set in the request will be updated.
|
||||||
//
|
//
|
||||||
// Returns error with connect.CodeNotFound if subreddit does not exist.
|
// 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) {
|
func (su *SubredditHandler) UpdateSubreddit(ctx context.Context, request *connect.Request[subreddits.UpdateSubredditRequest]) (*connect.Response[subreddits.UpdateSubredditResponse], error) {
|
||||||
panic("not implemented") // TODO: Implement
|
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.
|
// DeleteSubreddit deletes a subreddit.
|
||||||
|
|
|
@ -13,7 +13,7 @@ message UpdateSubredditRequest {
|
||||||
}
|
}
|
||||||
|
|
||||||
message SubredditSetter {
|
message SubredditSetter {
|
||||||
optional bool disable_scheduler = 1;
|
optional bool disabled = 1;
|
||||||
optional string schedule = 2 [(buf.validate.field).string.min_len = 1];
|
optional string schedule = 2 [(buf.validate.field).string.min_len = 1];
|
||||||
optional int64 countback = 3 [(buf.validate.field).int64.gt = 0];
|
optional int64 countback = 3 [(buf.validate.field).int64.gt = 0];
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue