public async Task <Cart> GetCartByUser(string pUserId) { FirebaseManegment wDB = new FirebaseManegment(); AbbigliamentoECommerceEntity.Cart wCart = await wDB.GetCartByUser(pUserId); return(wCart); }
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); }