Exemplo n.º 1
0
        public async Task <HttpResponseMessage> Update(HttpRequestMessage request, AppGroupVm appGroupViewModel)
        {
            if (ModelState.IsValid)
            {
                var appGroup = _appGroupService.GetDetail(appGroupViewModel.Id);
                try
                {
                    appGroup.UpdateAppGroup(appGroupViewModel);
                    _appGroupService.Update(appGroup);
                    //save group
                    var listRoleGroup = new List <AppRoleGroup>();
                    foreach (var role in appGroupViewModel.Roles)
                    {
                        listRoleGroup.Add(new AppRoleGroup()
                        {
                            GroupId = appGroup.Id,
                            RoleId  = role.Id
                        });
                    }
                    _appRoleService.AddRolesToGroup(listRoleGroup, appGroup.Id);

                    //add role to user
                    var listRole        = _appRoleService.GetListRoleByGroupId(appGroup.Id);
                    var listUserInGroup = _appGroupService.GetListUserByGroupId(appGroup.Id);
                    foreach (var user in listUserInGroup)
                    {
                        var listRoleName = listRole.Select(x => x.Name).ToArray();
                        foreach (var roleName in listRoleName)
                        {
                            await _userManager.RemoveFromRoleAsync(user.Id, roleName);

                            await _userManager.AddToRoleAsync(user.Id, roleName);
                        }
                    }
                    return(request.CreateResponse(HttpStatusCode.OK, appGroupViewModel));
                }
                catch (NameDuplicatedException dex)
                {
                    return(request.CreateErrorResponse(HttpStatusCode.BadRequest, dex.Message));
                }
            }
            else
            {
                return(request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }
        }
Exemplo n.º 2
0
        public HttpResponseMessage Create(HttpRequestMessage request, AppGroupVm appGroupViewModel)
        {
            if (ModelState.IsValid)
            {
                var newAppGroup = new AppGroup();
                newAppGroup.Name = appGroupViewModel.Name;
                try
                {
                    var appGroup = _appGroupService.Insert(newAppGroup);
                    //_appGroupService.Save();

                    //save group
                    var listRoleGroup = new List <AppRoleGroup>();
                    foreach (var role in appGroupViewModel.Roles)
                    {
                        listRoleGroup.Add(new AppRoleGroup()
                        {
                            GroupId = appGroup.Id,
                            RoleId  = role.Id
                        });
                    }
                    _appRoleService.AddRolesToGroup(listRoleGroup, appGroup.Id);
                    //_appRoleService.Save();

                    return(request.CreateResponse(HttpStatusCode.OK, appGroupViewModel));
                }
                catch (NameDuplicatedException dex)
                {
                    return(request.CreateErrorResponse(HttpStatusCode.BadRequest, dex.Message));
                }
            }
            else
            {
                return(request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }
        }
Exemplo n.º 3
0
 public static void UpdateAppGroup(this AppGroup appGroup, AppGroupVm appGroupViewModel)
 {
     appGroup.Id   = appGroupViewModel.Id;
     appGroup.Name = appGroupViewModel.Name;
 }