Created
July 11, 2021 17:08
-
-
Save subzero112233/33d74eabd4a497ad039b713001abf93b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package twirphandler | |
import ( | |
"context" | |
"net/http" | |
"time" | |
"github.com/subzero112233/golang-twirp/entity" | |
"github.com/subzero112233/golang-twirp/rpc/stats" | |
"github.com/subzero112233/golang-twirp/usecase/playerstats" | |
"github.com/twitchtv/twirp" | |
"google.golang.org/protobuf/types/known/timestamppb" | |
) | |
type TwirpHandler struct { | |
Usecase playerstats.UseCase | |
} | |
func NewTwirpHandler(usecase playerstats.UseCase) http.Handler { | |
t := &TwirpHandler{ | |
Usecase: usecase, | |
} | |
return stats.NewStatsServiceServer(t) | |
} | |
// errors may not be working well. check this by returning an error from the usecase | |
func (t *TwirpHandler) GetStats(ctx context.Context, input *stats.GetStatsRequest) (*stats.GetStatsResponse, error) { | |
var stat entity.Stats | |
stat.PlayerName = input.PlayerName | |
data, err := t.Usecase.GetStats(stat.PlayerName) | |
if err != nil { | |
return &stats.GetStatsResponse{}, twirp.InternalError(err.Error()) | |
} | |
statsSlice := convertFromEntity(data) | |
return &stats.GetStatsResponse{Stats: statsSlice}, nil | |
} | |
// errors may not be working well. check this by returning an error from the usecase | |
func (t *TwirpHandler) AddStats(ctx context.Context, input *stats.AddStatsRequest) (*stats.AddStatsResponse, error) { | |
e := convertToEntity(input.Stats) | |
err := t.Usecase.AddStats(e) | |
if err != nil { | |
return &stats.AddStatsResponse{}, twirp.InternalError(err.Error()) | |
} | |
return &stats.AddStatsResponse{Status: "success"}, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment