Exemplo n.º 1
0
        public IActionResult Register([FromBody] User user)
        {
            //Validate the user
            var validation = new RegisterUserValidation();
            var results    = validation.Validate(user);

            //If if valid, then register
            if (results.IsValid)
            {
                try
                {
                    var userRegistred = this.userServices.Register(user);
                    return(Ok(userRegistred));
                }
                catch (UserExistentException ex)
                {
                    logger.LogInformation(ex.Message);
                    return(BadRequest(new { ex.Message }));
                }
                catch (Exception ex)
                {
                    logger.LogError(ex, ex.Message);
                    return(StatusCode(StatusCodes.Status500InternalServerError));
                }
            }
            else
            {
                return(BadRequest(results.Errors.Select(x => new { x.PropertyName, x.ErrorMessage })));
            }
        }
Exemplo n.º 2
0
        public IActionResult Register([FromBody] User user)
        {
            //Validate the user
            var validation = new RegisterUserValidation();
            var results    = validation.Validate(user);

            //If if valid, then register
            if (results.IsValid)
            {
                try
                {
                    return(Ok(this.userServices.Register(user)));
                }
                catch (UserExistentException ex)
                {
                    return(BadRequest(new Error <UserExistentException>(ex)));
                }
                catch (Exception ex)
                {
                    logger.LogError(ex, ex.Message);
                    return(StatusCode(StatusCodes.Status500InternalServerError));
                }
            }
            else
            {
                return(BadRequest(results.Errors));
            }
        }
        public async Task <IActionResult> Register([FromBody] User user)
        {
            //Validate the user
            var validation = new RegisterUserValidation();
            var results    = validation.Validate(user);

            //If if valid, then register
            if (results.IsValid)
            {
                try
                {
                    var userRegistred = this.userServices.Register(user);

                    var pageModel = new AccountCreatedSuccessfullyModel();
                    pageModel.Name  = user.Name;
                    pageModel.Email = user.Email;

                    var         emailConfig = EmailConfiguration.GetFromConfiguration(configuration, "No Reply");
                    EmailSender emailSender = new EmailSender(emailConfig);
                    emailSender.To = user.Email;

                    templateEmailSender.EmailSender = emailSender;
                    templateEmailSender.SendAsync(pageModel);

                    return(Ok(userRegistred));
                }
                catch (UserExistentException ex)
                {
                    logger.LogInformation(ex.Message);
                    return(BadRequest(new { ex.Message, ex.ErrorCode }));
                }
                catch (Exception ex)
                {
                    logger.LogError(ex, ex.Message);
                    return(StatusCode(StatusCodes.Status500InternalServerError));
                }
            }
            else
            {
                return(BadRequest(results.Errors));
            }
        }