diff --git a/internal/controllers/user_controller.go b/internal/controllers/user_controller.go index fbacdd0..8f72cc1 100644 --- a/internal/controllers/user_controller.go +++ b/internal/controllers/user_controller.go @@ -106,6 +106,11 @@ func CreateUser(ctx echo.Context) error { } }() + err = services.WriteUserToGoogleSheet(user) + if err != nil { + slog.Error(err.Error()) + } + return ctx.JSON(http.StatusOK, map[string]string{ "message": "user creation was successful", "status": "success", diff --git a/internal/database/sheets.go b/internal/database/sheets.go index c6b2790..ef59067 100644 --- a/internal/database/sheets.go +++ b/internal/database/sheets.go @@ -10,7 +10,7 @@ import ( "google.golang.org/api/sheets/v4" ) -var SheetsService *sheets.Service +var SRV *sheets.Service func InitialiseGoogleSheetsClient() (*sheets.Service, error) { ctx := context.Background() @@ -26,10 +26,10 @@ func InitialiseGoogleSheetsClient() (*sheets.Service, error) { } client := config.Client(ctx) - SheetsService, err = sheets.NewService(ctx, option.WithHTTPClient(client)) + SRV, err = sheets.NewService(ctx, option.WithHTTPClient(client)) if err != nil { return nil, fmt.Errorf("unable to create Google Sheets client: %v", err) } - return SheetsService, nil + return SRV, nil } diff --git a/internal/services/user/sheets.go b/internal/services/user/sheets.go new file mode 100644 index 0000000..0a19e2e --- /dev/null +++ b/internal/services/user/sheets.go @@ -0,0 +1,49 @@ +package services + +import ( + "context" + "fmt" + "log" + + "github.com/CodeChefVIT/devsoc-backend-24/internal/database" + "github.com/CodeChefVIT/devsoc-backend-24/internal/models" + "google.golang.org/api/sheets/v4" +) + +func WriteUserToGoogleSheet(user models.User) error { + spreadsheetID := "1IBBUHGO9vst-bz1PbdVnSWbqgkyyC8IwZb4wNisUylQ" + sheetName := "Sheet1" + + values := [][]interface{}{ + { + user.ID, + user.FirstName, + user.LastName, + user.RegNo, + user.Gender, + user.Phone, + user.Email, + user.TeamID, + user.IsVitian, + user.City, + user.State, + user.College, + user.IsVerified, + }, + } + valueRange := &sheets.ValueRange{ + Values: values, + } + + _, err := database.SRV.Spreadsheets.Values.Append(spreadsheetID, sheetName, valueRange). + ValueInputOption("RAW"). + InsertDataOption("INSERT_ROWS"). + Context(context.Background()). + Do() + if err != nil { + log.Printf("unable to write to Google Sheets: %v", err) + return fmt.Errorf("unable to write to Google Sheets: %v", err) + } + + return nil +}