package server import ( "context" "connectrpc.com/connect" "github.com/tigorlazuardi/bluemage/go/api" "github.com/tigorlazuardi/bluemage/go/gen/converter" subreddits "github.com/tigorlazuardi/bluemage/go/gen/proto/subreddits/v1" "github.com/tigorlazuardi/bluemage/go/gen/proto/subreddits/v1/v1connect" "github.com/tigorlazuardi/bluemage/go/pkg/errs" ) var subredditConverter = converter.SubredditConverterImpl{} type SubredditHandler struct { API *api.API v1connect.UnimplementedSubredditsServiceHandler } // CreateSubreddit creates a new subreddit // Returns the subreddit name. // // Returns the following error codes: // - connect.CodeAlreadyExists if subreddit with the same name already exists. // - connect.CodeInvalidArgument if validation failed, e.g. Invalid schedule cron format. // - connect.CodeNotFound if the subreddit does not exist. func (su *SubredditHandler) CreateSubreddit(ctx context.Context, request *connect.Request[subreddits.CreateSubredditRequest]) (*connect.Response[subreddits.CreateSubredditResponse], error) { data := subredditConverter.CreateSubredditRequestToModelsSubredditSetter(request.Msg) if err := su.API.SubredditCreate(ctx, data); err != nil { return nil, errs.IntoConnectError(err) } resp := &subreddits.CreateSubredditResponse{ Name: data.Name.GetOrZero(), } return connect.NewResponse(resp), nil } // GetSubreddit returns a subreddit by its name. // // Returns error with connect.CodeNotFound if subreddit does not exist. func (su *SubredditHandler) GetSubreddit(_ context.Context, _ *connect.Request[subreddits.GetSubredditRequest]) (*connect.Response[subreddits.GetSubredditResponse], error) { panic("not implemented") // TODO: Implement } // ListSubreddits returns a list of subreddits. func (su *SubredditHandler) ListSubreddits(_ context.Context, _ *connect.Request[subreddits.ListSubredditsRequest]) (*connect.Response[subreddits.ListSubredditsResponse], error) { panic("not implemented") // TODO: Implement } // UpdateSubreddit updates a subreddit. // // 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 } // DeleteSubreddit deletes a subreddit. // // Returns error with connect.CodeNotFound if subreddit does not exist. func (su *SubredditHandler) DeleteSubreddit(_ context.Context, _ *connect.Request[subreddits.DeleteSubredditRequest]) (*connect.Response[subreddits.DeleteSubredditResponse], error) { panic("not implemented") // TODO: Implement } // ResolveSubredditName resolves the given subreddit name. // // The returned resolved_name is the name that is actually // used in Reddit. // // Returns error with connect.CodeNotFound if subreddit does not exist. // So this rpc endpoint also acts to check subreddit's existence. func (su *SubredditHandler) ResolveSubredditName(_ context.Context, _ *connect.Request[subreddits.ResolveSubredditNameRequest]) (*connect.Response[subreddits.ResolveSubredditNameResponse], error) { panic("not implemented") // TODO: Implement }