コード例 #1
0
        public HttpResponseMessage PostAddPost(PostModel postData, [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
                () =>
                {
                    var context = new BloggingSystemContext();
                    using (context)
                    {
                        var user = GetAndValidateUser(sessionKey, context);

                        ValidatePost(postData);

                        var postEntity = new Post()
                        {
                            Text = postData.Text,
                            Title = postData.Title
                        };

                        postEntity.PostDate = DateTime.Now;
                        postEntity.User = user;

                        AddTags(context, postData, postEntity);
                        AddTitleTags(context, postData, postEntity);

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

                        var postCreatedModel = new PostCreatedModel() { Id = postEntity.Id, Title = postEntity.Title };

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

            return responseMsg;
        }
コード例 #2
0
        public HttpResponseMessage PostCreate(PostModel postModel, 
            [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey)
        {
            var responseMsg = this.ExceptionHandling(
            () =>
            {
                var context = new BloggingSystemContext();
                using (context)
                {
                    var user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey);

                    if (user == null)
                    {
                        throw new InvalidOperationException("Invalid username or password!");
                    }

                    List<string> tags = new List<string>();
                    tags = TakeTags(postModel.Title);
                    foreach (var tagEntity in tags)
                    {
                        var tagInDb = context.Tags.FirstOrDefault(t => t.Name == tagEntity);

                        if(tagInDb == null)
                        {
                            var tag = new Tag()
                            {
                                Name = tagEntity,
                                User = user
                            };
                            context.Tags.Add(tag);
                            context.SaveChanges();
                        }
                    }

                    foreach (var tag in postModel.Tags)
                    {
                        var tagInDb = context.Tags.FirstOrDefault(t => t.Name == tag);

                        if (tagInDb == null)
                        {
                            var newTag = new Tag()
                            {
                                Name = tag,
                                User = user
                            };
                            context.Tags.Add(newTag);
                            context.SaveChanges();
                        }
                    }

                    var post = new Post()
                    {
                        Title = postModel.Title,
                        Text = postModel.Text,
                        User = user,
                        PostDate = DateTime.Now
                    };

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

                    var postCreated = new PostCreatedModel()
                    {
                        Id = post.Id,
                        Title = post.Title
                    };

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

            return responseMsg;
        }