Пример #1
0
        public async Task <IActionResult> UpdateTodo(int userId, int id, TodoForUpdateDto todoForUpdateDto)
        {
            var userTokenId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            if (userId != userTokenId)
            {
                _log = "'UpdateTodo' Error: 'Unauthorized' on user with id: '" + userId + "' and on todo with id: '" + id + "' from user with id: '" + userTokenId + "'";
                _logger.Warning(_log);

                return(Unauthorized());
            }

            var todoFromRepo = await _repo.GetTodo(id);

            _mapper.Map(todoForUpdateDto, todoFromRepo);

            if (await _repo.SaveAll())
            {
                _log = "'UpdateTodo' Successful: on user with id: '" + userId + "' and on todo with id: '" + id + "' from user with id: '" + userTokenId + "'";
                _logger.Information(_log);

                return(NoContent());
            }

            _log = "'UpdateTodo' Error: on user with id: '" + userId + "' and on todo with id: '" + id + "'  failed on save from user with id: '" + userTokenId + "'";
            _logger.Error(_log);
            throw new Exception($"Updating todo {id} failed on save");
        }
        public IActionResult UpdateTodo([FromBody] TodoForUpdateDto todo, int id)
        {
            if (ModelState.IsValid)
            {
                var todoForUpDate = Mapper.Map <Todo>(todo);
                _uow.Todos.Update(id, todoForUpDate);
                _uow.Commit();
                return(Ok());
            }

            return(BadRequest());
        }