routeUpdate method
WebRoute
routeUpdate(
- 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 updating existing documents in the collection.
This method generates a REST API endpoint that handles document updates via complete replacement. The route expects a document ID in the URL path and processes POST requests containing the new document data.
The update operation:
- Validates the new data against the form definition
- Replaces the entire document (not a partial update)
- Triggers the onUpdate event if successful
Parameters:
path
- URL path for this route (should include {id} placeholder)rq
- 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 updating documents.
Success response (200):
{
"success": true,
"message": "updated",
"data": {"_id": "...", "name": "..."}
}
Error responses:
- 404: Document not found
- 502: Validation errors or missing ID
Implementation
WebRoute routeUpdate(
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 id = rq.getParam('id', def: '').toString();
if (id.isEmpty) {
return rq.renderData(
data: {
'success': false,
'message': 'id is required',
},
status: 502,
);
}
var res = await replaceOne(id, rq.getAll());
if (res == null) {
return rq.renderData(
data: {
'success': false,
'message': 'id not found',
},
status: 404,
);
}
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': 'updated',
});
}
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,
);
}