api: implemented get subreddit handler

This commit is contained in:
Tigor Hutasuhut 2024-08-15 15:20:18 +07:00
parent c83387652b
commit baf4ba686e
3 changed files with 34 additions and 5 deletions

View file

@ -3,6 +3,7 @@ package converts
import ( import (
"github.com/aarondl/opt/omit" "github.com/aarondl/opt/omit"
"github.com/tigorlazuardi/bluemage/go/api" "github.com/tigorlazuardi/bluemage/go/api"
"github.com/tigorlazuardi/bluemage/go/gen/jet/model"
"github.com/tigorlazuardi/bluemage/go/gen/models" "github.com/tigorlazuardi/bluemage/go/gen/models"
subreddits "github.com/tigorlazuardi/bluemage/go/gen/proto/subreddits/v1" subreddits "github.com/tigorlazuardi/bluemage/go/gen/proto/subreddits/v1"
) )
@ -27,12 +28,18 @@ import (
// goverter:extend SubredditTypeToString // goverter:extend SubredditTypeToString
// goverter:extend SubredditTypeToOmitString // goverter:extend SubredditTypeToOmitString
// goverter:extend SubredditsDisabledFilterToPtrBool // goverter:extend SubredditsDisabledFilterToPtrBool
// goverter:extend Int8ToSubredditDisabledFilter
// goverter:extend StringToSubredditType
type SubredditConverter interface { type SubredditConverter interface {
// goverter:ignore CreatedAt UpdatedAt CoverImageID // goverter:ignore CreatedAt UpdatedAt CoverImageID
CreateSubredditRequestToModelsSubredditSetter(*subreddits.CreateSubredditRequest) *models.SubredditSetter CreateSubredditRequestToModelsSubredditSetter(*subreddits.CreateSubredditRequest) *models.SubredditSetter
// goverter:useZeroValueOnPointerInconsistency // goverter:useZeroValueOnPointerInconsistency
ProtoCountSubredditsRequestToAPICountSubredditsRequest(*subreddits.CountSubredditsRequest) api.CountSubredditsRequest ProtoCountSubredditsRequestToAPICountSubredditsRequest(*subreddits.CountSubredditsRequest) api.CountSubredditsRequest
// goverter:ignore state sizeCache unknownFields
// goverter:map CoverImageID CoverImageId
JetSubredditToProtoGetSubredditResponse(model.Subreddits) *subreddits.Subreddit
} }
func SubredditTypeToString(subType subreddits.SubredditType) string { func SubredditTypeToString(subType subreddits.SubredditType) string {
@ -58,3 +65,17 @@ func SubredditsDisabledFilterToPtrBool(filter subreddits.DisabledFilter) *bool {
} }
return nil return nil
} }
func Int8ToSubredditDisabledFilter(i int8) subreddits.DisabledFilter {
if i == 1 {
return subreddits.DisabledFilter_DISABLED_FILTER_TRUE
}
return subreddits.DisabledFilter_DISABLED_FILTER_FALSE
}
func StringToSubredditType(s string) subreddits.SubredditType {
if s == "user" {
return subreddits.SubredditType_SUBREDDIT_TYPE_USER
}
return subreddits.SubredditType_SUBREDDIT_TYPE_SUBREDDIT
}

View file

@ -56,8 +56,16 @@ func (su *SubredditHandler) CreateSubreddit(ctx context.Context, request *connec
// GetSubreddit returns a subreddit by its name. // GetSubreddit returns a subreddit by its name.
// //
// Returns error with connect.CodeNotFound if subreddit does not exist. // 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) { func (su *SubredditHandler) GetSubreddit(ctx context.Context, request *connect.Request[subreddits.GetSubredditRequest]) (*connect.Response[subreddits.GetSubredditResponse], error) {
panic("not implemented") // TODO: Implement data, err := su.API.GetSubreddit(ctx, request.Msg.Name)
if err != nil {
return nil, errs.IntoConnectError(err)
}
resp := &subreddits.GetSubredditResponse{
Subreddit: subredditConverter.JetSubredditToProtoGetSubredditResponse(data),
}
return connect.NewResponse(resp), nil
} }
// ListSubreddits returns a list of subreddits. // ListSubreddits returns a list of subreddits.

View file

@ -6,14 +6,14 @@ option go_package = "github.com/tigorlazuardi/bluemage/go/gen/proto/subreddits/v
message Subreddit { message Subreddit {
string name = 1; string name = 1;
bool disable_scheduler = 2; bool disabled = 2;
// type is never unspecified for this response. // type is never unspecified for this response.
// //
// It's always either `SUBREDDIT_TYPE_SUBREDDIT` // It's always either `SUBREDDIT_TYPE_SUBREDDIT`
// or `SUBREDDIT_TYPE_USER`. // or `SUBREDDIT_TYPE_USER`.
SubredditType type = 3; SubredditType type = 3;
string schedule = 4; string schedule = 4;
uint32 countback = 5; int64 countback = 5;
// cover_image_id is the id of an image that // cover_image_id is the id of an image that
// acts as the cover for this subreddit. // acts as the cover for this subreddit.
// //
@ -26,7 +26,7 @@ message Subreddit {
// cover_image_id changes after every insert of // cover_image_id changes after every insert of
// new images in that subreddit, so avoid // new images in that subreddit, so avoid
// caching the image for too long. // caching the image for too long.
optional uint32 cover_image_id = 6; optional int32 cover_image_id = 6;
int64 created_at = 7; int64 created_at = 7;
int64 updated_at = 8; int64 updated_at = 8;
} }