crud<T extends Model> method

void crud<T extends Model>({
  1. String prefix = "",
  2. required ModelCrudController<T> controller,
})

Implementation

void crud<T extends Model>({String prefix = "", required ModelCrudController<T> controller}) {
  String getModelPath() {
    final loweredHyphenate = T.toString().replaceAllMapped(RegExp(r'(?<=[a-z])(?=[A-Z])'), (match) => '-').toLowerCase();
    return loweredHyphenate.endsWith("/") ? loweredHyphenate : "${loweredHyphenate}s";
  }

  if (!prefix.endsWith('/')) prefix = '$prefix/';
  final path = getModelPath();

  group(
    prefix: "$prefix$path",
    routes: () {
      // index
      get("/", controller.index);

      // create
      get("/create", controller.create);

      // store
      post("/create", controller.store);

      // show
      get("/{id:int}", controller.show);

      // edit
      get("/{id:int}/edit", controller.edit);

      // update
      patch("/{id:int}/edit", controller.update);

      // destroy
      delete("/{id:int}", controller.destroy);
    },
  );
}