Exemplo n.º 1
0
        public async Task <IActionResult> AddRole(RoleToAddViewModel model, [FromHeader] string authorization)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            HttpParameters httpParameters =
                HttpParametersService
                .GetHttpParameters
                    (model,
                    ConfigHelper.AppSetting(Constants.ServerUrls,
                                            Constants.AddRole),
                    HttpMethod.Post,
                    string.Empty,
                    authorization
                    );

            AddRoleResponse addRoleResult = await _gWService.PostTo <AddRoleResponse>(httpParameters);

            if (addRoleResult.StatusCode == 400)
            {
                return(await ResponseService.GetResponse <BadRequestObjectResult, AddRoleResponse>(addRoleResult, ModelState));
            }
            else if (addRoleResult.StatusCode == 424)
            {
                return(await ResponseService.GetResponse <ConflictObjectResult, AddRoleResponse>(addRoleResult, ModelState));
            }
            else if (addRoleResult.StatusCode == 422)
            {
                return(await ResponseService.GetResponse <UnprocessableEntityObjectResult, AddRoleResponse>(addRoleResult, ModelState));
            }

            return(new OkObjectResult(addRoleResult));
        }
Exemplo n.º 2
0
        // POST api/accounts/addrole
        public async Task <IActionResult> AddRole([FromBody] RoleToAddViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            string role = model.RoleToAdd.Trim();

            if (await _accountsService.RoleExists(role))
            {
                return(new JsonResult(await Errors.GetGenericErrorResponse(
                                          new AddRoleResponse()
                {
                    Role = role,
                    Code = "role_exits",
                    Description = "Role already exists. Please try to add another role.",
                    Error = "Role exists.",
                    StatusCode = 424
                })));
            }

            IdentityResult roleResult = await _accountsService.CreateRole(role);

            if (!roleResult.Succeeded)
            {
                return(new JsonResult(await Errors.GetGenericErrorResponse(
                                          new AddRoleResponse()
                {
                    Id = "no_id",
                    Role = role,
                    Code = "faild_to_add_role",
                    Description = "Faild to add role to database.",
                    Error = "Faild to add role.",
                    StatusCode = 424
                })));
            }

            await _context.SaveChangesAsync();

            IdentityRole identityRole = await _accountsService.GetRoleId(role);

            return(new OkObjectResult(Wrappyfier.WrapAddRoleResponse(identityRole.Id, identityRole.Name)));
        }