-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabaseHelper(Updated)
235 lines (179 loc) · 8.21 KB
/
DatabaseHelper(Updated)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package com.example.bilkentcompanion;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import com.example.bilkentcompanion.R;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
public class DatabaseHelper {
public static String getUID() {
return FirebaseAuth.getInstance().getCurrentUser().getUid();
}
public static String getName(String uid) {
return FirebaseDatabase.getInstance().getReference().child("Users").child(uid)
.child("name").toString();
}
public static String getEmail(String uid) {
return FirebaseDatabase.getInstance().getReference().child("Users").child(uid)
.child("email").toString();
}
public static String getQuestion(String uid) {
return FirebaseDatabase.getInstance().getReference().child("Users").child(uid)
.child("question").toString();
}
public static String getAnswer(String uid) {
return FirebaseDatabase.getInstance().getReference().child("Users").child(uid)
.child("answer").toString();
}
//LostPost
public static String getLostPostUID(String postID) {
return FirebaseDatabase.getInstance().getReference().child("LostPost")
.child(postID).child("uid").toString();
}
public static String[] getLostPostTags(String postID) {
String s = FirebaseDatabase.getInstance().getReference().child("LostPost")
.child(postID).child("tags").toString();
return s.split("\\W+");
}
public static String getLostPostDate(String postID) {
return FirebaseDatabase.getInstance().getReference().child("LostPost")
.child(postID).child("date").toString();
}
public static String getLostPostName(String postID) {
return FirebaseDatabase.getInstance().getReference().child("LostPost")
.child(postID).child("name").toString();
}
public static String getLostPostCategory(String postID) {
return FirebaseDatabase.getInstance().getReference().child("LostPost")
.child(postID).child("category").toString();
}
public static String initialiseLostPost(String[] infos) {
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("LostPost")
.push();
HashMap<String, String> properties = new HashMap<>();
properties.put("name", infos[0]);
properties.put("uid", infos[1]);
properties.put("category", infos[2]);
properties.put("tags", infos[3]);
properties.put("date", infos[4]);
properties.put("key", ref.getKey());
ref.setValue(properties);
return ref.toString();
}
public static String initialiseSellPost(String[] infos) {
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("SellPost").push();
HashMap<String, String> properties = new HashMap<>();
properties.put("name", infos[0]);
properties.put("uid", infos[1]);
properties.put("category", infos[2]);
properties.put("tags", infos[3]);
properties.put("price", infos[4]);
properties.put("key", ref.getKey());
ref.setValue(properties);
return ref.toString();
}
public static void setLostPostName(String postID, String data) {
FirebaseDatabase.getInstance().getReference().child("LostPost")
.child(postID).child("name").setValue(data);
}
public static void setLostPostUID(String postID, String data) {
FirebaseDatabase.getInstance().getReference().child("LostPost")
.child(postID).child("uid").setValue(data);
}
public static void setLostPostCategory(String postID, String data) {
FirebaseDatabase.getInstance().getReference().child("LostPost")
.child(postID).child("category").setValue(data);
}
public static void setLostPostTags(String postID, String data) {
FirebaseDatabase.getInstance().getReference().child("LostPost")
.child(postID).child("tags").setValue(data);
}
public static void setLostPostDate(String postID, String data) {
FirebaseDatabase.getInstance().getReference().child("LostPost")
.child(postID).child("date").setValue(data);
}
private static ArrayList<String> lostPosts;
public static ArrayList<String> getAllLostPosts() {
FirebaseDatabase.getInstance().getReference().child("LostPost")
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
lostPosts = new ArrayList<>();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
lostPosts.add((String) snapshot.getValue());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return lostPosts;
}
//SellPost
public static String getSellPostUID(String postID) {
return FirebaseDatabase.getInstance().getReference().child("SellPost")
.child(postID).child("uid").toString();
}
public static String[] getSellPostTags(String postID) {
String s = FirebaseDatabase.getInstance().getReference().child("SellPost")
.child(postID).child("tags").toString();
return s.split("\\W+");
}
/*public static String getSellInfo(String postID, String info) {
final String information = info;
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("SellPost").child(postID);
ref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
String key = dataSnapshot.child(information).getValue(String.class);
return key;
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}*/
public static String getSellPostName(String postID) {
return FirebaseDatabase.getInstance().getReference().child("SellPost")
.child(postID).child("name").toString();
}
public static String getSellPostCategory(String postID) {
return FirebaseDatabase.getInstance().getReference().child("SellPost")
.child(postID).child("category").toString();
}
public static void setSellPostName(String postID, String data) {
FirebaseDatabase.getInstance().getReference().child("SellPost")
.child(postID).child("name").setValue(data);
}
public static void setSellPostUID(String postID, String data) {
FirebaseDatabase.getInstance().getReference().child("SellPost")
.child(postID).child("uid").setValue(data);
}
public static void setSellPostCategory(String postID, String data) {
FirebaseDatabase.getInstance().getReference().child("SellPost")
.child(postID).child("category").setValue(data);
}
public static void setSellPostTags(String postID, String data) {
FirebaseDatabase.getInstance().getReference().child("SellPost")
.child(postID).child("tags").setValue(data);
}
public static void setSellPostPrice(String postID, String data) {
FirebaseDatabase.getInstance().getReference().child("SellPost")
.child(postID).child("price").setValue(data);
}
private static ArrayList<String> keysSellPosts;
}