SkillAgentSearch skills...

JCTools.GenericCrud

Simplification of the Create, Read, Update and Delete web pages of the application models.

Install / Use

/learn @jeancarlo13/JCTools.GenericCrud

README

JCTools.GenericCrud

Simplification of the Create, Read, Update and Delete web pages of the application models.

Content

Overview

All application required multiple pages for edited the base models. This pages generally are equals to each other.

This package allows reduce this task at minimum of actions.

You only require create and configure your models, and this package create the necessary controllers, views and actions for the Create, Read, Update and Delete actions.

Status

v2.2.2

Requirements

.net core 2.1, .net core 3.1 or .net 5.0

bootstrap 3.3.7 or bootstrap 4.3.1

font awesome 5.0.6

Since version 2.2.1, the icons in the font awesome are replaced by the configured text labels if the font is not loaded.

Usage

Basic usage

  1. Add the package to your application
    Install-Package JCTools.GenericCrud -Version 2.2.2
    
    Or
    dotnet add package JCTools.GenericCrud --version 2.2.2
    
  2. Add the next lines in the method ConfigureServices of your Startup class
        services.AddGenericCrud<MyContext>(options =>
        {
            // add the models type to manage with the package
            options.Models.Add<Models.Country>(); 
            options.Models.Add<Models.Genre>(nameof(Models.Genre.Name));
            // ...
            
            // options is an instance from JCTools.GenericCrud.Settings.IOptions
            // use this interface to custom the generated CRUDs globally
            // eg;
    
            // Indicate if desired use Modals
            options.UseModals = true;
            // Set the bootstrap version to be used (default v4.3.1)
            options.BootstrapVersion = Settings.Bootstrap.Version3;
            
            // add a model with a custom controller
            options.Models.Add<Models.Movie, int, MovieController, Data.Context>();            
        });
    

    In the version 2.0.0 the method ConfigureGenericCrud was renamed to AddGenericCrud.

  3. Run to app and access at the url http://localhost:5000/[ModelName], sample: http://localhost:5000/Country. In the browser you should see a similar page to : Sample index page Sample index page

    Your app's layout page may make it look different from the images above.

Demo apps

The current repository include 3 demo apps for showing the described features of the package:

Other features

Custom controllers

If your desired personalize your controllers, add additional actions or override the default actions, then

  1. Create a new controller the inherits from JCTools.GenericCrud.Controllers.GenericController. e.g;

    using System;
    using System.Linq;
    using JCTools.GenericCrud.Controllers;
    using JCTools.GenericCrud.Services;
    using JCTools.GenericCrud.DataAnnotations;
    using Microsoft.AspNetCore.Mvc.Filters;
    using Microsoft.Extensions.Localization;
    using Microsoft.Extensions.Logging;
    
    namespace MyApp.Controllers
    {
        [CrudConstraint(typeof(Models.MyModel))]
        public class MyController : GenericController
        {
            public MyController(
                IServiceProvider serviceProvider,
                IViewRenderService renderingService,
                IStringLocalizerFactory localizerFactory,
                ILoggerFactory loggerFactory
            )
                : base(serviceProvider, renderingService, localizerFactory, loggerFactory, "Id")
            {
                // add your custom logic here
            }
        }
    }
    
  2. Add the related model in the method ConfigureServices of your Startup class using specifying the custom controller, eg;

        options.Models.Add<Models.Movie, int, MovieController, Data.Context>();
    
  3. (optional) If you override the OnActionExecuting(ActionExecutingContext filterContext) or OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) controller methods, make sure to invoke the base methods for the correct initializations of the controller settings

        // ...
        
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            // Call the initialization of the Settings property
            base.InitSettings(context);
            
            // Add your custom settings here, eg;
            Settings.UseModals = false; // disabled the modals
            Settings.Subtitle = "All entities"; // change the default subtitle
            
            // Customizing the Icons and Buttons Classes of the Index Page
            var index = Settings as IIndexModel;
            index.NewAction.IconClass = "fa fa-plus-circle";
            index.NewAction.ButtonClass = "btn btn-success btn-sm";
    
            index.DetailsAction.IconClass = "fa fa-info";
            index.DetailsAction.ButtonClass = "btn btn-info btn-sm";
            
            index.EditAction.IconClass = "fa fa-edit";
            index.EditAction.ButtonClass = "btn btn-warning btn-sm";
            
            index.DeleteAction.IconClass = "fa fa-eraser";
    
            // other things
            ViewBag.Countries = (DbContext as Context).Countries.ToList();
    
            base.OnActionExecuting(context);
        }
        
        // Or
        public override Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            // Call the initialization of the Settings property
            base.InitSettings(context);
            // Add your custom settings here, eg;
            Settings.UseModals = false;
            Settings.Subtitle = "All entities";
            ViewBag.OtherEntities = (DbContext as Data.Context).OtherEntities.ToList();
    
            return base.OnActionExecutionAsync(context, next);
        }
        //...
    
  4. Run to app and access at the url http://localhost:5000/[model name], e.g. http://localhost:5000/movie

You can see a sample custom controller in the demo apps called MovieController:

Entity model property settings

Version 2.0.0 includes the ability to customize property settings in an entity model. For this propose using the data annotation CrudAttribute in the namespace JCTools.GenericCrud.DataAnnotations.

This data annotation have third properties:

  • Visible: Boolean indicating that the property is or not visible in CRUD views.

  • UseCustomView: Boolean indicating that the property has custom views for rendering in details, create, delete, and edit actions.

    Two custom views are required per property, one for read-only views (Details and Delete actions) and one for editable views (Create and Edit actions).

    • Readonly views are named _Details<Property name>.cshtml.
    • Editable views are named _Edit<Property name>.cshtml.
      eg; if the property is named Status, the CRUD expects to find two views named _DetailsStatus.cshtml and _EditStatus.cshtml.
  • IsEditableKey: Boolean that indicates whether the entity property is an Id/Key and whether or not it is editable by the user.

    When an Id / Key property is editable, editing the entity is actually a deletion of the stored entity followed by the creation of a new entity using the new values.

Authorization

JCTool.GenericCrud includes since version 2.1.0 the possibility of managing access to CRUD controllers using an authorization policy.

The name of the default policy is JCTools.GenericCrud.CrudPolicy, and by default authorization is not activated and anonymous access is allowed.

To turn on authorization validation, just add a call to UseAuthorization in your Startup.ConfigureServices method. e.g;

    services.AddGenericCrud<MyContext>(options =>
    {
        // ...
        o.UseAuthorization(f => f.RequireAuthenticatedUser()); // add this line
    });

Note: If no action is specified for policy validation, by default only one authenticated user

Related Skills

View on GitHub
GitHub Stars6
CategoryDevelopment
Updated3y ago
Forks1

Languages

C#

Security Score

75/100

Audited on Jan 20, 2023

No findings