public static ThingModel FromNameValueCollection(string partitionKey, NameValueCollection pairs, bool autoSave) { ThingModel thing; if (string.IsNullOrWhiteSpace(pairs["id"])) { // We did not receive an id in the collection, create a new ThingModel thing = new ThingModel(partitionKey); } else { // We got an id but we need to make sure it exists. We will try // and retrieve it and if it fails we'll create a new thing with the passed id thing = FromRowKey(partitionKey, pairs["id"]); if (thing == null) thing = new ThingModel(partitionKey, pairs["id"]); } // Cycle through each of the value pairs we were passed and // pass them into SetProperProperty to update the model as // needed. foreach (var item in pairs.AllKeys) { thing.SetProperProperty(item, pairs[item]); } if (autoSave) thing.Save(); // Return the completed thing (note that we have not saved this thing) return thing; }
public ActionResult UpdateItem(ThingModel model) { var user = Lib.UserUtils.GetUser(this); model.PartitionKey = user.RepoId; model.RowKey = model.Id; model.Save(); return RedirectToAction("RepoItems", "Home"); }
public EventModel(ThingModel thing) :this() { _thing = thing; this.PartitionKey = thing.PartitionKey; this.RowKey = Guid.NewGuid().ToString("N"); EventProperties.Add("Id", thing.Id); EventProperties.Add("EventType", EventType.Change.ToString()); // Set the default type if (!string.IsNullOrWhiteSpace(thing.Agent)) EventProperties.Add("Agent", thing.Agent); }
private Dictionary <string, Tuple <string, string> > ThingDiff(ThingModel newThing, ThingModel oldThing) { var results = new Dictionary <string, Tuple <string, string> >(); if (newThing == null || oldThing == null) { return(results); } // Name Change if (newThing.Name != oldThing.Name) { results.Add("Name", new Tuple <string, string>(oldThing.Name, newThing.Name)); } // ID Change if (newThing.Id != oldThing.Id) { results.Add("Id", new Tuple <string, string>(oldThing.Id, newThing.Id)); } // Agent Change if (newThing.Agent != oldThing.Agent) { results.Add("Agent", new Tuple <string, string>(oldThing.Agent, newThing.Agent)); } // Type if (newThing.Type != oldThing.Type) { results.Add("Type", new Tuple <string, string>(oldThing.Type, newThing.Type)); } var comparer = EqualityComparer <string> .Default; foreach (KeyValuePair <string, string> kvp in newThing.Properties) { string secondValue; if (!oldThing.Properties.TryGetValue(kvp.Key, out secondValue)) { results.Add(kvp.Key, new Tuple <string, string>(string.Empty, kvp.Value)); } if (!comparer.Equals(kvp.Value, secondValue)) { results.Add(kvp.Key, new Tuple <string, string>(secondValue, kvp.Value)); } } return(results); }
private Dictionary<string, Tuple<string, string>> ThingDiff(ThingModel newThing, ThingModel oldThing) { var results = new Dictionary<string, Tuple<string, string>>(); if (newThing == null || oldThing == null) return results; // Name Change if (newThing.Name != oldThing.Name) { results.Add("Name", new Tuple<string, string>(oldThing.Name, newThing.Name)); } // ID Change if (newThing.Id != oldThing.Id) { results.Add("Id", new Tuple<string, string>(oldThing.Id, newThing.Id)); } // Agent Change if (newThing.Agent != oldThing.Agent) { results.Add("Agent", new Tuple<string, string>(oldThing.Agent, newThing.Agent)); } // Type if (newThing.Type != oldThing.Type) { results.Add("Type", new Tuple<string, string>(oldThing.Type, newThing.Type)); } var comparer = EqualityComparer<string>.Default; foreach (KeyValuePair<string, string> kvp in newThing.Properties) { string secondValue; if (!oldThing.Properties.TryGetValue(kvp.Key, out secondValue)) results.Add(kvp.Key, new Tuple<string, string>(string.Empty, kvp.Value)); if (!comparer.Equals(kvp.Value, secondValue)) results.Add(kvp.Key, new Tuple<string, string>(secondValue, kvp.Value)); } return results; }
private void StateChangedEvent(ThingModel current, ThingModel previous) { var thingDiff = this.ThingDiff(current, previous); if (thingDiff.Count == 0) { // No actual changes System.Diagnostics.Debug.WriteLine("No Changes Found: " + current.Id); return; } EventModel eventModel = new EventModel(current); foreach (var item in thingDiff) { eventModel.EventProperties.Add(item.Key, item.Value.Item1); } eventModel.Send(); }
public EventModel(ThingModel thing, EventType type) : this(thing) { EventProperties["EventType"] = type.ToString(); switch (type) { case EventType.Change: break; case EventType.Add: break; case EventType.Delete: break; case EventType.Message: EventProperties.Add("EventMessage", string.Empty); // Add if this is a message break; default: break; } }
public static ThingModel FromNameValueCollection(string partitionKey, NameValueCollection pairs, bool autoSave) { ThingModel thing; if (string.IsNullOrWhiteSpace(pairs["id"])) { // We did not receive an id in the collection, create a new ThingModel thing = new ThingModel(partitionKey); } else { // We got an id but we need to make sure it exists. We will try // and retrieve it and if it fails we'll create a new thing with the passed id thing = FromRowKey(partitionKey, pairs["id"]); if (thing == null) { thing = new ThingModel(partitionKey, pairs["id"]); } } // Cycle through each of the value pairs we were passed and // pass them into SetProperProperty to update the model as // needed. foreach (var item in pairs.AllKeys) { thing.SetProperProperty(item, pairs[item]); } if (autoSave) { thing.Save(); } // Return the completed thing (note that we have not saved this thing) return(thing); }