update recipe modal
This commit is contained in:
parent
ac45ca47d5
commit
2328da0ce8
11 changed files with 238 additions and 40 deletions
|
|
@ -4,8 +4,9 @@ import (
|
|||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"log"
|
||||
"recipe-manager/config"
|
||||
"recipe-manager/helpers"
|
||||
"recipe-manager/models"
|
||||
|
||||
"golang.org/x/oauth2"
|
||||
|
|
@ -28,28 +29,18 @@ type oauthService struct {
|
|||
|
||||
func NewOAuthService(cfg *config.ServerConfig) OAuthService {
|
||||
|
||||
file, err := os.Open("client_secret.json")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer file.Close()
|
||||
clientSecret, err := helpers.GetClientSecret("client_secret.json")
|
||||
|
||||
var clientSecret map[string]interface{}
|
||||
json.NewDecoder(file).Decode(&clientSecret)
|
||||
if err != nil {
|
||||
log.Fatalf("Unable to get client secret: %v", err)
|
||||
}
|
||||
|
||||
clientSecret.RedirectURL = cfg.ServerDomain + "/auth/google/callback"
|
||||
|
||||
return &oauthService{
|
||||
cfg: cfg,
|
||||
gConfig: &oauth2.Config{
|
||||
ClientID: clientSecret["web"].(map[string]interface{})["client_id"].(string),
|
||||
ClientSecret: clientSecret["web"].(map[string]interface{})["client_secret"].(string),
|
||||
RedirectURL: cfg.ServerDomain + "/auth/google/callback",
|
||||
Scopes: []string{"https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile"},
|
||||
Endpoint: oauth2.Endpoint{
|
||||
AuthURL: clientSecret["web"].(map[string]interface{})["auth_uri"].(string),
|
||||
TokenURL: clientSecret["web"].(map[string]interface{})["token_uri"].(string),
|
||||
},
|
||||
},
|
||||
nonce: make(map[string]map[string]string),
|
||||
cfg: cfg,
|
||||
gConfig: clientSecret,
|
||||
nonce: make(map[string]map[string]string),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
69
server/services/sheet/sheet.go
Normal file
69
server/services/sheet/sheet.go
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
package sheet
|
||||
|
||||
import (
|
||||
"context"
|
||||
"recipe-manager/helpers"
|
||||
|
||||
"golang.org/x/oauth2"
|
||||
"google.golang.org/api/option"
|
||||
"google.golang.org/api/sheets/v4"
|
||||
)
|
||||
|
||||
type SheetService interface {
|
||||
}
|
||||
|
||||
type sheetService struct {
|
||||
config *oauth2.Config
|
||||
service *sheets.Service
|
||||
}
|
||||
|
||||
func NewSheetService(ctx context.Context) (SheetService, error) {
|
||||
clientSecret, err := helpers.GetClientSecret("client_secret.json")
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
token, err := helpers.GetToken("token.json")
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
service, err := sheets.NewService(ctx, option.WithTokenSource(clientSecret.TokenSource(ctx, token)))
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &sheetService{
|
||||
config: clientSecret,
|
||||
service: service,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *sheetService) GetSheet(ctx context.Context, sheetID string) {
|
||||
spreadSheet, err := s.service.Spreadsheets.Get(sheetID).Do()
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
for _, sheet := range spreadSheet.Sheets {
|
||||
// print data collumn C
|
||||
readRange := sheet.Properties.Title + "!C:C"
|
||||
resp, err := s.service.Spreadsheets.Values.Get(sheetID, readRange).Do()
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if len(resp.Values) == 0 {
|
||||
println("No data found")
|
||||
} else {
|
||||
for _, row := range resp.Values {
|
||||
println(row[0].(string))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue