Пример #1
0
        public async Task EditUserRoles(editroles edit)
        {
            List <Role> rollist = new List <Role>();

            List <UserRole> usersrols = new List <UserRole>();

            var roles = await _dataContext.UserRoles
                        .Where(x => x.UserId == edit.id)
                        .ToListAsync();

            _dataContext.UserRoles
            .RemoveRange(roles);

            await _dataContext.SaveChangesAsync();

            List <User> user = _dataContext.Users
                               .Where(x => x.Id == edit.id)
                               .ToList();

            for (int i = 0; i < edit.roles.Count(); i++)
            {
                var role = _dataContext.Roles
                           .Where(x => x.Name == edit.roles[i])
                           .ToList();
                rollist.Add(role[0]);
            }

            for (int i = 0; i < rollist.Count(); i++)
            {
                var entity = _mapper.Map <User, UserRole>(user[0]);
                _mapper.Map <Role, UserRole>(rollist[i], entity);

                usersrols.Add(entity);
            }

            if (usersrols.Any())
            {
                await _dataContext.UserRoles.AddRangeAsync(usersrols);

                await _dataContext.SaveChangesAsync();
            }
        }
Пример #2
0
        public async Task <IActionResult> EditRoles(editroles edit)
        {
            await _unitofwork.admin.EditUserRoles(edit);

            return(Ok());
        }