Parse store current user in class - android

I am tryign to store the current username in the class and having such a tough time.
This is only part of the code
String usernameNewbet = currentUser.getUsername().toString();
bets.put("EndDate", endDate);
bets.put("EndTime", actualTimeString);
bets.put("Player_Pointer", usernameNewbet);
But when I am trying to save I am getting this error.

You probably created a Pointer column named "Player_Pointer" and tried inserting a String into it. When saving pointers in Parse, you're supposed to provide the actual object, in this case the User itself, to the field, and not the objectId.
In your case, change this:
String usernameNewbet = currentUser.getUsername().toString();
bets.put("Player_Pointer", usernameNewbet);
To this:
bets.put("Player_Pointer", currentUser);

Related

Check which coupon is used - Android

I'm making an app that uses coupons. I want to know programmatically what is the coupon code that the user has entered and based on the input do the stuff.
For example, I have added manually to Cloud Firestore a code for signing up. How do I check in the app that this is the code used for signing up and the user has entered? And based on that coupon code do what should be done.
Database image
Any help?
Get the coupon code from the user in a EditText
Get the coupon code from Firebase
Compare both with
String inputCode = editText.getText().toString();
String cloudCode = task.getString("code");
if (inputCode.equals(cloudCode)){
//do your thing
}
here task is from firebaseFirestore.collection().get();
Edit 1:
Things to do before implementing Coupons
All coupon code should follow a Pattern
Pattern eg - SALE0040 or SIGN0050 or DISC0020
Patter like - ABCD0123 or anything suits your needs
Your code will understand the type by looking at 1st half - SIGN and it will understand how much discount to give with 2nd half - 0050
code -
String couponCode = task.getString("coupon");
String userCode = editText.getText().toString();
String type = couponCode.subString(0,4);
int discount = Integer.valueOf(couponCode.subString(4,couponCode.length()));
Same you can do with the user entered code
Now you have both values of both Coupons Discount/Coupon type and Discount amount
Assuming you have an EditText, to get the code that is entered by the user you should use the following line of code:
String coupon = editText.getText().toString().trim();
Now having this coupon you can search the database to see if it actually exists, using the following query:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference couponsRef = rootRef.collection("Coupons");
Query query = couponsRef.whereEqualTo("coupon", coupon);
query.get().addOnCompleteListener(/* ... */);
Edit:
If the code is only for signup, then you should add a new property under your coupon object named type and use the following query:
Query query = couponsRef.whereEqualTo("coupon", coupon).whereEqualTo("type", "signup");
Another way to solve this might be to get the name of the coupon and check if it starts with SIGNUPCOUPON.

How to store long value as string

I want to store a long value as string for various reasons so im trying to convert serverValue.TIMESTAMP to string but not able to. Can someone helpe me out?
Code I tried
mDatabaseReference.child("Chats").child(MessageSenderId).child(MessageRecieverId)
.child("Seen").setValue(String.valueOf(ServerValue.TIMESTAMP));
Result
"{.sv=timestamp}"
The database is storing the value like that and i have no idea why. I just can't store it as a long value, so I need to convert it to string because while recieving all the values should be String due to some feature I have. So can someone help me in converting this long to string value?
The ServerValue.TIMESTAMP is a so-called marker that you send to the database server. The database server then expands that into the actual timestamp. As you can see in your string version, the marker is actually a map.
There is no way to instruct the database to store the ServerValue.TIMESTAMP value as a string. Your options are:
Modify the feature to deal with the actual numeric value.
Store the client-side timestamp as a string from your app, with setValue(String.valueOf(System.currentTimeMillis()))
Write a Cloud Function that triggers on the server-side timestamp and then converts it to a string.

How to add server time using FirebaseStore timestamp?

If I'm using Firebase data-base I can set server-time as below:
user.put("time", ServerValue.TIMESTAMP);
And then update it.
But if I used the same one with FireStore it doesn't succeed, Also I don't need to implement firebase-data-base in my gradle for only this purpose.
I can see that there is ServerTimestamp.class related to FireStore but I don't know how to use it.
Thanks in advance.
According to docs it would be something like this:
#ServerTimestamp Date time;
If null it will have the server-generated timestamp, so you don't need to do set the value for it.
Ref:
Annotation used to mark a Date field to be populated with a server timestamp. If a POJO being written contains null for a #ServerTimestamp-annotated field, it will be replaced with a server-generated timestamp.
(https://firebase.google.com/docs/reference/android/com/google/firebase/firestore/ServerTimestamp)
Edit:
Or when working with a map type object directly:
DocumentReference docRef = db.collection("objects").document("some-id");
// Update the timestamp field with the value from the server
Map<String,Object> updates = new HashMap<>();
updates.put("timestamp", FieldValue.serverTimestamp());
Ref:
https://firebase.google.com/docs/firestore/manage-data/add-data#update_fields_in_nested_objects

compare createdAt field with date only (not datetime) on parse.com in android

I am using parse.com in one of my app. I can insert/fetch data to/from parse. Everything works fine. But I want use createdAt field to fetch data.
I have two queries.
Fetch records which are created today.
Fetch records which are created before today.
Problem is parse compares createdAt fields using date-time so I can not use new Date() in query parameter. Is there any way to compare createdAt field by date only?
This question is similar to mine. But is there any standard method ?
As I understand you, You need function something like a keyword "like" which is used in Database to check if a substring exists in a column. So, we do have a method in Parse's Android APIs.
For that you need to call whereContains(String key, String substring)
which will add a constraint for finding string values that contain a provided string.
So, you can write something like this:
query.whereContains("createdAt", "25-05-2015");
That'll only retrieve those data which fall on this date and won't include time.
Doc Reference: https://www.parse.com/docs/android/api/#ParseQuery/whereContains
EDIT:
As createdAt column is Date type, It'll accept a Date variable only. In that case above mentioned function won't work. So, achieving that we will have to use whereGreaterThan and whereLessThan. Sample code snippet is:
Date midnight = new Date();
midnight.setHours(0);
midnight.setMinutes(0);
midnight.setSeconds(0);
Date elevenfiftynine = new Date();
elevenfiftynine.setHours(23);
elevenfiftynine.setMinutes(59);
elevenfiftynine.setSeconds(59);
query.whereGreaterThan(Constants.CREATED_AT_KEY, midnight);
query.whereLessThan(Constants.CREATED_AT_KEY, elevenfiftynine);
Reference: https://www.parse.com/questions/android-api-query-to-get-all-objects-created-today

Android get data from the parse table (json file) (imported in parse.com-data browser)

I have one json file, that I imported in parse.com->data browser->import partition. I can get the String and image value from the table but I have no idea about how to get following values. First column have name chapters of type array look like one field following,
[{"__type":"Pointer","className":"Chapter","objectId":"BCr3uAnapV"}]
how to get above value and second column have, name user of type object look like following,
{"password":"xxx"}
Please any one help me for above, I have tried Googling but it did not help.
EDIT:
See following screen shot: arrow display column 1) chapter and have another column 2) user I want to fetch (get ) that for e.g. if we want to get data of String type like: String provider = (String) objectList.get(i).get("provider"); this manner this way I want to above data, below my screen shot:
This line is a relation (of type Pointer) to another Class in Parse:
[{"__type":"Pointer","className":"Chapter","objectId":"BCr3uAnapV"}]
The class being Chapter. You can make sure this object is fetched together with the object pointing to it using "include":
query.include("chapter");
When you query for the other object, using query.include on pointer relations will ensure these related objects are fetched as well.

Categories

Resources