Ejemplo n.º 1
0
        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));
        }
Ejemplo n.º 2
0
        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)));
        }
Ejemplo n.º 3
0
        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));
        }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
 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));
     }
 }