Bluemage/go/server/subreddit_handlers.go

110 lines
4.3 KiB
Go
Raw Normal View History

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
}
// Count the existing subreddits using given filter.
// Default values count all.
func (su *SubredditHandler) CountSubreddits(ctx context.Context, request *connect.Request[subreddits.CountSubredditsRequest]) (*connect.Response[subreddits.CountSubredditsResponse], error) {
countRequest := subredditConverter.ProtoCountSubredditsRequestToAPICountSubredditsRequest(request.Msg)
count, err := su.API.CountSubreddits(ctx, countRequest)
if err != nil {
return nil, errs.IntoConnectError(err)
}
resp := &subreddits.CountSubredditsResponse{
Count: count,
}
return connect.NewResponse(resp), nil
}
// 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.
2024-08-15 15:20:18 +07:00
func (su *SubredditHandler) GetSubreddit(ctx context.Context, request *connect.Request[subreddits.GetSubredditRequest]) (*connect.Response[subreddits.GetSubredditResponse], error) {
data, err := su.API.GetSubreddit(ctx, request.Msg.Name)
if err != nil {
return nil, errs.IntoConnectError(err)
}
resp := &subreddits.GetSubredditResponse{
Subreddit: subredditConverter.JetSubredditToProtoSubreddit(data),
2024-08-15 15:20:18 +07:00
}
return connect.NewResponse(resp), nil
}
// ListSubreddits returns a list of subreddits.
func (su *SubredditHandler) ListSubreddits(ctx context.Context, request *connect.Request[subreddits.ListSubredditsRequest]) (*connect.Response[subreddits.ListSubredditsResponse], error) {
req := subredditConverter.ProtoListSubredditsRequestToAPIListSubredditsRequest(request.Msg)
subs, err := su.API.ListSubreddits(ctx, req)
if err != nil {
return nil, errs.IntoConnectError(err)
}
resp := &subreddits.ListSubredditsResponse{
Subreddits: subredditConverter.JetSubredditsToProtoSubreddits(subs),
}
return connect.NewResponse(resp), nil
}
// 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
}