public HttpResponseMessage Created(HttpRequestMessage request, ApplicationGroupViewModel applicationGroupVM)
        {
            return(CreateReponse(request, () =>
            {
                HttpResponseMessage response;
                if (!ModelState.IsValid)
                {
                    response = request.CreateResponse(HttpStatusCode.BadRequest, ModelState);
                }
                else
                {
                    var newApplicationGroup = new ApplicationGroup();
                    newApplicationGroup.UpdateApplicationGroup(applicationGroupVM);
                    var appGroup = _applicationGroupService.Add(newApplicationGroup);
                    _applicationGroupService.save();

                    var list = new List <ApplicationRoleGroup>();
                    foreach (var role in applicationGroupVM.Roles)
                    {
                        list.Add(new ApplicationRoleGroup()
                        {
                            RoleId = role.Id,
                            GroupId = appGroup.Id
                        });
                    }
                    _applicationRoleService.AddRoleGroup(list, appGroup.Id);
                    _applicationRoleService.Save();
                    response = request.CreateResponse(HttpStatusCode.OK, appGroup);
                }
                return response;
            }));
        }
Пример #2
0
 public HttpResponseMessage Post(HttpRequestMessage request, ApplicationGroupViewModel appGroup)
 {
     return(CreateHttpResponse(request, () =>
     {
         HttpResponseMessage response = null;
         if (!ModelState.IsValid)
         {
             request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
         }
         else
         {
             var newAppGroup = new ApplicationGroup();
             newAppGroup.UpdateApplicationGroup(appGroup);
             var result = _appGroupService.Add(newAppGroup);
             if (result.IsSuccessStatusCode)
             {
                 var newGroup = result.Content.ReadAsAsync <ApplicationGroupViewModel>().Result;
                 response = request.CreateResponse(result.StatusCode, newGroup);
             }
             else
             {
                 response = request.CreateErrorResponse(result.StatusCode, result.Content.ReadAsStringAsync().Result);
             }
         }
         return response;
     }));
 }
        public async Task <HttpResponseMessage> Update(HttpRequestMessage request, ApplicationGroupViewModel applicaionGroupViewModel)
        {
            bool statusUpdate            = false;
            HttpResponseMessage response = null;

            if (!ModelState.IsValid)
            {
                response     = request.CreateResponse(HttpStatusCode.BadRequest, ModelState);
                statusUpdate = false;
            }
            else
            {
                ApplicationGroup appGroup = new ApplicationGroup();
                appGroup.UpdateApplicationGroup(applicaionGroupViewModel);
                _applicationGroupService.Update(appGroup);
                var listRoleGroup = new List <ApplicationRoleGroup>();
                foreach (var item in applicaionGroupViewModel.Roles)
                {
                    listRoleGroup.Add(new ApplicationRoleGroup()
                    {
                        GroupId = applicaionGroupViewModel.ID,
                        RoleId  = item.Id
                    });
                }
                var role = _appRoleService.AddRolesToGroup(listRoleGroup, appGroup.ID);
                _applicationGroupService.Save();
                statusUpdate = true;
                var listRole        = _appRoleService.GetListRoleByGroupId(applicaionGroupViewModel.ID);
                var listUserInGroup = _applicationGroupService.GetListUserByGroupId(applicaionGroupViewModel.ID);
                foreach (var user in listUserInGroup)
                {
                    var listRoleName = listRole.Select(x => x.Name).ToArray();
                    foreach (var roleName in listRoleName)
                    {
                        await _appUsermanager.RemoveFromRoleAsync(user.Id, roleName);

                        await _appUsermanager.AddToRoleAsync(user.Id, roleName);
                    }
                }
                response = request.CreateResponse(HttpStatusCode.Created, statusUpdate);
            }
            return(response);
        }
 public HttpResponseMessage Create(HttpRequestMessage request, ApplicationGroupViewModel applivationGroupViewModel)
 {
     return(CreateHttpResponse(request, () =>
     {
         HttpResponseMessage response = null;
         if (!ModelState.IsValid)
         {
             response = request.CreateResponse(HttpStatusCode.BadRequest, ModelState);
         }
         else
         {
             ApplicationGroup appGroup = new ApplicationGroup();
             appGroup.UpdateApplicationGroup(applivationGroupViewModel);
             var model = _applicationGroupService.Add(appGroup);
             _applicationGroupService.Save();
             var mapperModel = Mapper.Map <ApplicationGroup, ApplicationGroupViewModel>(model);
             var listRoleGroup = new List <ApplicationRoleGroup>();
             foreach (var item in applivationGroupViewModel.Roles)
             {
                 listRoleGroup.Add(new ApplicationRoleGroup()
                 {
                     GroupId = model.ID,
                     RoleId = item.Id
                 });
             }
             var role = _appRoleService.AddRolesToGroup(listRoleGroup, mapperModel.ID);
             _appRoleService.Save();
             var listRole = _appRoleService.GetListRoleByGroupId(applivationGroupViewModel.ID);
             var listUserInGroup = _applicationGroupService.GetListUserByGroupId(applivationGroupViewModel.ID);
             foreach (var user in listUserInGroup)
             {
                 var listRoleName = listRole.Select(x => x.Name).ToArray();
                 foreach (var roleName in listRoleName)
                 {
                     _appUsermanager.AddToRoleAsync(user.Id, roleName);
                 }
             }
             response = request.CreateResponse(HttpStatusCode.Created, mapperModel);
         }
         return response;
     }));
 }
        public HttpResponseMessage Create(HttpRequestMessage request, ApplicationGroupViewModel appGroupViewModel)
        {
            if (ModelState.IsValid)
            {
                var newAppGroup = new ApplicationGroup();
                newAppGroup.UpdateApplicationGroup(appGroupViewModel);
                try
                {
                    var appGroup = _appGroupService.Add(newAppGroup);
                    _appGroupService.Save();

                    //save group
                    var listRoleGroup = new List <ApplicationRoleGroup>();
                    foreach (var role in appGroupViewModel.Roles)
                    {
                        listRoleGroup.Add(new ApplicationRoleGroup()
                        {
                            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));
            }
        }