public async Task <IActionResult> UpdateUser(Guid id, [FromBody] RegisterUserDataObject userInput) { return(Ok(await _userAppService.AddOrUpdateUserAsync(CurrentUser.Id.Value, id, false, userInput))); }
public async Task <RESTResult> AddOrUpdateUserAsync(Guid currentUserId, Guid id, bool isAdd, RegisterUserDataObject input) { RESTResult result = new RESTResult { Code = RESTStatus.Failed }; if (!RegUtility.IsValidationForEmail(input.AccountEmail)) { throw new ArgumentException("Wrong Email format!"); } if (!string.IsNullOrEmpty(input.AccountPhone) && !RegUtility.IsValidationForPhone(input.AccountPhone)) { throw new ArgumentException("Wrong Phone format!"); } if (isAdd) { //Check the user whteher exist in User var userEntities = await _userRepository.GetAllListAsync(item => item.AccountEmail.Equals(input.AccountEmail) || item.AccountCode.Equals(input.AccountCode) || item.AccountPhone.Equals(input.AccountPhone)); if (userEntities.Count > 0) { throw new ArgumentException($"The user {input.AccountEmail} {input.AccountCode} {input.AccountEmail} have been exist"); } var userEntity = new User(); userEntity.Id = Guid.NewGuid(); userEntity.AccountEmail = input.AccountEmail; userEntity.AccountPhone = input.AccountPhone; userEntity.AccountCode = input.AccountCode; //It need to be optimized. userEntity.LastLoginIP = "127.0.0.1"; userEntity.CreateTime = DateTime.Now; userEntity.LastLoginTime = DateTime.Now; userEntity.Password = HashUtility.CreateHash(string.IsNullOrEmpty(input.Password) ? GeneralPassword(input.AccountEmail) : input.Password); await _userRepository.InsertAsync(userEntity); if (input.UserInfo != null) { var userInfoEntity = Mapper.Map <UserInfoDataTransferObject, UserInfo>(input.UserInfo); await _userInfoRepository.InsertAsync(userInfoEntity); } await SetUserAndDepartmentMapAsync(currentUserId, userEntity.Id, input.DepartmentId); await SetUserAndRoleMapAsync(currentUserId, userEntity.Id, input.RoleIds); result.Code = RESTStatus.Success; result.Message = "Add user successful"; } else { if (id == null || Guid.Empty.Equals(id)) { throw new ArgumentException("The user id can not be null"); } //TODO:Update the User var userEntity = await _userRepository.GetAsync(id); if (userEntity == null) { result.Message = $"The user(id is {id}) doesn't exist"; return(result); } userEntity.AccountEmail = input.AccountEmail; userEntity.AccountPhone = input.AccountPhone; userEntity.ModifyByUserId = currentUserId; userEntity.ModifyTime = DateTime.Now; await _userRepository.UpdateAsync(userEntity); //TODO:Update the user info; var userInfoEntity = (await _userInfoRepository.GetAllListAsync(item => item.UserId.Equals(id))).FirstOrDefault(); if (userInfoEntity != null) { var userInfoModel = Mapper.Map <UserInfoDataTransferObject, UserInfo>(input.UserInfo); userInfoModel.Id = userInfoEntity.Id; userInfoModel.UserId = id; await _userInfoRepository.UpdateAsync(userInfoModel); } else { //TODO:Add new Informations var userinfoModel = Mapper.Map <UserInfoDataTransferObject, UserInfo>(input.UserInfo); userinfoModel.UserId = userEntity.Id; await _userInfoRepository.InsertAsync(userinfoModel); } // //TODO:Update the department; await SetUserAndDepartmentMapAsync(currentUserId, userEntity.Id, input.DepartmentId); //TODO:Remove before user role; await RemoveBeforeUserRole(userEntity.Id); //TODO:update the role; await SetUserAndRoleMapAsync(currentUserId, userEntity.Id, input.RoleIds); result.Code = RESTStatus.Success; result.Message = "update user successful"; } return(result); }
public async Task <IActionResult> CreateUser([FromBody] RegisterUserDataObject userInput) { return(Ok(await _userAppService.AddOrUpdateUserAsync(CurrentUser.Id.Value, Guid.Empty, true, userInput))); }