routeInsert method
WebRoute
routeInsert(
- String path, {
- required WebRequest rq,
- List<
String> methods = const [RequestMethods.POST], - Future<
ApiDoc> ? apiDoc()?, - WaAuthController? auth,
- List<
String> extraPath = const [], - List<
String> excludePaths = const [], - List<
String> hosts = const ['*'], - Map<
String, Object?> params = const {}, - List<
String> permissions = const [], - List<
int> ports = const [],
Creates a route for inserting new documents into the collection.
This method generates a REST API endpoint that handles document creation with automatic form validation. The route processes POST requests containing document data, validates it against the form definition, and inserts it into the collection.
Parameters:
path
- URL path for this routerq
- Web request instancemethods
- HTTP methods to accept (default: POST)apiDoc
- Optional API documentation generatorauth
- Optional authentication controllerextraPath
- Additional path segments to matchexcludePaths
- Path segments to exclude from matchinghosts
- Host names to match (default: all hosts)params
- Additional route parameterspermissions
- Required permissions for accessports
- Specific ports to match
Returns a WebRoute configured for creating documents.
Success response (201):
{
"success": true,
"message": "inserted",
"data": {"_id": "...", "name": "..."}
}
Error response (502):
{
"success": false,
"message": "error",
"form": {"field1": ["error message"]}
}
Implementation
WebRoute routeInsert(
String path, {
required WebRequest rq,
List<String> methods = const [RequestMethods.POST],
Future<ApiDoc>? Function()? apiDoc,
WaAuthController<dynamic>? auth,
List<String> extraPath = const [],
List<String> excludePaths = const [],
List<String> hosts = const ['*'],
Map<String, Object?> params = const {},
List<String> permissions = const [],
List<int> ports = const [],
}) {
Future<String> index() async {
var res = await insert(rq.getAll());
if (!res.success) {
return rq.renderData(
data: {
'form': res.toJson(),
'success': false,
'message': 'error',
},
status: 502,
);
}
return rq.renderData(data: {
'data': res.formValues(),
'success': true,
'message': 'inserted',
});
}
return WebRoute(
path: path,
methods: methods,
rq: rq,
apiDoc: apiDoc,
auth: auth,
excludePaths: excludePaths,
extraPath: extraPath,
hosts: hosts,
params: params,
permissions: permissions,
ports: ports,
index: index,
);
}