Ejemplo n.º 1
0
        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"));
        }