Ejemplo n.º 1
0
        public async Task <IActionResult> OnPost()
        {
            // 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 RolesTask
            {
                Items = new List <RoleInputModel>
                {
                    new RoleInputModel
                    {
                        Name = Input.Name
                    }
                }
            };

            // 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 role created successfully.";
            // Redirect to the index page.
            return(RedirectToPage("/Administration/Accounts/Roles/Index"));
        }
Ejemplo n.º 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/Accounts/Roles/Index"));
            }
            // Define the query.
            var query = _context.Roles
                        .Where(item => item.Id == Input.Id);

            // Define the view.
            View = new ViewModel
            {
                Role = query
                       .FirstOrDefault()
            };
            // Check if the item hasn't been found.
            if (View.Role == null)
            {
                // Display a message.
                TempData["StatusMessage"] = "Error: No item could be found with the provided ID.";
                // Redirect to the index page.
                return(RedirectToPage("/Administration/Accounts/Roles/Index"));
            }
            // Check if the role is the administrator role.
            if (View.Role.Name == "Administrator")
            {
                // Display a message.
                TempData["StatusMessage"] = "Error: The \"Administrator\" role can't be edited.";
                // Redirect to the index page.
                return(RedirectToPage("/Administration/Accounts/Roles/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 RolesTask
            {
                Items = new List <RoleInputModel>
                {
                    new RoleInputModel
                    {
                        Id   = Input.Id,
                        Name = Input.Name
                    }
                }
            };

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