Ejemplo n.º 1
0
        public async Task <IActionResult> Store([FromBody] Models.Validators.UserRegister item)
        {
            if (item is null)
            {
                return(BadRequest(_apiResponse.AddErrorResponse(code: "InvalidFormData").ErrorReponse));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(_apiResponse.ParseModelStateResponse(ModelState).ErrorReponse));
            }

            var user = new User {
                UserName = item.UserName, Email = item.Email
            };
            var result = await _users.UserManager.CreateAsync(user, item.Password);

            if (result.Succeeded == false)
            {
                foreach (var error in result.Errors)
                {
                    _apiResponse.AddErrorResponse(code: error.Code, detail: error.Description);
                }

                return(BadRequest(_apiResponse.ErrorReponse));
            }

            var code = await _users.UserManager.GenerateEmailConfirmationTokenAsync(user);

            await this._emails.sendEmailFromTemplateAsync(user.Email, "Registered", "Register", new Dictionary <string, object> {
                { "Name", user.UserName },
                { "Code", code }
            });

            return(StatusCode(StatusCodes.Status201Created, new
            {
                data = new { id = user.Id, code = code }
            }));
        }