Exemplo n.º 1
0
        public async Task <IActionResult> UpdateBlogForUser([FromBody] BlogForUpdateDto model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var blogFromRepo = await _repo.GetBlog(model.Id);

            if (blogFromRepo == null)
            {
                return(NotFound());
            }

            if (!await this.MatchAppUserWithToken(blogFromRepo.OwnerId))
            {
                return(Unauthorized());
            }

            this._mapper.Map(model, blogFromRepo);
            if (await _repo.SaveAll() > 0)
            {
                return(Ok());
            }

            return(BadRequest("ブログ情報の更新に失敗しました"));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> UpdateBlog(int userId, int id,
                                                     BlogForUpdateDto blogForUpdateDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var blogFromRepo = await _repo.GetBlog(id);

            _mapper.Map(blogForUpdateDto, blogFromRepo);
            blogFromRepo.Created = DateTime.Now;

            if (await _repo.SaveAll())
            {
                return(NoContent());
            }

            throw new Exception($"Updating blog {id} failed on save");
        }