Esempio n. 1
0
        public HttpResponseMessage PutLogoutUser(string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
              () =>
              {
                  var context = new BlogContext();

                  using (context)
                  {
                      ValidateSessionKey(sessionKey);
                      var user = context.Users.FirstOrDefault(usr => usr.SessionKey == sessionKey);

                      if (user == null)
                      {
                          throw new InvalidOperationException("Invalid SessionKey!");
                      }

                      user.SessionKey = null;
                      context.Users.Attach(user);
                      var entity = context.Entry(user);
                      entity.Property(e => e.SessionKey).IsModified = true;
                      context.SaveChanges();
                  }

                  return this.Request.CreateResponse(HttpStatusCode.OK);
              });

            return responseMsg;
        }
Esempio n. 2
0
        public HttpResponseMessage PutComment(int postId, [FromBody]Comment comment, string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
             () =>
             {
                 var context = new BlogContext();
                 ValidateSessionKey(sessionKey, context);
                 using (context)
                 {
                     var currPost = context.Posts.FirstOrDefault(p => p.Id == postId);

                     if (currPost == null)
                     {
                         throw new InvalidOperationException("Post does not exist!");
                     }

                     Comment newComment = new Comment()
                     {
                         Id = comment.Id,
                         Date = comment.Date,
                         Post = comment.Post,
                         Text = comment.Text,
                         User = comment.User
                     };

                     currPost.Comments.Add(newComment);
                     context.Posts.Attach(currPost);
                     context.Entry(currPost).CurrentValues.SetValues(currPost);
                     context.SaveChanges();
                 }

                 return this.Request.CreateResponse(HttpStatusCode.Created);
             });

            return responseMsg;
        }