public JsonValue Delete(string id) { Contact deleted = GetType(id); using (var context = new ContactsDataContext(connectionString)) { context.Contacts.Attach(deleted); context.Contacts.DeleteOnSubmit(deleted); context.SubmitChanges(); } return(JsonValueExtensions.CreateFrom(deleted)); }
public JsonValue Post(JsonValue contact) { Contact added = contact.ReadAsType <Contact>(); using (var context = new ContactsDataContext(connectionString)) { context.Contacts.InsertOnSubmit(added); context.SubmitChanges(); context.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues, added); } return(this.Get(added.ContactID.ToString(CultureInfo.InvariantCulture))); }
public JsonValue Update(string id, JsonValue contact) { Contact original = GetType(id); Contact updated = contact.ReadAsType <Contact>(); using (var context = new ContactsDataContext(connectionString)) { context.Contacts.Attach(updated, original); context.SubmitChanges(); } return(this.Get(id)); }
private static Contact GetType(string id) { Contact result; using (var context = new ContactsDataContext(connectionString)) { result = context.Contacts.Where <Contact>(x => x.ContactID == int.Parse(id)).FirstOrDefault(); } if (result == null) { throw new WebFaultException <string>("Contact not found", HttpStatusCode.NotFound); } return(result); }
public JsonValue GetAll() { using (var context = new ContactsDataContext(connectionString)) { // For some reason a lambda doesn't work here but a delegate does var result = context.Contacts.OrderBy <Contact, string>(x => x.Name).Select <Contact, JsonObject>( delegate(Contact x) { return(new JsonObject { { "Name", x.Name }, { "Self", "contacts/" + x.ContactID } }); }); return(new JsonArray(result)); } }