public void PostCreatePost_WhenSessionKeyIncorrect_Test() { string username = "******"; string displayName = "Test User"; string authCode = new String('*', 40); AddUser(username, displayName); LoginUser(username, authCode); string sessionKey = new string('*', 50); string title = "New post"; string text = "This is just a new post"; string[] tags = new string[] { "tag1", "tag2" }; PostWithTagsModel initialPost = new PostWithTagsModel() { Title = title, Text = text, Tags = tags }; var response = this.httpServer.CreatePostRequest("api/posts?sessionKey=" + sessionKey, initialPost); var contentString = response.Content.ReadAsStringAsync().Result; var postModel = JsonConvert.DeserializeObject <PostSimpleModel>(contentString); Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode); Post post = this.context.Set <Post>().FirstOrDefault(p => p.Title == title && p.User.SessionKey == sessionKey); Assert.IsNull(post); }
private PostSimpleModel CreatePost(string sessionKey, string title, string text, string[] tags) { PostWithTagsModel initialPost = new PostWithTagsModel() { Title = title, Text = text, Tags = tags }; var response = this.httpServer.CreatePostRequest("api/posts?sessionKey=" + sessionKey, initialPost); var contentString = response.Content.ReadAsStringAsync().Result; PostSimpleModel postModel = JsonConvert.DeserializeObject <PostSimpleModel>(contentString); return(postModel); }
public HttpResponseMessage CreatePost(string sessionKey, PostWithTagsModel postModel) { HttpResponseMessage responseMsg = this.PerformOperationAndHandleExceptions( () => { User user = VerifySessionKey(sessionKey); if (postModel.Title.Trim() == string.Empty) { throw new InvalidOperationException("The post title cannot be empty"); } if (postModel.Text.Trim() == string.Empty) { throw new InvalidOperationException("The post text cannot be empty"); } if (postModel.Tags == null) { throw new InvalidOperationException("Incorrect tags data"); } Post post = new Post() { Title = postModel.Title, Content = postModel.Text, User = user, PostDate = DateTime.Now }; IRepository <Post> postRepository = this.data.GetPostsRepository(); postRepository.Add(post); string[] tagsFromTitle = postModel.Title.ToLower().Split(new string[] { " ", ".", ",", "-", "!", "?", ";", ":", "'" }, StringSplitOptions.None); HashSet <string> tags = new HashSet <string>(); tags.UnionWith(tagsFromTitle); string[] postTagsToLower = postModel.Tags.Select(x => x.ToLower()).ToArray(); tags.UnionWith(postTagsToLower); IRepository <Tag> tagRepository = this.data.GetTagsRepository(); foreach (string tag in tags) { IQueryable <Tag> tagsQuery = tagRepository.GetConstraint(t => t.Name == tag); Tag postTag; if (tagsQuery.Count() == 0) { postTag = new Tag() { Name = tag }; postTag.Posts.Add(post); tagRepository.Add(postTag); } else { postTag = tagsQuery.First(); postTag.Posts.Add(post); tagRepository.Update(postTag.TagId, postTag); } } var postResultModel = new PostSimpleModel() { Id = post.PostId, Title = post.Title }; var response = this.Request.CreateResponse(HttpStatusCode.Created, postResultModel); return(response); }); return(responseMsg); }