Exemple #1
0
        public async Task <IActionResult> Register([FromBody] RegisterForDto model)
        {
            ApiResponse <RegisterForDto> response = new ApiResponse <RegisterForDto>();

            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }
                if (string.IsNullOrEmpty(model.Password) || string.IsNullOrEmpty(model.RTPassword))
                {
                    return(BadRequest(new { error = "Password & Repeat password is required." }));
                }
                if (model.Password != model.RTPassword)
                {
                    return(BadRequest(new { error = "Password & Repeat password does not match." }));
                }
                if (await _userSrv.UserExists(model.Email))
                {
                    ModelState.AddModelError("Email", "Email already taken");
                }
                var IsPhoneAlreadyRegistered = _userManager.Users.Any(item => item.PhoneNumber == model.PhoneNumber);
                if (IsPhoneAlreadyRegistered == true)
                {
                    return(BadRequest(new { error = $"{model.PhoneNumber} Already exist!" }));
                }

                var user = AppUser.Create(model.FirstName, model.LastName, model.Email, model.Gender, model.PhoneNumber, model.NIN);

                user.UserName       = model.Email;
                user.EmailConfirmed = true;
                user.FullName       = model.FirstName + " " + model.LastName;
                user.Activated      = true;
                user.IsDisabled     = false;
                user.CreatedOnUtc   = DateTime.Now.GetDateUtcNow();
                user.LockoutEnabled = false;

                var createResult = await _userManager.CreateAsync(user, model.Password);

                if (!createResult.Succeeded)
                {
                    return(BadRequest(new { error = $"{createResult.Errors.FirstOrDefault().Description}" }));
                }
                createResult = await _userManager.AddToRoleAsync(user, "USER");

                if (!createResult.Succeeded)
                {
                    return(BadRequest(new { error = $"{createResult.Errors.FirstOrDefault().Description}" }));
                }
                response.Code        = ApiResponseCodes.OK;
                response.Description = $"Registration Successful";
                return(Ok(response));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
        public async Task <IActionResult> AddUser([FromBody] AddUserDto payload)
        {
            ApiResponse <AddUserDto> response = new ApiResponse <AddUserDto>();

            try
            {
                if (!response.Errors.Any())
                {
                    if (string.IsNullOrEmpty(payload.Password) || string.IsNullOrEmpty(payload.RTPassword))
                    {
                        return(BadRequest(new { errorList = "Password & Repeat password is required." }));
                    }
                    if (payload.Password != payload.RTPassword)
                    {
                        return(BadRequest(new { errorList = "Password & Repeat password does not match." }));
                    }
                    if (await _userSrv.UserExists(payload.Email))
                    {
                        return(BadRequest(new { errorList = "Email is already taken" }));
                    }
                    if (await _userSrv.PhoneExists(payload.PhoneNumber))
                    {
                        return(BadRequest(new { errorList = "Phone Number is already taken" }));
                    }
                    (List <ValidationResult> Result, AddUserDto User)errorResult = await _userSrv.CreateUser(payload);

                    if (errorResult.Result.Any())
                    {
                        return(BadRequest(new { errorList = $"{errorResult.Result.FirstOrDefault().ErrorMessage}" }));
                    }
                    else
                    {
                        response.Code        = ApiResponseCodes.OK;
                        response.Description = $"User creation successful.";
                    }
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
            return(Ok(response));
        }