public HttpResponseMessage Comment(int postID, [FromBody]CommentModel comment,
[ValueProvider(typeof(HeaderValueProviderFactory<string>))]string sessionKey)
{
HttpResponseMessage httpResponse = this.PerformOperation(() =>
{
UserValidator.ValidateSessionKey(sessionKey);
var context = new BloggingSystemContext();
using (context)
{
var loggedUserEntity = context.Users.FirstOrDefault<User>(u => u.SessionKey == sessionKey);
if (loggedUserEntity == null)
{
throw new InvalidOperationException("Invalid user or pasword!");
}
var existingPostEntity = context.Posts.Find(postID);
if (existingPostEntity == null)
{
return this.Request.CreateErrorResponse(
HttpStatusCode.NotFound, "Post with provided ID not found!");
}
existingPostEntity.Comments.Add(CommentsMapper.ToEntity(comment, loggedUserEntity));
context.SaveChanges();
var response = this.Request.CreateResponse(HttpStatusCode.OK);
return response;
}
});
return httpResponse;
}