public async Task <IActionResult> Put([FromBody] EditRoleRequest request)
        {
            EditRoleResponse response = new EditRoleResponse();

            try
            {
                IdentityRole identityRole = await _roleManager.FindByIdAsync(request.Id);

                if (identityRole == null)
                {
                    throw new Exception("Role not found");
                }

                identityRole.Name = request.Name;
                IdentityResult result = await _roleManager.UpdateAsync(identityRole);

                if (result.Succeeded)
                {
                    response.Id        = identityRole.Id;
                    response.IsSuccess = true;
                }
                else
                {
                    response.Errors = result.Errors.Select(x => x.Description);
                }

                return(Ok(response));
            }
            catch (Exception ex)
            {
                _logger.LogError($"Post error: {ex.Message} - Name: {request.Name}");
                return(BadRequest());
            }
        }
示例#2
0
        public async Task <ActionResult <EditRoleResponse> > CreateRole(EditRoleRequest request)
        {
            var response     = new EditRoleResponse();
            var identityRole = new IdentityRole {
                Name = request.Name
            };
            var result = await _roleManager.CreateAsync(identityRole);

            if (result.Succeeded)
            {
                identityRole = await _roleManager.FindByNameAsync(identityRole.Name);

                response.Id        = identityRole.Id;
                response.IsSuccess = true;
            }
            else
            {
                response.Errors = result.Errors.Select(x => x.Description);
            }

            return(Ok(response));
        }
示例#3
0
        public async Task <IActionResult> UpdateRole(string id, EditRoleRequest request)
        {
            if (id != request.Id)
            {
                return(BadRequest());
            }

            var response     = new EditRoleResponse();
            var identityRole = await _roleManager.FindByIdAsync(request.Id);

            if (identityRole == null)
            {
                return(NotFound());
            }

            identityRole.Name = request.Name;

            try
            {
                var result = await _roleManager.UpdateAsync(identityRole);

                if (result.Succeeded)
                {
                    response.Id        = identityRole.Id;
                    response.IsSuccess = true;
                }
                else
                {
                    response.Errors = result.Errors.Select(x => x.Description);
                }

                return(Ok(response));
            }
            catch (DbUpdateConcurrencyException) when(!_roleManager.Roles.AsNoTracking().Any(r => r.Id == id))
            {
                return(NotFound());
            }
        }
示例#4
0
        public async Task <EditRoleResponse> EditRole(int roleID, Role newRole)
        {
            var response = new EditRoleResponse();

            try
            {
                var role = await _rolesContext.Roles.FirstAsync(x => x.Id == roleID);

                role.Name = newRole.Name;

                _rolesContext.Entry(role).State = EntityState.Modified;
                await _rolesContext.SaveChangesAsync();

                response.OperationSucceded = true;
                response.Message           = "Role editted successfully";
            }
            catch (Exception)
            {
                response.OperationSucceded = false;
            }

            return(response);
        }