public async Task <IActionResult> AddNewUserGroupAsync([FromBody] AddUserGroupAc newUserGroupAc) { ApplicationUser currentUser = await _userManager.FindByNameAsync(User.Identity.Name); int currentUserInstituteId = await _instituteUserMappingHelperService.GetUserCurrentSelectedInstituteIdAsync(currentUser.Id, true); UserGroup existingUserGroup = await _iMSDbContext.UserGroups.FirstOrDefaultAsync(x => x.Code.Equals(newUserGroupAc.Code) && x.InstituteId == currentUserInstituteId); if (existingUserGroup != null) { return(Ok(new { Message = "User Role already exists with this code", HasError = true })); } if (ModelState.IsValid) { await _userGroupManagementRepository.AddNewUserGroupAsync(newUserGroupAc, currentUser); return(Ok(new { Message = "New User Role Added Successfully" })); } else { if (string.IsNullOrEmpty(newUserGroupAc.Code)) { return(Ok(new { Message = "User Role Code can't be empty", HasError = true })); } else { return(Ok(new { Message = "User Role Name can't be empty", HasError = true })); } } }
/// <summary> /// Method for updating an existing user group /// </summary> /// <param name="updatedUserGroupAc"></param> /// <returns></returns> public async Task UpdateUserGroupAsync(int id, AddUserGroupAc updatedUserGroupAc, ApplicationUser currentUser) { UserGroup existingUserGroup = await _iMSDbContext.UserGroups.FirstAsync(x => x.Id == id); // Update details existingUserGroup.Code = updatedUserGroupAc.Code; existingUserGroup.Name = updatedUserGroupAc.Name; existingUserGroup.Description = updatedUserGroupAc.Description; existingUserGroup.LastUpdatedUserId = currentUser.Id; existingUserGroup.LastUpdatedDate = DateTime.UtcNow; _iMSDbContext.UserGroups.Update(existingUserGroup); await _iMSDbContext.SaveChangesAsync(); }
/// <summary> /// Method for adding a new user group /// </summary> /// <param name="newUerGroupAc"></param> /// <param name="currentUser"></param> /// <returns></returns> public async Task AddNewUserGroupAsync(AddUserGroupAc newUserGroupAc, ApplicationUser currentUser) { int currentUserInstituteId = await _instituteUserMappingHelperService.GetUserCurrentSelectedInstituteIdAsync(currentUser.Id, true); UserGroup newUserGroup = new UserGroup() { Code = newUserGroupAc.Code, Name = newUserGroupAc.Name, Description = newUserGroupAc.Description, CreatedOn = DateTime.UtcNow, CreatedByUserId = currentUser.Id, InstituteId = currentUserInstituteId //CompanyWbs = 1 }; _iMSDbContext.UserGroups.Add(newUserGroup); await _iMSDbContext.SaveChangesAsync(); await SeedingUserGroupFeaturesAsync(newUserGroup.Id); }
public async Task <IActionResult> UpdateUserGroupAsync(int id, [FromBody] AddUserGroupAc updatedUserGroupAc) { ApplicationUser currentUser = await _userManager.FindByNameAsync(User.Identity.Name); int currentUserInstituteId = await _instituteUserMappingHelperService.GetUserCurrentSelectedInstituteIdAsync(currentUser.Id, true); UserGroup existingUserGroup = await _iMSDbContext.UserGroups.FirstOrDefaultAsync(x => x.Code.Equals(updatedUserGroupAc.Code) && x.InstituteId == currentUserInstituteId && x.Id != id); if (existingUserGroup != null) { return(Ok(new { Message = "User group already exists with this code", HasError = true })); } if (ModelState.IsValid) { if ((await _userGroupManagementRepository.GetUserGroupByIdAsync(id)) == null) { return(Ok(new { Message = "Please make sure the user group exists", HasError = true })); } await _userGroupManagementRepository.UpdateUserGroupAsync(id, updatedUserGroupAc, currentUser); return(Ok(new { Message = "User Group updated successfully" })); } else { if (string.IsNullOrEmpty(updatedUserGroupAc.Code)) { return(Ok(new { Message = "User Group Code can't be null or empty", HasError = true })); } else { return(Ok(new { Message = "User Group Name can't be null or empty", HasError = true })); } } }