public uint CreateNewSubject(SubjectAlterations mods) { var withValues = mods.Where(x => HasValue(x)).ToList(); if (!withValues.Any()) { throw Fault.BadData(mods, "No non-NULL values in list."); } var newId = InsertSeedRow(withValues, 0); using (var db = ConnectToDB(out LiteCollection <SubjectValueMod> col)) { using (var trans = db.BeginTrans()) { for (int i = 1; i < withValues.Count; i++) { var row = withValues[i]; row.SubjectID = newId; col.Insert(row); } trans.Commit(); } } _subjectCreated.Raise(Tuple.Create(mods, newId)); return(newId); }
public static T GetOne <T>(this IEnumerable <T> collection, Func <T, bool> predicate, string predicateDescription) { if (collection == null) { throw Fault.BadData("collection == NULL"); } if (!collection.Any()) { throw Fault.BadData("Collection has no items"); } var matches = collection.Where(predicate); if (matches.Count() == 1) { return(matches.First()); } var typ = typeof(T).Name; if (matches.Count() == 0) { throw Fault.BadData($"No ‹{typ}› found where [{predicate}]."); } else { throw Fault.BadData($"Multiple ‹{typ}› found where [{predicate}]."); } }
public static void IfFound <TKey, TVal>( this Dictionary <TKey, TVal> dict, TKey key, Action <TVal> action, bool errorIfMissing = false) { if (dict.TryGetValue(key, out TVal value)) { action(value); } else if (errorIfMissing) { throw Fault.BadData($"Dictionary ‹{typeof(TVal).Name}› does not contain key [{key}]."); } }
//public async Task<NodeReply> Upload(R2Package localPkg) //{ // try // { // Alerter.Show(await ExecuteUpload(localPkg), "Upload"); // } // catch (Exception ex) // { // Alerter.ShowError("Upload Error", ex.Info(false, true)); // } //} public async Task <NodeReply> StartUpload(R2Package localPkg, string revisionLog) { if (localPkg.nid == 0) { throw Fault .BadData(localPkg, "nid should NOT be zero"); } _cancelr = new CancellationTokenSource(); try { return(await ExecuteUpload(localPkg, revisionLog, _cancelr.Token)); } catch (OperationCanceledException ex) { return(NodeReply.Fail(ex)); } }