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 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 })); }