grpc.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package BingoManager
  2. import (
  3. "context"
  4. "errors"
  5. glbingopb "git.alfi.li/gamelang/protobuf/golang-bingo"
  6. )
  7. type BingoServer struct {
  8. glbingopb.UnimplementedBingoServiceServer
  9. BingoManager *BingoManager
  10. }
  11. func NewBingoServer(um *BingoManager) *BingoServer {
  12. return &BingoServer{BingoManager: um}
  13. }
  14. func (bs BingoServer) CreateBingo(ctx context.Context, bingo *glbingopb.Bingo) (*glbingopb.Bingo, error) {
  15. err := bs.BingoManager.Create(*bingo)
  16. if err != nil {
  17. return &glbingopb.Bingo{}, err
  18. }
  19. return bingo, nil
  20. }
  21. func (bs BingoServer) DeleteBingo(ctx context.Context, bingo *glbingopb.Bingo) (*glbingopb.Bingo, error) {
  22. bs.BingoManager.Remove(bingo.GetName())
  23. return bingo, nil
  24. }
  25. func (bs BingoServer) GetBingo(ctx context.Context, bingo *glbingopb.Bingo) (*glbingopb.Bingo, error) {
  26. retBingo := bs.BingoManager.Get(*bingo)
  27. if bingo.GetName() == retBingo.GetName() {
  28. return &retBingo, nil
  29. }
  30. return &glbingopb.Bingo{}, errors.New("Bingo not found")
  31. }
  32. func (bs BingoServer) ListBingo(bingo *glbingopb.Bingo, stream glbingopb.BingoService_ListBingoServer) error {
  33. bingos := bs.BingoManager.List()
  34. for _, bingo := range bingos {
  35. stream.Send(&bingo)
  36. }
  37. return nil
  38. }
  39. func (bs BingoServer) ModifyBingo(ctx context.Context, bingo *glbingopb.Bingo) (*glbingopb.Bingo, error) {
  40. return &glbingopb.Bingo{}, errors.New("bingo not found or password missmatch")
  41. }