public async Task <IActionResult> OnPost() { // Check if there aren't any non-generic databases. if (!_context.Databases.Any(item => item.DatabaseType.Name != "Generic")) { // Display a message. TempData["StatusMessage"] = "Error: No non-generic databases could be found. Please create a database first."; // Redirect to the index page. return(RedirectToPage("/Administration/Databases/DatabaseEdgeFields/Index")); } // Check if the provided model isn't valid. if (!ModelState.IsValid) { // Add an error to the model. ModelState.AddModelError(string.Empty, "An error has been encountered. Please check again the input fields."); // Redisplay the page. return(Page()); } // Define a new task. var task = new DatabaseEdgeFieldsTask { Items = new List <DatabaseEdgeFieldInputModel> { new DatabaseEdgeFieldInputModel { Name = Input.Name, Description = Input.Description, Url = Input.Url, IsSearchable = Input.IsSearchable, Database = new DatabaseInputModel { Id = Input.DatabaseId } } } }; // Try to run the task. try { // Run the task. await task.CreateAsync(_serviceProvider, CancellationToken.None); } catch (Exception exception) { // Add an error to the model. ModelState.AddModelError(string.Empty, exception.Message); // Redisplay the page. return(Page()); } // Display a message. TempData["StatusMessage"] = "Success: 1 database edge field created successfully."; // Redirect to the index page. return(RedirectToPage("/Administration/Databases/DatabaseEdgeFields/Index")); }
public async Task <IActionResult> OnPost() { // Check if there isn't any ID provided. if (string.IsNullOrEmpty(Input.Id)) { // Display a message. TempData["StatusMessage"] = "Error: No ID has been provided."; // Redirect to the index page. return(RedirectToPage("/Administration/Databases/DatabaseEdgeFields/Index")); } // Define the query. var query = _context.DatabaseEdgeFields .Where(item => item.Id == Input.Id); // Define the view. View = new ViewModel { DatabaseEdgeField = query .Include(item => item.Database) .ThenInclude(item => item.DatabaseType) .FirstOrDefault() }; // Check if the item hasn't been found. if (View.DatabaseEdgeField == null) { // Display a message. TempData["StatusMessage"] = "Error: No item could be found with the provided ID."; // Redirect to the index page. return(RedirectToPage("/Administration/Databases/DatabaseEdgeFields/Index")); } // Check if the database edge field is the generic database edge field. if (View.DatabaseEdgeField.Database.DatabaseType.Name == "Generic") { // Display a message. TempData["StatusMessage"] = "Error: The generic database edge field can't be edited."; // Redirect to the index page. return(RedirectToPage("/Administration/Databases/DatabaseEdgeFields/Index")); } // Check if the provided model isn't valid. if (!ModelState.IsValid) { // Add an error to the model. ModelState.AddModelError(string.Empty, "An error has been encountered. Please check again the input fields."); // Redisplay the page. return(Page()); } // Define a new task. var task = new DatabaseEdgeFieldsTask { Items = new List <DatabaseEdgeFieldInputModel> { new DatabaseEdgeFieldInputModel { Id = Input.Id, Name = Input.Name, Description = Input.Description, Url = Input.Url, IsSearchable = Input.IsSearchable } } }; // Try to run the task. try { // Run the task. await task.EditAsync(_serviceProvider, CancellationToken.None); } catch (Exception exception) { // Add an error to the model. ModelState.AddModelError(string.Empty, exception.Message); // Redisplay the page. return(Page()); } // Display a message. TempData["StatusMessage"] = "Success: 1 database edge field updated successfully."; // Redirect to the index page. return(RedirectToPage("/Administration/Databases/DatabaseEdgeFields/Index")); }