Esempio n. 1
0
 public HttpResponseMessage Add(HttpRequestMessage request, ApplicationRoleViewModel appRoleVm)
 {
     return(CreateHttpResponse(request, () =>
     {
         if (ModelState.IsValid)
         {
             HttpResponseMessage response = null;
             var newRole = new AppRole();
             newRole.UpdateRole(appRoleVm);
             newRole.Id = Guid.NewGuid().ToString();//sinh ngẫu nhiên id
             _appRoleService.Add(newRole);
             _appRoleService.Save();
             var modelVm = Mapper.Map <AppRole, ApplicationRoleViewModel>(newRole);
             response = request.CreateResponse(HttpStatusCode.OK, modelVm);
             return response;
         }
         else
         {
             return request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
         }
     }));
 }
Esempio n. 2
0
 public HttpResponseMessage Create(HttpRequestMessage request, AppRoleViewModel appRoleViewModel)
 {
     if (ModelState.IsValid)
     {
         var newAppRole = new AppRole();
         newAppRole.UpdateAppRole(appRoleViewModel);
         try
         {
             _appRoleService.Add(newAppRole);
             _appRoleService.Save();
             return(request.CreateResponse(HttpStatusCode.OK, appRoleViewModel));
         }
         catch (NameDuplicatedException dex)
         {
             return(request.CreateErrorResponse(HttpStatusCode.BadRequest, dex.Message));
         }
     }
     else
     {
         return(request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
     }
 }
Esempio n. 3
0
        public HttpResponseMessage Create(HttpRequestMessage request, AppGroupViewModel appGroupViewModel)
        {
            if (ModelState.IsValid)
            {
                var newAppGroup = new AppGroup();
                newAppGroup.Name = appGroupViewModel.Name;
                try
                {
                    var appGroup = _appGroupService.Add(newAppGroup);

                    //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));
            }
        }