public HttpResponseMessage CreatePost([FromBody] PostRequestModel postModel
, [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey)
        {
            var messageResponse = this.TryExecuteOperation<HttpResponseMessage>(() =>
            {
                var user = unitOfWork.userRepository.All().Single(x => x.SessionKey == sessionKey);
                if (user == null)
                {
                    throw new InvalidOperationException("User has not logged in!");
                }

                var postToAdd = new Post()
                {
                    Title = postModel.Title,
                    PostDate = DateTime.Now,
                    Text = postModel.Text,
                    PostedBy = unitOfWork.userRepository.All().Single(x => x.SessionKey == sessionKey),
                };

                foreach (var tagName in postModel.Tags)
                {
                    var tag = unitOfWork.tagRepository.All().FirstOrDefault(x => x.Name == tagName);
                    if (tag == null)
                    {
                        Tag toAdd = new Tag()
                        {
                            Name = tagName
                        };
                        postToAdd.Tags.Add(toAdd);
                    }

                    postToAdd.Tags.Add(tag);
                }
                //postToAdd.PostedBy = unitOfWork.userRepository.All().Single(x => x.SessionKey == sessionKey);
                unitOfWork.postRepository.Add(postToAdd);

                return Request.CreateResponse(HttpStatusCode.OK, PostResponseModel.FromEntity(postToAdd));
            });

            return messageResponse;
        }
Example #2
0
        public HttpResponseMessage PostCreatePost(string sessionKey, RequestPostModel model)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
                () =>
                {
                    var context = new BloggingSystemContext();
                    using (context)
                    {
                        var postCreatedBy = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey);

                        var post = new Post()
                        {
                            Title = model.Title,
                            Text = model.Text,
                            PostDate = DateTime.Now,
                            User = postCreatedBy,
                            //Tags =
                        };

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

                        //

                        //IEnumerable<string> modelTagsParts = model.Tags.ToString().Split(new char[] { ' ', ',', '!', '?' });

                        //List<TagModel> tagModels = new List<TagModel>();

                        //foreach (var tagName in modelTagsParts)
                        //{
                        //    var tagInDb = context.Tags.FirstOrDefault(t => t.Name == tagName);

                        //    if (tagInDb == null)
                        //    {
                        //        var newTagModel = new TagModel()
                        //        {
                        //            Name = tagName
                        //        };

                        //        tagModels.Add(newTagModel);
                        //    }
                        //    else
                        //    {
                        //        tagInDb.Post.Id = post.Id;
                        //        context.SaveChanges();
                        //    }
                        //}

                        //

                        var createdPost = new CreatedPostModel()
                        {
                            Id = post.Id,
                            Title = post.Title
                        };

                        var response =
                            this.Request.CreateResponse(HttpStatusCode.Created,
                                            createdPost);
                        return response;
                    }
                });

            return responseMsg;
        }