| 1 |
package apimethods |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"fsutils" |
| 7 |
firestore "go-genproto/googleapis/firestore/v1beta1" |
| 8 |
"userutil" |
| 9 |
) |
| 10 |
|
| 11 |
//CreateDocument ... Create a New Document |
| 12 |
func CreateDocument() { |
| 13 |
fmt.Println("\n:: Creating a New Document ::\n") |
| 14 |
|
| 15 |
client, conn := fsutils.MakeFSClient() |
| 16 |
|
| 17 |
defer conn.Close() |
| 18 |
|
| 19 |
fields := make(map[string]*firestore.Value) |
| 20 |
|
| 21 |
fmt.Print("Enter Document Name: ") |
| 22 |
docId := userutil.ReadFromConsole() |
| 23 |
|
| 24 |
for { |
| 25 |
fmt.Print("Enter Field Name (blank when finished): ") |
| 26 |
fieldName := userutil.ReadFromConsole() |
| 27 |
if fieldName != "" { |
| 28 |
fmt.Print("Enter Field Value: ") |
| 29 |
fieldValString := userutil.ReadFromConsole() |
| 30 |
fields[fieldName] = &firestore.Value{ |
| 31 |
ValueType: &firestore.Value_StringValue{ |
| 32 |
StringValue: fieldValString, |
| 33 |
}, |
| 34 |
} |
| 35 |
} else { |
| 36 |
break |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
doc := firestore.Document{ |
| 41 |
Fields: fields, |
| 42 |
} |
| 43 |
|
| 44 |
createDocRequest := firestore.CreateDocumentRequest{ |
| 45 |
Parent: "projects/firestoretestclient/databases/(default)/documents", |
| 46 |
CollectionId: "GrpcTestData", |
| 47 |
DocumentId: docId, |
| 48 |
Document: &doc, |
| 49 |
} |
| 50 |
|
| 51 |
resp, err := client.CreateDocument(context.Background(), &createDocRequest) |
| 52 |
if err != nil { |
| 53 |
fmt.Println(err) |
| 54 |
return |
| 55 |
} |
| 56 |
|
| 57 |
fmt.Println("Successfully created document!") |
| 58 |
userutil.DrawDocument(*resp) |
| 59 |
|
| 60 |
} |