static void TestPersons(UserSecurityContext context) { PersonProvider prov = PersonManager.Instance.GetProvider(context); Console.WriteLine("Testing Persons"); Person dev = prov.Create("Grant", "Fuji"); if (dev != null) { Console.WriteLine("Create Person: " + dev.FirstName + " " + dev.LastName); } else { Console.WriteLine("Failed to create Person"); } dev.LastName = "Fujimoto"; bool updated = prov.Update(dev); if (updated) { Console.WriteLine("Updated Person"); } else { Console.WriteLine("Failed to update Person"); } var retrievedPerson = prov.Get(dev.Identity); if (retrievedPerson != null) { Console.WriteLine("Get Person: " + retrievedPerson.FirstName + " " + retrievedPerson.LastName); } else { Console.WriteLine("Failed to retrieve Person"); } bool deleted = prov.Delete(dev); if (deleted) { Console.WriteLine("Deleted Person"); } else { Console.WriteLine("Failed to Delete Person"); } Console.WriteLine("Person Test Complete!"); return; }
protected void lnbDelete_Click(object sender, EventArgs e) { try { int[] selectedID = WebFormHelper.GetRowIdForDeletion(gvwParent); personProvider.Delete(selectedID); gvwParent.DataBind(); btnCancel_Click(sender, e); } catch (Exception ex) { WebFormHelper.SetLabelTextWithCssClass(lblStatus, ex.Message, LabelStyleNames.ErrorMessage); } }
public override void Handle(HttpContext context, CancellationToken cancel) { if (context != null) { UserIdentityBase user = Security.Session.GetUser(context); if (user != null) { UserSecurityContext ctx = new UserSecurityContext(user); string localUrl = RestUtils.LocalUrl(this, context.Request); string meth = RestUtils.StripLocal(this.BaseUrl, localUrl); meth = meth.Substring(1); if (!string.IsNullOrEmpty(meth)) { if (context.Request.Method == "POST") { if (meth.Equals("all", StringComparison.OrdinalIgnoreCase)) { Get(ctx, context, cancel); return; } else if (meth.Equals("create", StringComparison.OrdinalIgnoreCase)) { try { JToken token = JsonUtils.GetDataPayload(context.Request); PersonProvider provider = PersonManager.Instance.GetProvider(ctx); Person person = null; JObject jperson = null; string firstName = token["firstname"].ToString(); string lastName = token["lastname"].ToString(); JArray contacts = token["contacts"] != null ? token["contacts"] as JArray : null; if (!string.IsNullOrEmpty(firstName) && !string.IsNullOrEmpty(lastName)) { person = provider.Create(firstName, lastName); if (person != null) { var result = true; if (contacts != null) { //add all contacts to person object foreach (JToken contact in contacts) { string name = contact["name"].ToString(); string email = contact["email"].ToString(); person.Contacts.Add(name, new EmailAddress(email)); } //persist contact info result &= provider.Update(person); } if (result) { jperson = Jsonifier.ToJson(person); if (person != null) { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Ok, jperson.ToString())); } else { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); } return; } } } RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); } catch { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); return; } } else if (meth.Equals("update", StringComparison.OrdinalIgnoreCase)) { try { JToken token = JsonUtils.GetDataPayload(context.Request); PersonProvider provider = PersonManager.Instance.GetProvider(ctx); Person person = null; bool result = true; CompoundIdentity id = JsonUtils.ToId(token["id"]); string firstName = token["firstname"].ToString(); string lastName = token["lastname"].ToString(); if (!string.IsNullOrEmpty(firstName) && !string.IsNullOrEmpty(lastName) && id != null) { person = new Person(id, firstName, lastName); if (token.SelectToken("contacts") != null && person != null) { JToken contact = token["contacts"]; string name = contact["name"].ToString(); string email = contact["schemeid"].ToString(); if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(email)) { person.Contacts.Add(name, new EmailAddress(email)); } } result &= provider.Update(person); } if (person != null && result) { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Ok)); } else { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); } return; } catch { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); return; } } else if (meth.Equals("delete", StringComparison.OrdinalIgnoreCase)) { try { bool result = true; JToken token = JsonUtils.GetDataPayload(context.Request); PersonProvider provider = PersonManager.Instance.GetProvider(ctx); if (provider != null && token != null) { string first = token["firstname"].ToString(); string last = token["lastname"].ToString(); CompoundIdentity id = JsonUtils.ToId(token["id"]); if (first != null && last != null && id != null) { Person p = new Person(id, first, last); result &= provider.Delete(p); } if (result == true) { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Ok)); } else { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); } } RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); } catch { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); return; } } } } } else { context.Response.StatusCode = HttpStatusCodes.Status401Unauthorized; return; } } context.Response.StatusCode = HttpStatusCodes.Status400BadRequest; }