Skip to content

Commit

Permalink
Implementando rotina GetNextEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
mffonseca committed Jul 26, 2024
1 parent 030e940 commit ce92b0f
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions backend/pkg/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package http
import (
"context"
docs "faladev/cmd/docs"
"faladev/config"
"faladev/internal/auth"
"faladev/internal/models"
"faladev/internal/services"
"fmt"
"html/template"
Expand All @@ -27,13 +25,19 @@ type App struct {
config *oauth2.Config
calendarService services.CalendarService
emailService services.EmailService
studentService services.StudentService
tokenService services.TokenService
eventService services.EventService
}

func NewApp(config *oauth2.Config, calendar services.CalendarService, email services.EmailService) *App {
func NewApp(config *oauth2.Config, calendar services.CalendarService, email services.EmailService, studentService services.StudentService, tokenService services.TokenService, eventService services.EventService) *App {
return &App{
config: config,
calendarService: calendar,
emailService: email,
studentService: studentService,
tokenService: tokenService,
eventService: eventService,
}
}

Expand Down Expand Up @@ -73,12 +77,12 @@ func (app *App) EventHandler(c *gin.Context) {
email := c.PostForm("email")
phone := c.PostForm("phone")

if err := models.InsertOrUpdateStudent(name, email, phone); err != nil {
if err := app.studentService.InsertOrUpdateStudent(name, email, phone); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error inserting record into database: " + err.Error()})
return
}

token, err := models.LoadToken()
token, err := app.tokenService.GetToken()

if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to load token: " + err.Error()})
Expand All @@ -94,7 +98,7 @@ func (app *App) EventHandler(c *gin.Context) {
return
}

err = models.SaveToken(token)
err = app.tokenService.CreateToken(token)

if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save token: " + err.Error()})
Expand All @@ -109,8 +113,14 @@ func (app *App) EventHandler(c *gin.Context) {
return
}

eventID := config.GetEventGoogleMeet()
eventDetails, err := app.calendarService.AddGuestToEvent(context.Background(), calendarService, eventID, email)
event, err := app.eventService.GetNextEvent()

if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error getting next event: " + err.Error()})
return
}

eventDetails, err := app.calendarService.AddGuestToEvent(context.Background(), calendarService, event.Location, email)

if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error getting event details: " + err.Error()})
Expand Down Expand Up @@ -159,7 +169,7 @@ func (app *App) OAuthCallbackHandler(c *gin.Context) {
return
}

err = models.SaveToken(token)
err = app.tokenService.CreateToken(token)

if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Unable to save token: %v", err)})
Expand All @@ -171,9 +181,9 @@ func (app *App) OAuthCallbackHandler(c *gin.Context) {
c.Redirect(http.StatusSeeOther, "/")
}

func StartServer(appAuthConfig config.AuthConfig) {
func StartServer(appOAuth2Config *oauth2.Config, studentService services.StudentService, calendarService services.CalendarService, emailService services.EmailService, tokenService services.TokenService, eventService services.EventService) {

app := NewApp(appAuthConfig.Config, services.NewGoogleCalendarService(), services.NewGmailService(appAuthConfig.Config, appAuthConfig.Token))
app := NewApp(appOAuth2Config, calendarService, emailService, studentService, tokenService, eventService)

router := gin.Default()

Expand Down

0 comments on commit ce92b0f

Please sign in to comment.