public Task <double> CalcRepairPrice(int id) { double total = 0; FirestoreDb db = connection.GetFirestoreDb(); QuerySnapshot snapshot = db .Collection("service-repairs") .WhereEqualTo("RepairId", id) .Limit(1) .GetSnapshotAsync() .Result; Repair repair = snapshot .FirstOrDefault() .ConvertTo <Repair>(); if (repair.RepairStatus.Contains("OOW") || repair.RepairStatus.Equals("Awaiting payment")) { total += repair.TechnicianLabor; QuerySnapshot qs = db .Collection("repair-parts") .WhereEqualTo("RepairId", id) .Limit(1) .GetSnapshotAsync() .Result; RequestPartBindingModel model = qs .FirstOrDefault() .ConvertTo <RequestPartBindingModel>(); foreach (KeyValuePair <string, int> x in model.PartsForRepair) { QuerySnapshot qst = db .Collection("warehouse-parts") .WhereEqualTo("PartNumber", x.Key) .Limit(1) .GetSnapshotAsync() .Result; WarehousePart part = qst .FirstOrDefault() .ConvertTo <WarehousePart>(); total += part.Price * x.Value; } qs .FirstOrDefault() .Reference .UpdateAsync("Total", total); } else { total = 0; } return(Task.FromResult(total)); }
ConvertPartToDictionary(WarehousePart part) { return(part.GetType() .GetProperties(BindingFlags.Instance | BindingFlags.Public) .ToDictionary(prop => prop.Name, prop => prop.GetValue(part, null))); }
RunTransaction( WarehousePart newPart, CollectionReference colRef) { await colRef.Database .RunTransactionAsync(async transaction => { await colRef.AddAsync(newPart); }); }
public async void AddWarehousePart( AddWarehousePartBindingModel model, string serviceName) { FirestoreDb db = connection.GetFirestoreDb(); CollectionReference colRef = db .Collection("warehouse-parts") .Document("bcyvKBFBWE6DxnvIQ1Kn") .Collection("parts"); WarehousePart newPart = new WarehousePart(model); QuerySnapshot partsWithSamePN = await colRef .WhereEqualTo("PartNumber", newPart.PartNumber) .GetSnapshotAsync(); if (partsWithSamePN.Count == 0) { QuerySnapshot partsWithSameSPN = await colRef .WhereArrayContains( "SubstituteParts", newPart.PartNumber) .GetSnapshotAsync(); if (partsWithSameSPN.Count == 0) { await RunTransaction(newPart, colRef); } else { WarehousePart lastPartWithSameSPN = GetLastPartWithSamePnOrSpn(partsWithSameSPN); newPart.Model = GetUniqueElements( newPart.Model, lastPartWithSameSPN.Model); newPart.SubstituteParts = GetUniqueElements( newPart.SubstituteParts, lastPartWithSameSPN.SubstituteParts); partsWithSameSPN.Documents .ToList() .ForEach(async x => { WarehousePart p = x.ConvertTo <WarehousePart>(); p.Model = newPart.Model; p.SubstituteParts = newPart.SubstituteParts; Dictionary <string, object> partAsDictionary = ConvertPartToDictionary(p); await db.RunTransactionAsync(async t => { await x.Reference .UpdateAsync(partAsDictionary); }); }); } } else { WarehousePart lastPartWithSamePN = GetLastPartWithSamePnOrSpn(partsWithSamePN); newPart.Model = GetUniqueElements( newPart.Model, lastPartWithSamePN.Model); newPart.SubstituteParts = GetUniqueElements( newPart.SubstituteParts, lastPartWithSamePN.SubstituteParts); partsWithSamePN.Documents .ToList() .ForEach(async x => { WarehousePart p = x.ConvertTo <WarehousePart>(); p.Model = newPart.Model; p.SubstituteParts = newPart.SubstituteParts; p.Availability = partsWithSamePN.Count + 1; Dictionary <string, object> partAsDictionary = ConvertPartToDictionary(p); await db.RunTransactionAsync(async t => { await x.Reference .UpdateAsync(partAsDictionary); }); }); } }
public async void SendPartToRepair(int id, string partNumber, int qnt) { FirestoreDb db = connection.GetFirestoreDb(); CollectionReference colRef = db .Collection("warehouse-parts"); QuerySnapshot snapshot = colRef .WhereEqualTo("PartNumber", partNumber) .GetSnapshotAsync() .Result; if (snapshot.Count == 0) { return; } else { WarehousePart part = new List <DocumentSnapshot> (snapshot.Documents) .OrderByDescending(x => x.CreateTime) .FirstOrDefault() .ConvertTo <WarehousePart>(); if (part.Availability < qnt) { return; } else { snapshot.Documents .ToList() .ForEach(async x => { WarehousePart p = x.ConvertTo <WarehousePart>(); p.Availability -= qnt; await x .Reference .UpdateAsync("Availability", p.Availability); }); QuerySnapshot qs = db .Collection("repair-parts") .WhereEqualTo("RepairId", id) .Limit(1) .GetSnapshotAsync() .Result; RequestPartBindingModel parts = qs .Documents .FirstOrDefault() .ConvertTo <RequestPartBindingModel>(); parts.PartsForRepair[partNumber] = qnt; await qs.Documents .FirstOrDefault() .Reference .UpdateAsync("PartsForRepair", parts.PartsForRepair); RepairLog log = new RepairLog() { TimeOfEvent = DateTime.UtcNow, TypeOfEvent = "update part qnt", Description = "Update part qnt" }; await new LogService() .UploadLogToExistingRepair(id, log); } } }