Esempio n. 1
0
 // POST api/values
 public HttpResponseMessage Post(Topic topic)
 {
     using (var db = new TopicsDataContext())
     {
         db.Topics.Add(topic);
         db.SaveChanges();
         return(Request.CreateResponse(HttpStatusCode.OK, new
         {
             id = topic.Id,
             url = Request.RequestUri.AbsoluteUri + "/" + topic.Id
         }));
     }
 }
Esempio n. 2
0
 public HttpResponseMessage Put(Topic topic)
 {
     using (var db = new TopicsDataContext())
     {
         db.Topics.Attach(topic);
         db.Entry(topic).State = EntityState.Modified;
         db.SaveChanges();
         return(Request.CreateResponse(HttpStatusCode.OK, new
         {
             message = "topic is updated successfully"
         }));
     }
 }
Esempio n. 3
0
 public void Save()
 {
     try
     {
         _dataContext.SaveChanges();
     }
     catch (DbUpdateException)
     {
         throw;
     }
     catch (DbEntityValidationException)
     {
         throw;
     }
     catch (EntityException)
     {
         throw;
     }
 }
Esempio n. 4
0
        // DELETE api/topic/2
        public HttpResponseMessage Delete(int id)
        {
            using (var db = new TopicsDataContext())
            {
                var topic = db.Topics.SingleOrDefault(t => t.Id == id);
                if (topic == null)
                {
                    return(Request.CreateResponse(HttpStatusCode.NotFound, new
                    {
                        message = "The topic you want to delete was not found"
                    }));
                }

                db.Topics.Attach(topic);
                db.Entry(topic).State = EntityState.Deleted;
                db.SaveChanges();
                return(Request.CreateResponse(HttpStatusCode.OK, new
                {
                    message = "The topic was deleted successfully"
                }));
            }
        }