grpc.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package JeopardyManager
  2. import (
  3. "context"
  4. "errors"
  5. gljeopardypb "git.alfi.li/gamelang/protobuf/gamelang-jeopardy"
  6. )
  7. type JeopardyServer struct {
  8. gljeopardypb.UnimplementedJeopardyServiceServer
  9. JeopardyManager *JeopardyManager
  10. }
  11. func NewJeopardyServer(um *JeopardyManager) *JeopardyServer {
  12. return &JeopardyServer{JeopardyManager: um}
  13. }
  14. func (bs JeopardyServer) CreateJeopardy(ctx context.Context, jeopardy *gljeopardypb.Jeopardy) (*gljeopardypb.Jeopardy, error) {
  15. err := bs.JeopardyManager.Create(*jeopardy)
  16. if err != nil {
  17. return &gljeopardypb.Jeopardy{}, err
  18. }
  19. return jeopardy, nil
  20. }
  21. func (bs JeopardyServer) DeleteJeopardy(ctx context.Context, jeopardy *gljeopardypb.Jeopardy) (*gljeopardypb.Jeopardy, error) {
  22. bs.JeopardyManager.Remove(jeopardy.GetName())
  23. return jeopardy, nil
  24. }
  25. func (bs JeopardyServer) GetJeopardy(ctx context.Context, jeopardy *gljeopardypb.Jeopardy) (*gljeopardypb.Jeopardy, error) {
  26. retJeopardy := bs.JeopardyManager.Get(*jeopardy)
  27. if jeopardy.GetName() == retJeopardy.GetName() {
  28. return &retJeopardy, nil
  29. }
  30. return &gljeopardypb.Jeopardy{}, errors.New("Jeopardy not found")
  31. }
  32. func (bs JeopardyServer) ListJeopardy(jeopardy *gljeopardypb.Jeopardy, stream gljeopardypb.JeopardyService_ListJeopardyServer) error {
  33. jeopardys := bs.JeopardyManager.List()
  34. for _, jeopardy := range jeopardys {
  35. stream.Send(&jeopardy)
  36. }
  37. return nil
  38. }
  39. func (bs JeopardyServer) ModifyJeopardy(ctx context.Context, jeopardy *gljeopardypb.Jeopardy) (*gljeopardypb.Jeopardy, error) {
  40. return &gljeopardypb.Jeopardy{}, errors.New("jeopardy not found or password missmatch")
  41. }