public async Task <GroupDto> CreateGroup(GroupDto model, string userId) { var entity = _mapper.Map <GroupDto, Groups>(model); try { // add new group Groups group = null; using (var uow = _uowProvider.CreateUnitOfWork()) { var repository = uow.GetRepository <Groups>(); group = await repository.AddAsync(entity); await uow.SaveChangesAsync(); } if (model.ParentId == null) { // add member to group await _memberService.AddUserToGroup(new MemberGroups { GroupId = entity.Id, UserId = new Guid(userId) }); // add role to member user var user = await _userManager.FindByIdAsync(userId); await _userManager.AddToRoleAsync(user, "GroupAdmin", group.Id); } // add paths between new descendant and exist ancestors await _groupNodeService.AddPathsBetweenDescendantAndAncestors(entity.Id, model.ParentId); } catch (Exception ex) { throw ex; } return(_mapper.Map <Groups, GroupDto>(entity)); }