public void UpdateUser_ForUserIdLessThanZero_Throws() { // Init var appUserRepository = new Mock <IUserRepository>(); appUserRepository.Setup(repo => repo.GetById(It.IsAny <long>())) .Returns(new Func <long, AppUser>(id => _appUsers.Find(p => p.Id.Equals(id)))); appUserRepository.Setup(repo => repo.Update(It.IsAny <AppUser>())); var userService = SetupUserService(appUserRepository); // Execute var userId = -1; var userToUpdatedto = new UserResDto { Email = "[email protected]", FirstName = "update first name", LastName = "update last name", MiddleName = "update middle name", PhoneNumber = "update phone number", AddressId = 1 }; // Assert Assert.Throws <ArgumentOutOfRangeException>(() => userService.UpdateUser(userId, userToUpdatedto)); }
public void UpdateUser_ForAnExistingUserIdAndUser_ReturnsTrue() { // Init var appUserRepository = new Mock <IUserRepository>(); appUserRepository.Setup(repo => repo.GetById(It.IsAny <long>())) .Returns(new Func <long, AppUser>(id => _appUsers.Find(p => p.Id.Equals(id)))); appUserRepository.Setup(repo => repo.Update(It.IsAny <AppUser>())); var userService = SetupUserService(appUserRepository); // Execute var userId = MaxUsersCount; var userToUpdatedto = new UserResDto { Email = "[email protected]", FirstName = "update first name", LastName = "update last name", MiddleName = "update middle name", PhoneNumber = "update phone number", AddressId = 1 }; var result = userService.UpdateUser(userId, userToUpdatedto); // Assert Assert.IsTrue(result); }
public void UpdateUser_ForExistingUserId_AndNullUserDto_Throws() { // Init var appUserRepository = new Mock <IUserRepository>(); appUserRepository.Setup(repo => repo.GetById(It.IsAny <long>())) .Returns(new Func <long, AppUser>(id => _appUsers.Find(p => p.Id.Equals(id)))); appUserRepository.Setup(repo => repo.Update(It.IsAny <AppUser>())); var userService = SetupUserService(appUserRepository); // Execute var userId = MaxUsersCount; UserResDto userToUpdatedto = null; // Assert Assert.Throws <ArgumentNullException>(() => userService.UpdateUser(userId, userToUpdatedto)); }
public async Task <IActionResult> GetUsers() { UserResDto responseResult = new UserResDto(); List <User> lstUsers = new List <User>(); try { lstUsers = await uow.UserRepository.GetUsersAsync(); List <UserReqDto> lstreqDto = new List <UserReqDto>(); foreach (var item in lstUsers) { UserReqDto reqDto = new UserReqDto(); reqDto = mapper.Map <UserReqDto>(item); List <UserCompanyList> lstUserCompany = new List <UserCompanyList>(); List <UserCompany> lstcom = await uow.UserCompanyRepository.GetUserCompanyByUserId(reqDto.userId); foreach (var userCompany in lstcom) { UserCompanyList lst = new UserCompanyList(); lst.companyCode = userCompany.CompanyCode; lst.companyName = userCompany.CompanyName; lstUserCompany.Add(lst); } reqDto.userCompanyList = lstUserCompany; lstreqDto.Add(reqDto); } responseResult.UserReqDto = lstreqDto; responseResult.messageType = "S"; } catch (Exception ex) { logger.Log(LogLevel.Error, ex.Message); } return(Ok(new { responseDto = responseResult })); }
public bool UpdateUser(long id, UserResDto userDto) { if (userDto == null) { throw new ArgumentNullException($"{nameof(userDto)} can not be null."); } var user = GetById(id, UnitOfWork.AppUsers); if (userDto.AddressId.HasValue) { user.DefaultAddressId = userDto.AddressId.Value; } user.Email = userDto.Email; user.FirstName = userDto.FirstName; user.LastName = userDto.LastName; user.MiddleName = userDto.MiddleName; user.PhoneNumber = userDto.PhoneNumber; UnitOfWork.AppUsers.Update(user); UnitOfWork.Save(); return(true); }
public async Task <IActionResult> AddEditUser([FromBody] UserReqDto userData) { ResponseResultDto res = new ResponseResultDto(); UserResDto responseResult = new UserResDto(); try { var identity = User.Identity as ClaimsIdentity; if (identity != null) { IEnumerable <Claim> claims = identity.Claims; var CompanyId = claims.Where(p => p.Type == "companyId").FirstOrDefault()?.Value; int companyID = Convert.ToInt32(CompanyId); var UserId = claims.Where(p => p.Type == "userId").FirstOrDefault()?.Value; int userID = Convert.ToInt32(UserId); if (uow.UserRepository.IsUserNameExists(userData.userName, userData.userId)) { res.MessageType = "E"; res.Message = String.Format(MessageConstant.RecordAlreadyExists, "username '" + userData.userName + "'"); return(Ok(new { ResponseResultDto = res })); } var user = mapper.Map <User>(userData); user.ModifiedOn = DateTime.Now; user.ModifiedBy = userID; if (user.userId == 0) { user.CreatedOn = DateTime.Now; user.CreatedBy = userID; user.password = CommonFuntions.Encrypt(user.password); uow.UserRepository.AddUser(user); } else { User objUser = (await uow.UserRepository.FindUser(userData.userId)).FirstOrDefault(); user.password = CommonFuntions.Encrypt(user.password); user.CreatedOn = objUser.CreatedOn; user.CreatedBy = objUser.CreatedBy; uow.UserRepository.UpdateUser(user); uow.UserCompanyRepository.DeleteUserCompanyByUserId(user.userId); } List <UserCompany> lstuserCompany = new List <UserCompany>(); foreach (var item in userData.userCompanyList) { UserCompany userCompany = new UserCompany(); userCompany.CompanyCode = item.companyCode; userCompany.CompanyName = item.companyName; userCompany.CreatedBy = userID; userCompany.CreatedOn = DateTime.Now; userCompany.userID = user.userId; lstuserCompany.Add(userCompany); } user.Usercompany = lstuserCompany; uow.UserCompanyRepository.AddUserCompany(lstuserCompany); await uow.SaveAsync(); List <User> lstUsers = new List <User>(); lstUsers = await uow.UserRepository.GetUsersAsync(); List <UserReqDto> lstreqDto = new List <UserReqDto>(); foreach (var item in lstUsers) { UserReqDto reqDto = new UserReqDto(); reqDto = mapper.Map <UserReqDto>(item); List <UserCompanyList> lstUserCompany = new List <UserCompanyList>(); List <UserCompany> lstcom = await uow.UserCompanyRepository.GetUserCompanyByUserId(reqDto.userId); foreach (var userCompany in lstcom) { UserCompanyList lst = new UserCompanyList(); lst.companyCode = userCompany.CompanyCode; lst.companyName = userCompany.CompanyName; lstUserCompany.Add(lst); } reqDto.userCompanyList = lstUserCompany; lstreqDto.Add(reqDto); } responseResult.UserReqDto = lstreqDto; responseResult.messageType = "S"; if (userData.userId == 0) { responseResult.message = MessageConstant.RecordAdded; } else { responseResult.message = MessageConstant.RecordUpdated; } } else { responseResult.messageType = "E"; responseResult.message = MessageConstant.Something_went_wrong; } } catch (Exception ex) { responseResult.messageType = "E"; if (userData.userId == 0) { responseResult.message = MessageConstant.FailedToAddRecord; } else { responseResult.message = MessageConstant.FailedToUpdateRecord; } logger.Log(LogLevel.Error, ex.Message); } return(Ok(new { responseDto = responseResult })); }