springboot add to collection firebase

Solutions on MaxInterview for springboot add to collection firebase by the best coders in the world

showing results for - "springboot add to collection firebase"
Juan
14 Oct 2020
1// Assuming that you have the google sdk setup correctly
2package za.co.migal.service;
3
4import com.google.api.core.ApiFuture;
5import com.google.cloud.firestore.DocumentReference;
6import com.google.cloud.firestore.Firestore;
7import com.google.firebase.cloud.FirestoreClient;
8import org.springframework.stereotype.Service;
9
10@Service()
11public class MyPojoService {
12  private Firestore firestore;
13  private static final String MY_POJO_COLLECTION = "myPojo";
14  
15  public boolean addMyPojo(MyPojo myPojo) {
16    firestore = FirestoreClient.getFirestore();
17    ApiFuture<DocumentReference> myPojoRef = firestore.
18      collection(MY_POJO_COLLECTION).
19      add(myPojo);
20    return myPojoRef.isDone();
21  }
22}
23
24// MyPojo class
25package za.co.migal.pojo;
26
27import com.google.api.core.ApiFuture;
28import com.google.cloud.firestore.DocumentReference;
29import com.google.cloud.firestore.Firestore;
30import com.google.firebase.cloud.FirestoreClient;
31
32// FYI
33// I usually use Lombok @Data instead of adding getters and setters
34// @Data removes boiler point code, it just makes the code alot cleaner 
35public class MyPojo {
36  private String myVariable;
37  private int myIntVariable;
38  
39  public String getMyVariable() {
40    return myVariable;
41  }
42  public void setMyVariable(String myVariable) {
43    this.myVariable = myVariable;
44  }
45  public int getMyIntVariable() {
46    return myIntVariable;
47  }
48  public void setMyIntVariable(int myIntVariable) {
49    this.myIntVariable = myIntVariable;
50  }
51  
52  @Override
53  public String toString() {
54    return "Option{" +
55      "myVariable='" + myVariable + '\'' +
56      ", myIntVariable=" + myIntVariable +
57      '}';
58  }
59  
60  @Override
61  public boolean equals(Object o) {
62    if (this == o) return true;
63    if (o == null || getClass() != o.getClass()) return false;
64    Option option = (Option) o;
65    return myIntVariable == option.myIntVariable &&
66      myVariable.equals(option.myVariable);
67  }
68
69  @Override
70  public int hashCode() {
71    return Objects.hash(myVariable, myIntVariable);
72  }
73}