Exemple #1
0
        public async Task <IActionResult> GetById(int id)
        {
            SuccessResponseUser success;

            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(new ErrorResponse(Utils.Helper.GetErrors(ModelState), 1)));
                }
                success = new SuccessResponseUser();
                UserResponse oUser = await _service.GetById(id);

                success.Data = oUser;
                if (oUser == null)
                {
                    string[] aErrors = { Utils.Constants.NOT_FOUND };
                    return(NotFound(new ErrorResponse(aErrors, 1)));
                }
                return(Ok(success));
            }
            catch (Exception e)
            {
                string[] aErrors = { e.Message };
                return(StatusCode(500, new ErrorResponse(aErrors, -1)));
            }
        }
Exemple #2
0
        public async Task <IActionResult> Post(UserRequest model)
        {
            try {
                if (await _service.UserNameExists(model))
                {
                    string[] aErrors = { Utils.Constants.USERNAME_EXIST };
                    return(BadRequest(new ErrorResponse(aErrors, 1)));
                }
                if (await _service.EmailExists(model.Email))
                {
                    string[] aErrors = { Utils.Constants.EMAIL_EXISTS };
                    return(BadRequest(new ErrorResponse(aErrors, 1)));
                }
                model.Password = Utils.Helper.GetSHA256(model.Password);
                Users userAdded = await _service.Add(model);

                UserResponse newUser = new UserResponse();
                newUser.PkuserId = userAdded.PkuserId;
                newUser.Email    = userAdded.Email;
                newUser.UserName = userAdded.UserName;
                newUser.PkuserId = userAdded.PkuserId;
                newUser.PkuserId = userAdded.PkuserId;
                SuccessResponseUser response = new SuccessResponseUser
                {
                    Data    = newUser,
                    Message = "Usuario registrado con exito"
                };
                return(CreatedAtAction(actionName: "GetById", controllerName: "Users", routeValues: new { id = newUser.PkuserId }, response));
            }
            catch (Exception e)
            {
                string[] aErrors = { e.Message };
                return(StatusCode(500, new ErrorResponse(aErrors, -1)));
            }
        }