Пример #1
0
        // PUT api/Comments/5
        public HttpResponseMessage PutComment(int id, Comment comment)
        {
            //Check isLoggedIn
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != comment.CommentId)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            db.Entry(comment).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }
Пример #2
0
        // POST api/Comments
        public HttpResponseMessage PostComment(Comment comment)
        {
            //Check isLoggedIn
            if (ModelState.IsValid)
            {
                db.Comments.Add(comment);
                db.SaveChanges();

               
               // response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = comment.CommentId }));

                // Notification receiver channel
                string channel = "-channel";//comment.Image.Album.User.Username + "-channel";

                // Notification message
                string user = db.Users.FirstOrDefault(x => x.UserId == comment.UserId).Username;
                string image = db.Images.FirstOrDefault(x => x.ImageId == comment.ImageId).Title;
                string notificationMessage = user +
                                            " has just commented your image with title: '" +
                 image + "'";

                // Send notification message to the user whose image was commented
                PubnubPublisher.Publish(channel, notificationMessage);

                return Request.CreateResponse(HttpStatusCode.Created);
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }
        // POST api/Comments
        public HttpResponseMessage PostComment(Comment comment)
        {
            //Check isLoggedIn
            if (ModelState.IsValid)
            {
                db.Comments.Add(comment);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, comment);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = comment.CommentId }));
                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }