Esempio n. 1
0
        public async Task <bool> InsertUserScoreBand(UserScoreBandModel userScoreBandViewModel)
        {
            var userBand = new UserScoreBand()
            {
                UserId    = userScoreBandViewModel.UserId,
                ScoreList = userScoreBandViewModel.ScoreList
            };
            var result = await _userService.InsertUserScoreBand(userBand);

            return(result > 0);
        }
        public async Task <IActionResult> UpdateUser([FromBody] UserModel userModel)
        {
            if (ModelState.IsValid == true)
            {
                ApplicationUser applicationUser = await _userManager.FindByIdAsync(userModel.Id);

                if (applicationUser != null)
                {
                    applicationUser.UserName = userModel.Username;
                    //var test = await _userManager.RemovePasswordAsync(applicationUser);

                    //var isNewPwdSet = await _userManager.AddPasswordAsync(applicationUser, userModel.Password);
                    var isNewPwdSet = await _userManager.ChangePasswordAsync(applicationUser, userModel.CurrentPassword, userModel.Password);

                    if (isNewPwdSet.Succeeded)
                    {
                        var isUpdated = await _userManager.UpdateAsync(applicationUser);

                        if (isUpdated.Succeeded)
                        {
                            var tokenInsertResult = await _appAdminService.InsertUserToken(userModel.Id, userModel.Token);

                            if (tokenInsertResult == true)
                            {
                                string scoreList          = string.Join(",", userModel.ScoreBands);
                                var    userScoreBandModel = new UserScoreBandModel()
                                {
                                    UserId    = userModel.Id,
                                    ScoreList = scoreList
                                };
                                var scoreBandInsetResult = await _appAdminService.InsertUserScoreBand(userScoreBandModel);

                                return(new OkObjectResult(new { Status = true, Message = "User created successfully." }));
                            }
                        }
                    }
                }
                return(new OkObjectResult(new { Status = false, Message = "User update failed." }));
            }
            return(new BadRequestObjectResult(new { Status = false, ModelError = ModelState.GetErrors() }));
        }
        public async Task <IActionResult> CreateUser([FromBody] UserRegisterModel userRegisterModel)
        {
            if (ModelState.IsValid == true)
            {
                ApplicationUser applicationUser = new ApplicationUser();
                applicationUser.UserName = userRegisterModel.Username;
                applicationUser.Email    = userRegisterModel.Username + "@example.com";
                string userId = applicationUser.Id;
                var    result = await _userManager.CreateAsync(applicationUser, userRegisterModel.Password);

                if (result.Succeeded)
                {
                    applicationUser.Id = userId;
                    var isRoleCreated = await _userManager.AddToRoleAsync(applicationUser, "User");

                    if (isRoleCreated.Succeeded)
                    {
                        var tokenInsertResult = await _appAdminService.InsertUserToken(userId, userRegisterModel.Token);

                        if (tokenInsertResult == true)
                        {
                            string scoreList          = string.Join(",", userRegisterModel.ScoreBandList);
                            var    userScoreBandModel = new UserScoreBandModel()
                            {
                                UserId    = userId,
                                ScoreList = scoreList
                            };
                            var scoreBandInsetResult = await _appAdminService.InsertUserScoreBand(userScoreBandModel);

                            return(new OkObjectResult(new { Status = true, Message = "User created successfully." }));
                        }
                    }
                }
                return(new OkObjectResult(new { Status = false, Message = "User creation failed." }));
            }
            return(new BadRequestObjectResult(new { Status = false, ModelError = ModelState.GetErrors() }));
        }