/// <summary> /// Gets the updated navigation title card for the database node field details page. /// </summary> /// <param name="databaseNodeField">Represents the current database node field.</param> /// <returns>The navigation title card for the database node field details page.</returns> public static NavigationTitleCardViewModel GetDatabaseNodeFieldNavigationTitleCard(DatabaseNodeField databaseNodeField) { // Get the corresponding navigation title card. var navigationTitleCard = DatabaseNodeFieldNavigationTitleCard; // Update the title and subtitle. navigationTitleCard.Title = databaseNodeField.Name; navigationTitleCard.Subtitle = databaseNodeField.Id; // Return the navigation title card. return(navigationTitleCard); }
public async Task <IActionResult> OnPostAsync() { // Check if there aren't any databases of non-generic type. if (!_context.Databases.Where(item => item.DatabaseType.Name != "Generic").Any()) { // Display a message. TempData["StatusMessage"] = "Error: No databases of non-generic type could be found. Please create a database first."; // Redirect to the index page. return(RedirectToPage("/Administration/Databases/DatabaseNodeFields/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()); } // Check if there is another database node field with the same name. if (_context.DatabaseNodeFields.Any(item => item.Name == Input.Name)) { // Add an error to the model ModelState.AddModelError(string.Empty, $"A database node field with the name \"{Input.Name}\" already exists."); // Redisplay the page. return(Page()); } // Get the corresponding database. var database = _context.Databases .Where(item => item.DatabaseType.Name != "Generic") .FirstOrDefault(item => item.Id == Input.DatabaseString || item.Name == Input.DatabaseString); // Check if no database has been found. if (database == null) { // Add an error to the model ModelState.AddModelError(string.Empty, "No non-generic database could be found with the provided ID."); // Redisplay the page. return(Page()); } // Define the new database node field. var databaseNodeField = new DatabaseNodeField { Name = Input.Name, Description = Input.Description, Url = Input.Url, IsSearchable = Input.IsSearchable, DatabaseId = database.Id, Database = database, DateTimeCreated = DateTime.Now }; // Mark it for addition. _context.DatabaseNodeFields.Add(databaseNodeField); // Save the changes to the database. await _context.SaveChangesAsync(); // Display a message. TempData["StatusMessage"] = "Success: 1 database node field created successfully."; // Redirect to the index page. return(RedirectToPage("/Administration/Databases/DatabaseNodeFields/Index")); }
/// <summary> /// Creates the items in the database. /// </summary> /// <param name="serviceProvider">The application service provider.</param> /// <param name="token">The cancellation token for the task.</param> public async Task CreateAsync(IServiceProvider serviceProvider, CancellationToken token) { // Check if there weren't any valid items found. if (Items == null) { // Throw an exception. throw new TaskException("No valid items could be found with the provided data."); } // Check if the exception item should be shown. var showExceptionItem = Items.Count() > 1; // Get the total number of batches. var count = Math.Ceiling((double)Items.Count() / ApplicationDbContext.BatchSize); // Go over each batch. for (var index = 0; index < count; index++) { // Check if the cancellation was requested. if (token.IsCancellationRequested) { // Break. break; } // Get the items in the current batch. var batchItems = Items .Skip(index * ApplicationDbContext.BatchSize) .Take(ApplicationDbContext.BatchSize); // Get the IDs of the items in the current batch. var batchIds = batchItems .Where(item => !string.IsNullOrEmpty(item.Id)) .Select(item => item.Id); var batchNames = batchItems .Where(item => !string.IsNullOrEmpty(item.Name)) .Select(item => item.Name) .Distinct(); // Check if any of the IDs are repeating in the list. if (batchIds.Distinct().Count() != batchIds.Count()) { // Throw an exception. throw new TaskException("Two or more of the manually provided IDs are duplicated."); } // Get the IDs of the related entities that appear in the current batch. var batchDatabaseIds = batchItems .Where(item => item.Database != null) .Select(item => item.Database) .Where(item => !string.IsNullOrEmpty(item.Id)) .Select(item => item.Id) .Distinct(); // Define the list of items to get. var databases = new List <Database>(); var existingDatabaseNodeFieldNames = new List <string>(); var validBatchIds = new List <string>(); // Use a new scope. using (var scope = serviceProvider.CreateScope()) { // Use a new context instance. using var context = scope.ServiceProvider.GetRequiredService <ApplicationDbContext>(); // Get the related entities that appear in the current batch. databases = context.Databases .Include(item => item.DatabaseType) .Where(item => batchDatabaseIds.Contains(item.Id)) .ToList(); existingDatabaseNodeFieldNames = context.DatabaseNodeFields .Where(item => batchNames.Contains(item.Name)) .Select(item => item.Name) .ToList(); // Get the valid IDs, that do not appear in the database. validBatchIds = batchIds .Except(context.DatabaseNodeFields .Where(item => batchIds.Contains(item.Id)) .Select(item => item.Id)) .ToList(); } // Save the items to add. var databaseNodeFieldsToAdd = new List <DatabaseNodeField>(); // Go over each item in the current batch. foreach (var batchItem in batchItems) { // Check if the ID of the item is not valid. if (!string.IsNullOrEmpty(batchItem.Id) && !validBatchIds.Contains(batchItem.Id)) { // Continue. continue; } // Check if there is another database node field with the same name. if (existingDatabaseNodeFieldNames.Any(item => item == batchItem.Name) || databaseNodeFieldsToAdd.Any(item => item.Name == batchItem.Name)) { // Throw an exception. throw new TaskException("A database node field with the same name already exists.", showExceptionItem, batchItem); } // Check if there was no database provided. if (batchItem.Database == null || string.IsNullOrEmpty(batchItem.Database.Id)) { // Throw an exception. throw new TaskException("There was no database provided.", showExceptionItem, batchItem); } // Get the database. var database = databases .FirstOrDefault(item => item.Id == batchItem.Database.Id); // Check if there was no database found. if (database == null) { // Throw an exception. throw new TaskException("There was no database found.", showExceptionItem, batchItem); } // Check if the database is generic. if (database.DatabaseType.Name == "Generic") { // Throw an exception. throw new TaskException("The database node field can't be generic.", showExceptionItem, batchItem); } // Define the new item. var databaseNodeField = new DatabaseNodeField { DateTimeCreated = DateTime.UtcNow, Name = batchItem.Name, Description = batchItem.Description, Url = batchItem.Url, IsSearchable = batchItem.IsSearchable, DatabaseId = database.Id }; // Check if there is any ID provided. if (!string.IsNullOrEmpty(batchItem.Id)) { // Assign it to the item. database.Id = batchItem.Id; } // Add the item to the list. databaseNodeFieldsToAdd.Add(databaseNodeField); } // Create the items. await IEnumerableExtensions.CreateAsync(databaseNodeFieldsToAdd, serviceProvider, token); } }