如何限制用户在数据库中多次写入 [英] How to restrict users from writing more than once in a database

查看:59
本文介绍了如何限制用户在数据库中多次写入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发公交车预订应用程序.因此,每当用户预订行程时,我都会存储该特定行程的凭据(名称,电子邮件).我需要限制该乘车的预订数量(例如每次乘车仅20个).为此,我正在使用Firebase事务.但是如何限制他们在一天中的特定时间多次预订.谁能告诉我如何?下面是我的数据库代码(mref1是我要存储预订数量的位置)

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.");
        }
    });
}

我的数据库结构为

推荐答案

要解决此问题,您应该创建另一个名为users的节点,并在其中添加用户对象.每个用户对象都应具有一个名为numberOfRides的属性,其默认值为0(零).第一次骑行之后,将该数字增加到1(一个),并且每次用户想要进行另一次骑行时,请检查此属性的值.如果它是0,则让用户乘车,否则限制其乘车.然后,建议您在 Firebase的云函数中编写一个函数,该函数可以为每个用户的numberOfRides属性为0(零).最后,只需设置 cron作业即可在午夜之前调用此函数.就是这样!

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!

这篇关于如何限制用户在数据库中多次写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆