diff --git a/internal/calendar/option.go b/internal/calendar/option.go new file mode 100644 index 0000000..5ac7438 --- /dev/null +++ b/internal/calendar/option.go @@ -0,0 +1,18 @@ +package calendar + +import ( + "fmt" +) + +type Option func(*Service) error + +func WithCalendar(key string, calendarId string) Option { + return func(s *Service) error { + _, err := s.CalendarList.Get(calendarId).Do() + if err != nil { + return fmt.Errorf("cannot find calendar entry with id %q: %w", calendarId, err) + } + s.calIds[key] = calendarId + return nil + } +} diff --git a/internal/calendar/service.go b/internal/calendar/service.go index 130a02c..3acb3eb 100644 --- a/internal/calendar/service.go +++ b/internal/calendar/service.go @@ -3,7 +3,6 @@ package calendar import ( "context" "encoding/json" - "errors" "fmt" "log" "net/http" @@ -19,10 +18,11 @@ import ( const tokFile = "token.json" type Service struct { + calIds map[string]string *calendar.Service } -func NewService(ctx context.Context, credJson string) *Service { +func NewService(ctx context.Context, credJson string, opts ...Option) (*Service, error) { b := []byte(credJson) config, err := google.ConfigFromJSON(b, calendar.CalendarReadonlyScope) @@ -36,27 +36,35 @@ func NewService(ctx context.Context, credJson string) *Service { log.Fatalf("Unable to retrieve Calendar client: %v", err) } - return &Service{srv} + s := &Service{ + Service: srv, + calIds: make(map[string]string), + } + + for _, opt := range opts { + err := opt(s) + if err != nil { + return nil, err + } + } + + return &Service{Service: srv}, nil } // TODO: implement create event, list events in a period, delete event - -// TODO: Delete this. It is just for Proof of Concept -func (s Service) ListNextEvents() (*calendar.Events, error) { - t := time.Now().Format(time.RFC3339) - events, err := s.Events.List("primary").ShowDeleted(false). - SingleEvents(true).TimeMin(t).MaxResults(10).OrderBy("startTime").Do() - if err != nil { - return nil, fmt.Errorf("unable to retrieve next ten of the user's events: %w", err) - } - if len(events.Items) == 0 { - return nil, errors.New("no upcoming events found") - } else { - // TODO: return a custom type - return events, nil +func (s *Service) Create(from, to time.Time) (*calendar.Event, error) { + l := s.CalendarList.List() + r, e := l.Do() + log.Println(e) + for _, c := range r.Items { + log.Printf("%+v: %s", c.Summary, c.Id) } + return nil, nil } +func (s *Service) List(from, to time.Time) (*calendar.Events, error) { return nil, nil } +func (s *Service) Delete(id int) error { return nil } + // Retrieve a token, saves the token, then returns the generated client. func getClient(ctx context.Context, config *oauth2.Config) *http.Client { // The file token.json stores the user's access and refresh tokens, and is diff --git a/internal/server/server.go b/internal/server/server.go index 37df58e..a883933 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -20,6 +20,7 @@ import ( "github.com/rjNemo/rentease/config" "github.com/rjNemo/rentease/internal/auth" "github.com/rjNemo/rentease/internal/booking" + "github.com/rjNemo/rentease/internal/calendar" "github.com/rjNemo/rentease/internal/pdf" ) @@ -28,6 +29,7 @@ type Server struct { bs *booking.Service as *auth.Service ps *pdf.PdfService + cs *calendar.Service hc *config.Host addr string secretKey string @@ -90,7 +92,7 @@ func WithApiKey(apiKey string) Option { } } -func New(bs *booking.Service, as *auth.Service, ps *pdf.PdfService, hc *config.Host, opts ...Option) (*Server, error) { +func New(bs *booking.Service, as *auth.Service, ps *pdf.PdfService, cs *calendar.Service, hc *config.Host, opts ...Option) (*Server, error) { option := new(options) for _, opt := range opts { err := opt(option) @@ -104,6 +106,7 @@ func New(bs *booking.Service, as *auth.Service, ps *pdf.PdfService, hc *config.H bs: bs, as: as, ps: ps, + cs: cs, hc: hc, addr: fmt.Sprintf("0.0.0.0:%d", *option.port), secretKey: *option.secretKey, diff --git a/main.go b/main.go index 3f21675..3204cf6 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "context" "embed" "fmt" + "log" "os" "os/signal" "strconv" @@ -17,6 +18,7 @@ import ( "github.com/rjNemo/rentease/config" "github.com/rjNemo/rentease/internal/auth" "github.com/rjNemo/rentease/internal/booking" + "github.com/rjNemo/rentease/internal/calendar" "github.com/rjNemo/rentease/internal/pdf" "github.com/rjNemo/rentease/internal/server" ) @@ -72,6 +74,18 @@ func run(c context.Context, getEnv func(string) string) error { return fmt.Errorf("error starting auth service %s", err) } + creds := os.Getenv("CALENDAR_CREDENTIALS") + t2Id := os.Getenv("CALENDAR_ID_T2") + t3Id := os.Getenv("CALENDAR_ID_T3") + + cs, err := calendar.NewService(ctx, creds, + calendar.WithCalendar("T2", t2Id), + calendar.WithCalendar("T3", t3Id), + ) + if err != nil { + log.Fatalf("cannot build calendar service: %s", err) + } + p := getEnv("PORT") port, err := strconv.Atoi(p) if err != nil { @@ -85,6 +99,7 @@ func run(c context.Context, getEnv func(string) string) error { booking.NewService(db), as, ps, + cs, config.NewHost(), server.WithPort(port), server.WithFileSystem(static),