Esempio n. 1
0
 // POST api/<controller>
 public HttpResponseMessage Post([FromBody]Contact value)
 {
     using (var db = new AddressBookEntities())
     {
         var contact = db.Contacts.Add(value);
         db.SaveChanges();
         var response = Request.CreateResponse(HttpStatusCode.Created, contact);
         response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = contact.ContactId }));
         return response;
     }
 }
Esempio n. 2
0
        // DELETE api/<controller>/5
        public void Delete(int id)
        {
            using (var db = new AddressBookEntities())
            {
                var contact = db.Contacts.Find(id);

                // Remove existing contact
                if (contact != null)
                {
                    db.Contacts.Remove(contact);
                    db.SaveChanges();
                }
            }
        }
Esempio n. 3
0
        // PUT api/<controller>/5
        public void Put(int id, [FromBody]Contact value)
        {
            using (var db = new AddressBookEntities())
            {
                var contact = db.Contacts.Find(id);

                // Update existing contact
                if (contact != null)
                {
                    contact.FirstName = value.FirstName;
                    contact.MiddleName = value.MiddleName;
                    contact.LastName = value.LastName;
                    contact.Suffix = value.Suffix;
                    contact.DOB = value.DOB;
                    db.SaveChanges();
                }
            }
        }