public HttpResponseMessage Create([FromBody]PostModel post,
[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 newPostEntity = PostsMapper.CreateNewEntity(post, loggedUserEntity, context);
context.Posts.Add(newPostEntity);
context.SaveChanges();
var postedPost = PostsMapper.ToPostedModel(newPostEntity);
var response = this.Request.CreateResponse(HttpStatusCode.Created, postedPost);
return response;
}
});
return httpResponse;
}