コード例 #1
0
        public void CreatePost_WhenUserModelValid_ShouldSaveToDatabase()
        {
            var testUser = new UserModel()
            {
                UserName = "******",
                DisplayName = "ValidNick",
                AuthCode = new string('b', 40)
            };

            var httpServer = new InMemoryHttpServer("http://localhost/");
            var response = httpServer.CreatePostRequest("api/users/register", testUser);
            var contentString = response.Content.ReadAsStringAsync().Result;
            var userModel = JsonConvert.DeserializeObject<UserLoggedModel>(contentString);

            var testPost = new PostModel
            {
                Title = "NEW POST",
                Tags = new List<string>() { "post" },
                Text = "this is just a test post"
            };

            var headers = new Dictionary<string, string>();
            headers["X-sessionKey"] = userModel.SessionKey;

            //var postResponse = httpServer.CreatePostRequest("api/posts", testPost, headers);

            //Assert.AreEqual(HttpStatusCode.Created, postResponse.StatusCode);
            //Assert.IsNotNull(postResponse.Content);

            //var contentString = response.Content.ReadAsStringAsync().Result;
            //var model = JsonConvert.DeserializeObject<CreatePostResponseModel>(contentString);
            //Assert.AreEqual(testPost.Title, model.Title);
            //Assert.IsNotNull(model.Id > 0);
        }
コード例 #2
0
        public HttpResponseMessage PostCreatePost(PostModel model,
            [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(() =>
            {
                this.ValidateSessionKey(sessionKey);

                if (model == null)
                {
                    throw new InvalidOperationException("Model cannot be null");
                }

                var context = new BlogSystemContext();
                using (context)
                {
                    User user = context.Users.Where(usr => usr.SessionKey == sessionKey).FirstOrDefault();
                    if (user == null)
                    {
                        throw new InvalidOperationException("Invalid sessionkey");
                    }

                    var titleTags = model.Title.Split(
                        new char[] { '.', ',', ';', '!', '?', ' ' }, StringSplitOptions.RemoveEmptyEntries);

                    var newTags = new HashSet<Tag>();
                    foreach (var tagName in titleTags)
                    {
                        Tag newtag = new Tag()
                        {
                            Name = tagName.ToLower()
                        };

                        if (!newTags.Contains(newtag))
                        {
                            newTags.Add(newtag);
                        }
                    }

                    foreach (var tagName in model.Tags)
                    {
                        Tag newtag = new Tag()
                        {
                            Name = tagName.ToLower()
                        };

                        if (!newTags.Contains(newtag))
                        {
                            newTags.Add(newtag);
                        }
                    }

                    var newDbTags = new HashSet<Tag>();
                    foreach (var tag in newTags)
                    {
                        var tagDb = context.Tags.FirstOrDefault(tg => tg.Name == tag.Name);
                        if (tagDb == null)
                        {
                            newDbTags.Add(tag);
                        }
                        else
                        {
                            newDbTags.Add(tagDb);
                        }
                    }

                    var post = new Post()
                    {
                        Title = model.Title,
                        Text = model.Text,
                        DatePosted = DateTime.Now,
                        User = user,
                        Tags = newDbTags
                    };

                    user.Posts.Add(post);
                    context.SaveChanges();

                    var responseModel = new CreatePostResponseModel()
                    {
                        Title = post.Title,
                        Id = post.Id
                    };

                    var response = this.Request.CreateResponse(HttpStatusCode.Created, responseModel);

                    return response;
                }
            });

            return responseMsg;
        }