Exemplo n.º 1
0
        public IActionResult CreateUser([FromBody] User user)
        {
            try
            {
                if (user.IsEntityNull())
                {
                    return(BadRequest("User object is null"));
                }

                if (!user.IsEntityEmpty())
                {
                    return(BadRequest("For create, the Id must be null"));
                }

                if (!ModelState.IsValid)
                {
                    return(BadRequest("Invalid model object"));
                }

                _userService.CreateUser(user);
                _userService.Save();

                return(CreatedAtRoute("UserById", new { id = user.Id.Value }, user));
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "Error in call : api/user/CreateUser", user);
                return(StatusCode(StatusCodes.Status500InternalServerError, "Internal server error"));
            }
        }
Exemplo n.º 2
0
        public IActionResult UpdateUser(int id, [FromBody] User user)
        {
            try
            {
                if (user.IsEntityNull())
                {
                    return(BadRequest("User object is null"));
                }

                if (!ModelState.IsValid)
                {
                    return(BadRequest("Invalid model object"));
                }

                User dbUser = _userService.GetUserById(id);
                if (dbUser.IsEntityNull())
                {
                    _logger.Error($"User with id: {id} not found in db");
                    return(NotFound());
                }

                _userService.UpdateUser(dbUser, user);
                _userService.Save();

                return(NoContent());
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "Error in call : api/user/UpdateUser/" + id, user);
                return(StatusCode(StatusCodes.Status500InternalServerError, "Internal server error"));
            }
        }
Exemplo n.º 3
0
        public IActionResult DeleteUser(int id)
        {
            try
            {
                User user = _userService.GetUserById(id);
                if (user.IsEntityNull())
                {
                    _logger.Error($"User with id: {id} not found in db");
                    return(NotFound());
                }

                _userService.DeleteUser(user);
                _userService.Save();

                return(NoContent());
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "Error in call : api/user/DeleteUser/" + id);
                return(StatusCode(StatusCodes.Status500InternalServerError, "Internal server error"));
            }
        }