How to restrict users from writing more than once in a database

旧巷老猫 提交于 2020-02-05 05:07:27

问题


I am working on a Bus booking app. So whenever a user books a ride I will store his credentials(name, email) for that particular ride. I need to restrict the number of bookings for that ride(like only 20 per ride). To do this I am using Firebase transactions. But how to restrict them from booking more than once for a particular time in a day. Can anyone tell me how? Below is my code for database(mref1 is the location where I want to store the number of bookings)

private DatabaseReference mTime1;
private DatabaseReference mTime2;
private DatabaseReference mCount1;
private DatabaseReference mCount2;
private FirebaseAuth mAuth;
private static final String TAG = "BookingActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_booking);

    mAuth = FirebaseAuth.getInstance();

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mDatabase1 = FirebaseDatabase.getInstance().getReference().child("Time1");
    mDatabase2 = FirebaseDatabase.getInstance().getReference().child("Time2");
    mref1 = FirebaseDatabase.getInstance().getReference().child("Count@Time1");
    mref2 = FirebaseDatabase.getInstance().getReference().child("Count@Time2");

    findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Book(mDatabase1,mref1);
        }
    });

    findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Book(mDatabase2,mref2);
        }
    });

}


public void Book(DatabaseReference mDatabase,DatabaseReference mref) {

    final FirebaseUser user = mAuth.getCurrentUser();
    HashMap<String,String>datamap = new HashMap<>();
    Long tsLong = System.currentTimeMillis()/1000;
    String ts = tsLong.toString();

    if(user!=null) {
        datamap.put("Name", user.getDisplayName());
        datamap.put("Email", user.getEmail());
        datamap.put("Timestamp",ts);
    }

    mDatabase.push().setValue(datamap);
    Update(mref);
    Toast.makeText(BookingActivity.this, "Booked Successfully", Toast.LENGTH_SHORT).show();

}

public void Update(DatabaseReference mDatabase) {

    mDatabase.runTransaction(new Transaction.Handler() {
        @NonNull
        @Override
        public Transaction.Result doTransaction(@NonNull MutableData mutableData) {

            Integer CurrentValue = mutableData.getValue(Integer.class);
            mutableData.setValue(CurrentValue+1);
            return Transaction.success(mutableData);
        }

        @Override
        public void onComplete(@Nullable DatabaseError databaseError, boolean b, @Nullable DataSnapshot dataSnapshot) {

            Log.d(TAG, "Updating count transaction is completed.");
        }
    });
}

my database structure is


回答1:


To solve this, you should create another node named users in which you should add user objects. Each user object should have a property named numberOfRides with the default value of 0 (zero). After the first ride, increase that numer to 1 (one) and everytime a user wants to take another ride, check the value of this property. If it is 0 let the user get the ride otherwise, restrict him from getting the ride. Then, I recommend you write a function in Cloud Functions for Firebase that can set the value for the numberOfRides property for each user to 0 (zero). In the end just set a cron job to call this function by midnight. That's it!



来源:https://stackoverflow.com/questions/51402277/how-to-restrict-users-from-writing-more-than-once-in-a-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!