public async Task <IActionResult> OnPost()
        {
            // Check if there aren't any non-generic database types.
            if (!_context.DatabaseTypes.Any(item => item.Name != "Generic"))
            {
                // Display a message.
                TempData["StatusMessage"] = "Error: No non-generic database types could be found. Please create a database type first.";
                // Redirect to the index page.
                return(RedirectToPage("/Administration/Databases/Databases/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 DatabasesTask
            {
                Items = new List <DatabaseInputModel>
                {
                    new DatabaseInputModel
                    {
                        Name         = Input.Name,
                        Description  = Input.Description,
                        Url          = Input.Url,
                        IsPublic     = Input.IsPublic,
                        DatabaseType = new DatabaseTypeInputModel
                        {
                            Id = Input.DatabaseTypeId
                        }
                    }
                }
            };

            // 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 created successfully.";
            // Redirect to the index page.
            return(RedirectToPage("/Administration/Databases/Databases/Index"));
        }
示例#2
0
        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/Databases/Index"));
            }
            // Define the query.
            var query = _context.Databases
                        .Where(item => item.Id == Input.Id);

            // Define the view.
            View = new ViewModel
            {
                Database = query
                           .Include(item => item.DatabaseType)
                           .FirstOrDefault()
            };
            // Check if the item hasn't been found.
            if (View.Database == 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/Databases/Index"));
            }
            // Check if the database is the generic database.
            if (View.Database.DatabaseType.Name == "Generic")
            {
                // Display a message.
                TempData["StatusMessage"] = "Error: The generic database can't be edited.";
                // Redirect to the index page.
                return(RedirectToPage("/Administration/Databases/Databases/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 DatabasesTask
            {
                Items = new List <DatabaseInputModel>
                {
                    new DatabaseInputModel
                    {
                        Id          = Input.Id,
                        Name        = Input.Name,
                        Description = Input.Description,
                        Url         = Input.Url,
                        IsPublic    = Input.IsPublic
                    }
                }
            };

            // 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 updated successfully.";
            // Redirect to the index page.
            return(RedirectToPage("/Administration/Databases/Databases/Index"));
        }