public async Task <Cart> GetCartByUser(string userId) { FirestoreDb db = CreateInstanceDB(); var appSettings = ConfigurationManager.AppSettings; string wApiKey = appSettings["FirebaseApiKey"] ?? "Not Found"; var authProvider = new FirebaseAuthProvider(new FirebaseConfig(wApiKey)); AbbigliamentoECommerceEntity.Cart wCart = new AbbigliamentoECommerceEntity.Cart(); wCart.listProduct = new List <CartDetail>(); try { //uso il uid utente per recuperare tutte le info dell'utente loggato DocumentReference wDocRef = db.Collection("user").Document(userId); DocumentSnapshot snapshot = await wDocRef.GetSnapshotAsync(); if (snapshot.Exists) { Dictionary <string, object>[] wCartArray = snapshot.ContainsField("carrello") ? snapshot.GetValue <Dictionary <string, object>[]>("carrello") : null; if (wCartArray != null) { foreach (Dictionary <string, object> wCartDBColl in wCartArray) { DocumentReference wDocRefProd = db.Collection("prodotto").Document(wCartDBColl["uidProdotto"].ToString()); DocumentSnapshot snapshotProd = await wDocRefProd.GetSnapshotAsync(); if (snapshotProd.Exists) { AbbigliamentoECommerceEntity.CartDetail wCartDetail = new AbbigliamentoECommerceEntity.CartDetail(); Product wProd = new Product(); wProd.UId = snapshotProd.Id; wProd.categoria = snapshotProd.ContainsField("categoria") ? snapshotProd.GetValue <string>("categoria") : ""; wProd.colore = snapshotProd.ContainsField("colore") ? snapshotProd.GetValue <string>("colore") : ""; wProd.marca = snapshotProd.ContainsField("marca") ? snapshotProd.GetValue <string>("marca") : ""; wProd.nome = snapshotProd.ContainsField("nome") ? snapshotProd.GetValue <string>("nome") : ""; wProd.prezzo = snapshotProd.ContainsField("prezzo") ? snapshotProd.GetValue <double>("prezzo") : 0; wProd.taglia = snapshotProd.ContainsField("taglia") ? snapshotProd.GetValue <string>("taglia") : ""; wProd.UrlDownloadWeb = snapshotProd.ContainsField("urlDownloadWeb") ? snapshotProd.GetValue <string>("urlDownloadWeb") : ""; wProd.descrizione = snapshotProd.ContainsField("descrizione") ? snapshotProd.GetValue <string>("descrizione") : ""; wProd.Quantity = snapshotProd.ContainsField("Quantity") ? snapshotProd.GetValue <int>("Quantity") : 0; wCartDetail.quantita = Convert.ToInt32(wCartDBColl["quantita"]); wCartDetail.singleProduct = wProd; wCart.listProduct.Add(wCartDetail); } } } } } catch (Exception) { throw; } return(wCart); }
public async Task <WriteResult> RemoveProductToCart(string pProductId, string pUserUId) { FirestoreDb db = CreateInstanceDB(); FirebaseApp wApp = CreateFirebaseApp(); //GetData DocumentReference wDocRef = db.Collection("user").Document(pUserUId); DocumentSnapshot snapshot = await wDocRef.GetSnapshotAsync(); WriteResult wWResult = null; Dictionary <string, object>[] wCartArray = snapshot.ContainsField("carrello") ? snapshot.GetValue <Dictionary <string, object>[]>("carrello") : null; if (wCartArray != null) { foreach (Dictionary <string, object> wCartDBColl in wCartArray) { if (wCartDBColl["uidProdotto"].ToString() == pProductId) { wWResult = await wDocRef.UpdateAsync("carrello", FieldValue.ArrayRemove(wCartDBColl)); } } } //End get Data return(wWResult); }
//Metodo che effettua la login e verifica utenza di Firebase public async Task <AbbigliamentoECommerceEntity.User> SignIn(string pEmail, string pPassword) { FirestoreDb db = CreateInstanceDB(); AbbigliamentoECommerceEntity.User wUser = new AbbigliamentoECommerceEntity.User(); var appSettings = ConfigurationManager.AppSettings; string wApiKey = appSettings["FirebaseApiKey"] ?? "Not Found"; var authProvider = new FirebaseAuthProvider(new FirebaseConfig(wApiKey)); try { //Effettuo la login tramite email e password FirebaseAuthLink auth = await authProvider.SignInWithEmailAndPasswordAsync(pEmail, pPassword); //recupero del uid utente per recuperare tutte le info del'utente loggato var decoded = await FirebaseAdmin.Auth.FirebaseAuth.DefaultInstance.VerifyIdTokenAsync(auth.FirebaseToken); var uid = decoded.Uid; DocumentReference wDocRef = db.Collection("user").Document(uid); DocumentSnapshot snapshot = await wDocRef.GetSnapshotAsync(); if (snapshot.Exists) { wUser.Address = snapshot.ContainsField("Address") ? snapshot.GetValue <string>("Address") : ""; wUser.City = snapshot.ContainsField("City") ? snapshot.GetValue <string>("City") : ""; wUser.cognome = snapshot.ContainsField("cognome") ? snapshot.GetValue <string>("cognome") : ""; wUser.DateOfBirth = snapshot.ContainsField("DateOfBirth") ? snapshot.GetValue <DateTime>("DateOfBirth") : DateTime.MinValue; wUser.District = snapshot.ContainsField("District") ? snapshot.GetValue <string>("District") : ""; wUser.email = snapshot.ContainsField("email") ? snapshot.GetValue <string>("email") : ""; wUser.Id = uid; wUser.nome = snapshot.ContainsField("nome") ? snapshot.GetValue <string>("nome") : ""; wUser.Ruolo = snapshot.ContainsField("Ruolo") ? snapshot.GetValue <string>("Ruolo") : "cliente"; wUser.TelefoneNumber = snapshot.ContainsField("TelefoneNumber") ? snapshot.GetValue <string>("TelefoneNumber") : ""; } } catch (Exception) { throw; } return(wUser); }
public async Task <AbbigliamentoECommerceEntity.User> GetUser(string pId) { FirestoreDb db = CreateInstanceDB(); AbbigliamentoECommerceEntity.User wUser = new AbbigliamentoECommerceEntity.User(); var appSettings = ConfigurationManager.AppSettings; string wApiKey = appSettings["FirebaseApiKey"] ?? "Not Found"; // var authProvider = new FirebaseAuthProvider(new FirebaseConfig(wApiKey)); try { //recupero del uid utente per recuperare tutte le info del'utente loggato DocumentReference wDocRef = db.Collection("user").Document(pId); DocumentSnapshot snapshot = await wDocRef.GetSnapshotAsync(); if (snapshot.Exists) { wUser.Address = snapshot.ContainsField("Address") ? snapshot.GetValue <string>("Address") : ""; wUser.City = snapshot.ContainsField("City") ? snapshot.GetValue <string>("City") : ""; wUser.cognome = snapshot.ContainsField("cognome") ? snapshot.GetValue <string>("cognome") : ""; wUser.DateOfBirth = snapshot.ContainsField("DateOfBirth") ? snapshot.GetValue <DateTime>("DateOfBirth") : DateTime.MinValue; wUser.District = snapshot.ContainsField("District") ? snapshot.GetValue <string>("District") : ""; wUser.email = snapshot.ContainsField("email") ? snapshot.GetValue <string>("email") : ""; wUser.Id = pId; wUser.nome = snapshot.ContainsField("nome") ? snapshot.GetValue <string>("nome") : ""; wUser.Ruolo = snapshot.ContainsField("Ruolo") ? snapshot.GetValue <string>("Ruolo") : "cliente"; wUser.TelefoneNumber = snapshot.ContainsField("TelefoneNumber") ? snapshot.GetValue <string>("TelefoneNumber") : ""; } } catch (Exception ex) { throw; } return(wUser); }
//recupera il prodotto tramite uid per la gestione del dettaglio prodotto public async Task <Product> GetProductById(string pId) { Product wProd = new Product(); FirestoreDb db = CreateInstanceDB(); DocumentReference wDocRef = db.Collection("prodotto").Document(pId); DocumentSnapshot documentSnapshot = await wDocRef.GetSnapshotAsync(); if (documentSnapshot.Exists) { wProd.UId = documentSnapshot.Id; wProd.categoria = documentSnapshot.ContainsField("categoria") ? documentSnapshot.GetValue <string>("categoria") : ""; wProd.colore = documentSnapshot.ContainsField("colore") ? documentSnapshot.GetValue <string>("colore") : ""; wProd.marca = documentSnapshot.ContainsField("marca") ? documentSnapshot.GetValue <string>("marca") : ""; wProd.nome = documentSnapshot.ContainsField("nome") ? documentSnapshot.GetValue <string>("nome") : ""; wProd.prezzo = documentSnapshot.ContainsField("prezzo") ? documentSnapshot.GetValue <double>("prezzo") : 0; wProd.taglia = documentSnapshot.ContainsField("taglia") ? documentSnapshot.GetValue <string>("taglia") : ""; wProd.UrlDownloadWeb = documentSnapshot.ContainsField("urlDownloadWeb") ? documentSnapshot.GetValue <string>("urlDownloadWeb") : ""; wProd.descrizione = documentSnapshot.ContainsField("descrizione") ? documentSnapshot.GetValue <string>("descrizione") : ""; wProd.Quantity = documentSnapshot.ContainsField("Quantity") ? documentSnapshot.GetValue <int>("Quantity") : 0; } return(wProd); }
public async Task Listening2() { CollectionReference collection = db.Collection("testecsharp"); Query query = collection.WhereGreaterThan("age", 5).OrderByDescending("age"); FirestoreChangeListener listener = collection.Listen(snapshot => { Console.WriteLine($"Callback received query snapshot"); Console.WriteLine($"Count: {snapshot.Count}"); Console.WriteLine("Changes:"); string name = ""; int age = 0; foreach (DocumentChange change in snapshot.Changes) { DocumentSnapshot document = change.Document; Console.WriteLine($"{document.Reference.Id}: ChangeType={change.ChangeType}; OldIndex={change.OldIndex}; NewIndex={change.NewIndex})"); if (document.Exists) { if (document.ContainsField("name")) { name = document.GetValue <string>("name"); } if (document.ContainsField("age")) { age = document.GetValue <int>("age"); } Console.WriteLine($" Document data: Name={name}; age={age}"); } } Console.WriteLine(); }); Console.WriteLine("Creating document for Sophie (age = 7)"); DocumentReference doc1Ref = await collection.AddAsync(new { name = "Sophie", age = 7 }); Console.WriteLine($"Sophie document ID: {doc1Ref.Id}"); await Task.Delay(1000); Console.WriteLine("Creating document for James (age = 10)"); DocumentReference doc2Ref = await collection.AddAsync(new { name = "James", age = 10 }); Console.WriteLine($"James document ID: {doc2Ref.Id}"); await Task.Delay(1000); Console.WriteLine("Modifying document for Sophie (set age = 11, higher than age for James)"); await doc1Ref.UpdateAsync("age", 11); await Task.Delay(1000); Console.WriteLine("Modifying document for Sophie (set age = 12, no change in position)"); await doc1Ref.UpdateAsync("age", 12); await Task.Delay(1000); Console.WriteLine("Modifying document for James (set age = 4, below threshold for query)"); await doc2Ref.UpdateAsync("age", 4); await Task.Delay(1000); Console.WriteLine("Deleting document for Sophie"); await doc1Ref.DeleteAsync(); await Task.Delay(1000); Console.WriteLine("Stopping listener"); await listener.StopAsync(); }