Debian Codegraph @

firestore/examples/end2end/src/apimethods/updatedocument.go

raw
1 package apimethods
2
3 import (
4 "context"
5 "fmt"
6 "fsutils"
7 firestore "go-genproto/googleapis/firestore/v1beta1"
8 "userutil"
9 )
10
11 //UpdateDocument ... Update a Document in the Database
12 func UpdateDocument() {
13 fmt.Println("\n:: Updating a Document ::\n")
14
15 client, conn := fsutils.MakeFSClient()
16
17 defer conn.Close()
18
19 fields := make(map[string]*firestore.Value)
20 fieldPaths := []string{}
21
22 fmt.Print("Enter Document Name: ")
23 docId := userutil.ReadFromConsole()
24
25 docName := "projects/firestoretestclient/databases/(default)/documents/GrpcTestData/" + docId
26
27 getDocRequest := firestore.GetDocumentRequest{
28 Name: docName,
29 }
30
31 doc, err := client.GetDocument(context.Background(), &getDocRequest)
32
33 if err != nil {
34 fmt.Println(err)
35 return
36 }
37
38 for {
39 fmt.Print("Enter Field Name (blank when finished): ")
40 fieldName := userutil.ReadFromConsole()
41 if fieldName != "" {
42 fieldPaths = append(fieldPaths, fieldName)
43 fmt.Print("Enter Field Value: ")
44 fieldValString := userutil.ReadFromConsole()
45 fields[fieldName] = &firestore.Value{
46 ValueType: &firestore.Value_StringValue{
47 StringValue: fieldValString,
48 },
49 }
50 } else {
51 break
52 }
53 }
54
55 doc.Fields = fields
56
57 docMask := firestore.DocumentMask{
58 FieldPaths: fieldPaths,
59 }
60
61 updateDocRequest := firestore.UpdateDocumentRequest{
62 Document: doc,
63 UpdateMask: &docMask,
64 }
65
66 resp, err := client.UpdateDocument(context.Background(), &updateDocRequest)
67
68 if err != nil {
69 fmt.Println(err)
70 return
71 }
72
73 fmt.Println("Successfully updated document!")
74
75 userutil.DrawDocument(*resp)
76
77 }