public HttpResponseMessage CreateComment(int postId, [FromBody] CommentDto value) { try { var sessionKey = ApiControllerHelper.GetHeaderValue(Request.Headers, "X-SessionKey"); if (sessionKey == null) { throw new ArgumentNullException("No session key provided in the request header!"); } Validate(value.Text, "text"); var context = new BloggingSystemContext(); using (context) { var user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey); if (user == null) { throw new ArgumentException("Users must be logged in to leave comments!"); } var post = context.Posts.FirstOrDefault(p => p.Id == postId); if (post == null) { throw new ArgumentException("Invalid post id: " + postId); } var newComment = new Comment() { Text = value.Text, PostDate = DateTime.Now, Author = user, Post = post }; context.Comments.Add(newComment); context.SaveChanges(); var response = Request.CreateResponse(HttpStatusCode.OK); return(response); } } catch (Exception ex) { var errorResponse = Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message); throw new HttpResponseException(errorResponse); } }
public HttpResponseMessage LogoutUser() { BloggingSystemContext context = null; try { string sessionKey = ApiControllerHelper.GetHeaderValue(Request.Headers, "X-SessionKey"); if (sessionKey == null) { throw new ArgumentNullException("No session key provided in the request header!"); } context = new BloggingSystemContext(); var user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey); if (user.SessionKey == null) { throw new ArgumentNullException("User is already logged out!"); } user.SessionKey = null; context.SaveChanges(); var response = Request.CreateResponse(HttpStatusCode.OK); return(response); } catch (Exception ex) { var errorResponse = Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message); throw new HttpResponseException(errorResponse); } finally { if (context != null) { context.Dispose(); } } }
public HttpResponseMessage CreatePost(CreatePostDto value) { try { var sessionKey = ApiControllerHelper.GetHeaderValue(Request.Headers, "X-SessionKey"); if (sessionKey == null) { throw new ArgumentNullException("No session key provided in the request header!"); } Validate(value.Title, "title"); Validate(value.Text, "text"); var context = new BloggingSystemContext(); using (context) { var user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey); if (user == null) { throw new ArgumentException("Users must be logged in to create posts!"); } var newPost = new Post() { Title = value.Title, Text = value.Text, PostDate = DateTime.Now, Author = user }; string[] tagsFromTitle = value.Title.Split( new char[] { ' ', ',', '.', ';', '!', '?', ':' }, StringSplitOptions.RemoveEmptyEntries); List <string> tagsToCheck = new List <string>(); foreach (var tagFromTitle in tagsFromTitle) { tagsToCheck.Add(tagFromTitle); } if (value.Tags != null) { foreach (string tagName in value.Tags) { tagsToCheck.Add(tagName); } } foreach (string tagName in tagsToCheck) { var matchingTag = context.Tags.FirstOrDefault(t => string.Compare(t.Name, tagName, true) == 0); if (matchingTag == null) { // tag not found, insert it in the database matchingTag = new Tag { Name = tagName.ToLower() }; context.Tags.Add(matchingTag); context.SaveChanges(); } newPost.Tags.Add(matchingTag); } context.Posts.Add(newPost); context.SaveChanges(); var createdPostDto = new CreatePostDto() { Id = newPost.Id, Title = newPost.Title, Tags = newPost.Tags.Select(t => t.Name), Text = newPost.Text }; var response = Request.CreateResponse(HttpStatusCode.Created, createdPostDto); return(response); } } catch (Exception ex) { var errorResponse = Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message); throw new HttpResponseException(errorResponse); } }