firestore中每个用户一次上传

icomxhvb  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(265)

这个问题在这里已经有了答案

防止firestore规则中的重复条目不起作用(1个答案)
14分钟前关门。
我有一个应用程序,你可以上传优惠,人们可以购买它们(如fiverr),我正在使用firestore存储优惠。
每个用户都可以上传任意数量的优惠,但由于某些原因,它不允许每个用户上传多个优惠。
我根本没有收到任何错误消息。
创建优惠活动:

package com.example.create4me;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class CreateOfferActivity extends AppCompatActivity {
Button finish, uploadImg;
EditText title, desc, price;
ImageView image;
String name, uuid, offerID;
FirebaseUser user;
FirebaseFirestore db = FirebaseFirestore.getInstance();
FirebaseAuth auth = FirebaseAuth.getInstance();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_offer);
        //Hide the Action Bar
        getSupportActionBar().hide();
        //Initialize all the views
        title = findViewById(R.id.createOfferActivityTitle);
        desc = findViewById(R.id.createOfferActivityDescription);
        image = findViewById(R.id.createOfferActivity2Image);
        price = findViewById(R.id.createOfferActivityPrice);
        finish = findViewById(R.id.createOfferActivity2FinishButton);
        uploadImg = findViewById(R.id.createOfferActivity2UploadButton);
        //Create a uuid for the offer
        offerID = uuidGen();
        //Get the user's uuid
        user = auth.getCurrentUser();
        uuid = user.getUid();
        //Get the user's username
        db.collection("users")
                .whereEqualTo("uuid", uuid)
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {
                            for (QueryDocumentSnapshot doc : task.getResult()) {
                                if(doc.getData().get("uuid").equals(user.getUid().toString())){
                                    name = doc.getData().get("username").toString();
                                }
                            }
                        } else {
                            Toast.makeText(CreateOfferActivity.this, "Couldn't get the username from Database\nPlease restart the application", Toast.LENGTH_LONG).show();
                            Intent intent = new Intent(CreateOfferActivity.this, HomeActivity.class);
                            startActivity(intent);
                        }
                    }
                });
        //When we finish, we click on the finish button and come here
        finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    sendData();

            }
        });
    }

/*
This function basically gets all the data from the views and the id's we got in onCreate and puts them in a Firestore Collection.
 */
    public void sendData(){
        if(checkPrice(price.getText().toString())){
            //Placing all the data to the HashMap
            Map<String, Object> offer = new HashMap<>();
            offer.put("title", title.getText().toString());
            offer.put("description", desc.getText().toString());
            offer.put("price", price.getText().toString());
            offer.put("madeByID", user.getUid());
            offer.put("madeByName", name.toString());
            offer.put("offerID", offerID.toString());
            //Connecting to the collection and placing the HashMap in it
            db.collection("offers")
                    .add(offer)
                    .addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
                        @Override
                        public void onComplete(@NonNull Task<DocumentReference> task) {
                            if(task.isSuccessful()){
                                Toast.makeText(CreateOfferActivity.this, "Uploaded offer sucessfully!", Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent(CreateOfferActivity.this, offerActivity.class);
                                intent.putExtra("offerID", offerID.toString());
                                startActivity(intent);
                            }else{
                                finish.setText(task.getException().toString());
                                Toast.makeText(CreateOfferActivity.this, "Could not upload the offer to the Database\nPlease restart the application", Toast.LENGTH_LONG).show();
                                Intent intent = new Intent(CreateOfferActivity.this, HomeActivity.class);
                                startActivity(intent);
                            }
                        }
                    });

        }
    }

//Here we check if the price is comprised of numbers only
    public boolean checkPrice(String price){
        for(int i = 0; i < price.length(); i++){
            if(price.charAt(i) > '9' || price.charAt(i) < '1'){
                return false;
            }
        }
        return true;
    }
    //Here we generate a uuid for the offers
    public String uuidGen() {
        UUID uuid = UUID.randomUUID();
        String uuidAsString = uuid.toString();
        return uuidAsString;
    }
}

编辑:忘记firestore结构:结构
我还没有真正尝试过任何东西,因为我不知道它为什么会这样做,这像firebase中的重复保护吗?
我正在使用firestore来存储报价,并使用auth来获取用户信息。

tez616oj

tez616oj1#

没关系,在这篇文章的帮助下我成功地修复了它。我将控制台中的规则更改为 allow read, write, create; 这一切都解决了。

相关问题