コード例 #1
0
        public async Task <IActionResult> OnPostAsync()
        {
            // Check if there aren't any databases.
            if (!_context.Databases.Any())
            {
                // Display a message.
                TempData["StatusMessage"] = "Error: No databases could be found. Please create a database first.";
                // Redirect to the index page.
                return(RedirectToPage("/Administration/Permissions/DatabaseUserInvitations/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());
            }
            // Try to get the user with the provided e-mail.
            var user = _context.Users.FirstOrDefault(item => item.Email == Input.Email);

            // Check if there was any user found.
            if (user != null)
            {
                // Add an error to the model.
                ModelState.AddModelError(string.Empty, "A user with the provided e-mail already exists.");
                // Redisplay the page.
                return(Page());
            }
            // Get the database based on the provided string.
            var database = _context.Databases.FirstOrDefault(item => item.Id == Input.DatabaseString || item.Name == Input.DatabaseString);

            // Check if there was no database found.
            if (database == null)
            {
                // Add an error to the model.
                ModelState.AddModelError(string.Empty, "No database could be found with the given string.");
                // Redisplay the page.
                return(Page());
            }
            // Create a new database user.
            var databaseUserInvitation = new DatabaseUserInvitation
            {
                DatabaseId      = database.Id,
                Database        = database,
                Email           = Input.Email,
                DateTimeCreated = DateTime.Now
            };

            // Mark it for addition to the database.
            _context.DatabaseUserInvitations.Add(databaseUserInvitation);
            // Save the changes.
            await _context.SaveChangesAsync();

            // Display a message.
            TempData["StatusMessage"] = "Success: 1 database user invitation created successfully.";
            // Redirect to the index page.
            return(RedirectToPage("/Administration/Permissions/DatabaseUserInvitations/Index"));
        }
コード例 #2
0
        /// <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 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();
                var batchUserEmails = batchItems
                                      .Where(item => !string.IsNullOrEmpty(item.Email))
                                      .Select(item => item.Email)
                                      .Distinct();
                // Define the list of items to get.
                var databases = new List <Database>();
                var users     = new List <User>();
                // Create 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
                                .Where(item => batchDatabaseIds.Contains(item.Id))
                                .ToList();
                    users = context.Users
                            .Where(item => batchUserEmails.Contains(item.Email))
                            .ToList();
                }
                // Save the items to add.
                var databaseUserInvitationsToAdd = new List <DatabaseUserInvitation>();
                // Go over each item in the current batch.
                foreach (var batchItem in batchItems)
                {
                    // 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 there was no e-mail provided.
                    if (string.IsNullOrEmpty(batchItem.Email))
                    {
                        // Throw an exception.
                        throw new TaskException("There was no e-mail provided.", showExceptionItem, batchItem);
                    }
                    // Try to get the user.
                    var user = users
                               .FirstOrDefault(item => item.Email == batchItem.Email);
                    // Check if there was a user found.
                    if (user != null)
                    {
                        // Throw an exception.
                        throw new TaskException("The user with the provided e-mail already exists.", showExceptionItem, batchItem);
                    }
                    // Define the new item.
                    var databaseUserInvitation = new DatabaseUserInvitation
                    {
                        DateTimeCreated = DateTime.UtcNow,
                        DatabaseId      = database.Id,
                        Email           = batchItem.Email
                    };
                    // Add the item to the list.
                    databaseUserInvitationsToAdd.Add(databaseUserInvitation);
                }
                // Create the items.
                await IEnumerableExtensions.CreateAsync(databaseUserInvitationsToAdd, serviceProvider, token);
            }
        }