/// <summary> /// Helper method called when user select a Rest action /// </summary> /// <param name="restAction"></param> private void SwitchToRestAction(RestAction restAction, string path) { ShowResponse(null); var requestPath = string.Empty; string restActionStr = restAction.ToString(); _viewModel[RestActionViewModel.SELECTED_REST_ACTION] = restActionStr; if (!String.IsNullOrWhiteSpace(path)) { var account = AccountManager.GetAccount(); requestPath = String.Format(path, ApiVersionStrings.VersionNumber, account.UserId); _viewModel[RestActionViewModel.REQUEST_PATH] = requestPath; } HashSet<string> names = RestActionViewHelper.GetNamesOfControlsToShow(restActionStr); foreach (TextBox tb in new[] { tbApiVersion, tbObjectType, tbObjectId, tbExternalIdField, tbExternalId, tbFieldList, tbFields, tbSoql, tbSosl, tbRequestPath, tbRequestBody, tbRequestMethod }) { tb.Visibility = names.Contains(tb.Name) ? Visibility.Visible : Visibility.Collapsed; tb.BorderThickness = new Thickness(0,0,0,0); } if (!String.IsNullOrWhiteSpace(path)) { tbRequestPath.Text = requestPath; } }
/// <summary> /// Registers the controllers. /// </summary> public static void RegisterControllers() { var rockContext = new RockContext(); var restControllerService = new RestControllerService( rockContext ); var existingControllers = restControllerService.Queryable( "Actions" ).ToList(); var discoveredControllers = new List<RestController>(); var config = GlobalConfiguration.Configuration; var explorer = config.Services.GetApiExplorer(); foreach(var apiDescription in explorer.ApiDescriptions) { var action = apiDescription.ActionDescriptor; var name = action.ControllerDescriptor.ControllerName; var controller = discoveredControllers.Where( c => c.Name == name ).FirstOrDefault(); if ( controller == null ) { controller = new RestController { Name = name, ClassName = action.ControllerDescriptor.ControllerType.FullName }; discoveredControllers.Add( controller ); } controller.Actions.Add( new RestAction { ApiId = apiDescription.ID, Method = apiDescription.HttpMethod.Method, Path = apiDescription.RelativePath } ); } var actionService = new RestActionService( rockContext ); foreach(var discoveredController in discoveredControllers) { var controller = restControllerService.Queryable( "Actions" ) .Where( c => c.Name == discoveredController.Name ).FirstOrDefault(); if ( controller == null ) { controller = new RestController { Name = discoveredController.Name }; restControllerService.Add( controller ); } controller.ClassName = discoveredController.ClassName; foreach(var discoveredAction in discoveredController.Actions) { var action = controller.Actions.Where( a => a.ApiId == discoveredAction.ApiId ).FirstOrDefault(); { if ( action == null ) { action = new RestAction { ApiId = discoveredAction.ApiId }; controller.Actions.Add( action ); } action.Method = discoveredAction.Method; action.Path = discoveredAction.Path; } } var actions = discoveredController.Actions.Select( d => d.ApiId).ToList(); foreach( var action in controller.Actions.Where( a => !actions.Contains(a.ApiId)).ToList()) { actionService.Delete( action ); controller.Actions.Remove(action); } } var controllers = discoveredControllers.Select( d => d.Name ).ToList(); foreach ( var controller in restControllerService.Queryable().Where( c => !controllers.Contains( c.Name ) ).ToList() ) { restControllerService.Delete( controller ); } rockContext.SaveChanges(); }