Skip to content

Commit

Permalink
Merge pull request #10 from prateek-srivastava001/master
Browse files Browse the repository at this point in the history
feat: sheets support
  • Loading branch information
Mr-Emerald-Wolf authored Feb 29, 2024
2 parents 9bdc0ba + 414e922 commit 8917f5c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
5 changes: 5 additions & 0 deletions internal/controllers/user_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions internal/database/sheets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
}
49 changes: 49 additions & 0 deletions internal/services/user/sheets.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 8917f5c

Please sign in to comment.