示例#1
0
        public IHttpActionResult Update(int id, PostOutputData post)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var exisitingPost = this.data.Posts.All().FirstOrDefault(u => u.Id == id);
            if (exisitingPost == null)
            {
                return BadRequest("Such post does not exists!");
            }

            exisitingPost.Title = post.Title ?? exisitingPost.Title;
            exisitingPost.Content = post.Content ?? exisitingPost.Content;


            this.data.Posts.Update(exisitingPost);
            this.data.SaveChanges();

            return Ok();
        }
示例#2
0
        public IHttpActionResult Create(int id, PostOutputData post)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var existingUser = this.data.Users.All().FirstOrDefault(u => u.Id == id);
            if (existingUser == null)
            {
                return BadRequest("Such user does not exists!");
            }

            var newPost = new Post
            {
                Title = post.Title,
                Content = post.Content
            };

            existingUser.Posts.Add(newPost);
            this.data.SaveChanges();

            return Ok(newPost.Id);
        }