Skip to content

matteoventuri7/MinimalApiCrud

Repository files navigation

MinimalApiCrud

Nuget package

Nuget

You can use MinimalApiCrud installing the Nuget package from here https://www.nuget.org/packages/MinimalApiCrud

What is it?

It's a very simple library that permit to create CRUD endpoints for your entities.
It works with .NET Minimal API and .NET 6 and the implementation is covered by unit tests at 100%

Keep in mind that this library try to offer a base set of CRUD endpoints. If you need to customize something you have to write your endpoint.

Important change log

1.2.1 -> 1.3.0 Library upgrade

1.2.0 -> 1.2.1
Now "Filter" method had pagination!

1.1.1 -> 1.2.0
Methods "GetAll" and "Filter" now will return an object with the total count and the items instead of only the items.

Methods

  • MapCrud: initialize the library with the data model, the id data type and the data context.
  • SetupMapping: configure the mapping between data model and view model. (for details see https://github.com/MapsterMapper/Mapster)
  • GetAll: setup the Read endpoint. It's paginated.
  • GetOneById: setup the single Read by id endpoint.
  • Filter: setup the filters and the comparing logics to retrieve the results. (for details see https://dynamic-linq.net/advanced-parse-lambda#dynamic-lambda))
  • Insert: setup the Create endpoint.
  • Update: setup the Update endpoint.
  • Delete: setup the Delete enpoint.

Example

In this example you can see how configurate endpoints and setup mappings.

For more details, please check the example here https://github.com/matteoventuri7/MinimalApiCrud/tree/master/MinimalApi

Program.cs

using var minimalApiCrudBuilder =
    app.MapCrud<WeatherForecast, int, WeatherForecastContext>()
    .SetupMapping<WeatherForecast, WeatherForecastViewModel>(x =>
        x.Map(dest => dest.DisplayDate, src => src.Date.ToLongDateString())
        .Map(dest => dest.Temperature, src => src.TemperatureC + "°C"))
    .GetAll<WeatherForecastViewModel>(config: x => x.WithName("GetWeatherForecast"))
    .GetOneById<WeatherForecastViewModel>()
    .Filter<WeatherForecastViewModel>(new Dictionary<string, string> {
        {nameof(WeatherForecast.TemperatureC),$"{nameof(WeatherForecast.TemperatureC)} == @0" }, 
        {nameof(WeatherForecast.Summary), $"{nameof(WeatherForecast.Summary)} == @0" } }
        , FilterLogic.OR)
    .Insert<WeatherForecastDto>()
    .Update<WeatherForecastViewModel>()
    .Delete("/weatherforecast/{id:int}");

About

Library to create automatically simple crud enpoints.

Topics

Resources

License

Stars

Watchers

Forks

Contributors 3

  •  
  •  
  •  

Languages