SHARE
TWEET

Untitled

a guest Jul 18th, 2019 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package v1
  2.  
  3. import (
  4.     "context"
  5.     "log"
  6.  
  7.     "cloud.google.com/go/firestore"
  8.     firebase "firebase.google.com/go"
  9.     v1 "github.com/ElonSalfati/events-manager/pkg/api/v1"
  10.  
  11.     "google.golang.org/api/iterator"
  12.     "google.golang.org/grpc/codes"
  13.     "google.golang.org/grpc/status"
  14.  
  15.     "github.com/mitchellh/mapstructure"
  16. )
  17.  
  18. // The accepted version of the API
  19. const apiVersion = "v1"
  20. const eventsCollectionPath = "events"
  21.  
  22. // eventsServiceServer is implementation of
  23. // v1.EventsManagerServiceServer proto intrface
  24. type eventsServiceServer struct {
  25.     client *firestore.Client
  26.     app    *firebase.App
  27. }
  28.  
  29. // NewEventsServiceServer connection to firestore app
  30. func NewEventsServiceServer(app *firebase.App, client *firestore.Client) v1.EventsManagerServiceServer {
  31.     return &eventsServiceServer{
  32.         app:    app,
  33.         client: client,
  34.     }
  35. }
  36.  
  37. // ----------------- //
  38. // Implement methods //
  39. // ----------------- //
  40.  
  41. // checkAPI verifies the API version
  42. func (s *eventsServiceServer) checkAPI(api string) error {
  43.     // If API version is an empty string force current version
  44.     if len(api) > 0 {
  45.         if apiVersion != api {
  46.             return status.Errorf(codes.Unimplemented,
  47.                 "Unsupported API version: service implements API version '%s', but asked for '%s'", apiVersion, api)
  48.         }
  49.     }
  50.  
  51.     // given version is fine
  52.     return nil
  53. }
  54.  
  55. // CreateEvent adds a new event to firestore
  56. func (s *eventsServiceServer) CreateEvent(ctx context.Context, req *v1.CreateEventRequest) (*v1.CreateEventResponse, error) {
  57.     // Check if the API version requested by the client is supported by the server
  58.     if err := s.checkAPI(req.Api); err != nil {
  59.         return nil, err
  60.     }
  61.  
  62.     doc, _, err := s.client.Collection(eventsCollectionPath).Add(ctx, req.Event)
  63.     if err != nil {
  64.         log.Printf("Faild to add event: %v", err)
  65.         return nil, err
  66.     }
  67.  
  68.     return &v1.CreateEventResponse{
  69.         Api: apiVersion,
  70.         Id:  doc.ID,
  71.     }, nil
  72. }
  73.  
  74. // ReadEvent pulls an event or set of events from firestore
  75. func (s *eventsServiceServer) ReadEvent(ctx context.Context, req *v1.ReadEventRequest) (*v1.ReadEventResponse, error) {
  76.     // Check if the API version requested by the client is supported by the server
  77.     if err := s.checkAPI(req.Api); err != nil {
  78.         return nil, err
  79.     }
  80.  
  81.     events := []*v1.Event{}
  82.     if req.Id == "all" {
  83.         iter := s.client.Collection(eventsCollectionPath).Documents(ctx)
  84.         for {
  85.             doc, err := iter.Next()
  86.             if err == iterator.Done {
  87.                 break
  88.             }
  89.             if err != nil {
  90.                 log.Printf("Failed to iterate over documents: %v", err)
  91.                 return nil, err
  92.             }
  93.  
  94.             var event v1.Event
  95.             mapstructure.Decode(doc.Data(), &event)
  96.             events = append(events, &event)
  97.         }
  98.     } else {
  99.         docRef := s.client.Collection(eventsCollectionPath).Doc(req.Id)
  100.         doc, err := docRef.Get(ctx)
  101.         if err != nil {
  102.             return nil, err
  103.         }
  104.  
  105.         var event v1.Event
  106.         mapstructure.Decode(doc.Data(), &event)
  107.         events = append(events, &event)
  108.     }
  109.  
  110.     return &v1.ReadEventResponse{
  111.         Api:    apiVersion,
  112.         Events: events,
  113.     }, nil
  114. }
  115.  
  116. // UpdateEvent upadtes a single event
  117. func (s *eventsServiceServer) UpdateEvent(ctx context.Context, req *v1.UpdateEventRequest) (*v1.UpdateEventResponse, error) {
  118.     // Check if the API version requested by the client is supported by the server
  119.     if err := s.checkAPI(req.Api); err != nil {
  120.         return nil, err
  121.     }
  122.  
  123.     _, err := s.client.Collection(eventsCollectionPath).Doc(req.Event.Id).Set(ctx, req.Event, firestore.MergeAll)
  124.     if err != nil {
  125.         log.Printf("An error has occurred: %v", err)
  126.         return nil, err
  127.     }
  128.  
  129.     return &v1.UpdateEventResponse{
  130.         Api:     apiVersion,
  131.         Updated: true,
  132.     }, nil
  133. }
  134.  
  135. // DeleteEvent removes a single event
  136. func (s *eventsServiceServer) DeleteEvent(ctx context.Context, req *v1.DeleteEventRequest) (*v1.DeleteEventResponse, error) {
  137.     // Check if the API version requested by the client is supported by the server
  138.     if err := s.checkAPI(req.Api); err != nil {
  139.         return nil, err
  140.     }
  141.  
  142.     _, err := s.client.Collection(eventsCollectionPath).Doc(req.Id).Delete(ctx)
  143.     if err != nil {
  144.         log.Printf("An error has occurred: %v", err)
  145.         return nil, err
  146.     }
  147.  
  148.     return &v1.DeleteEventResponse{
  149.         Api:     apiVersion,
  150.         Deleted: true,
  151.     }, nil
  152. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top