Esempio n. 1
0
        // api/post
        public IHttpActionResult PostAddNewPost(NewPostBindingModel model)
        {
            if (model == null)
            {
                return(this.BadRequest("Empty post is not allowed!"));
            }

            if (!this.ModelState.IsValid)
            {
                return(this.BadRequest(this.ModelState));
            }

            var user = this.Data.ApplicationUsers.GetById(this.UserIdProvider.GetUserId());
            var post = new Post()
            {
                Title   = model.Title,
                Comment = model.Comment,
                Replies = new List <Reply>(),
                Author  = user
            };

            this.Data.Posts.Add(post);
            this.Data.SaveChanges();

            var postView = new PostViewModel()
            {
                Id         = post.Id,
                AuthorName = post.Author.UserName,
                Title      = post.Title,
                Comment    = post.Comment,
                CreateAt   = post.CreatedAt,
            };

            return(this.Ok(postView));
        }
        // api/post
        public IHttpActionResult PostAddNewPost(NewPostBindingModel model)
        {
            if (model == null)
            {
                return this.BadRequest("Empty post is not allowed!");
            }

            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var user = this.Data.ApplicationUsers.GetById(this.UserIdProvider.GetUserId());
            var post = new Post()
            {
                Title = model.Title,
                Comment = model.Comment,
                Replies = new List<Reply>(),
                Author = user
            };

            this.Data.Posts.Add(post);
            this.Data.SaveChanges();

            var postView = new PostViewModel()
            {
                Id = post.Id,
                AuthorName = post.Author.UserName,
                Title = post.Title,
                Comment = post.Comment,
                CreateAt = post.CreatedAt,
            };

            return this.Ok(postView);
        }