handlePost method Null safety
- Request request
Handles Post http requisitions. Requires a http Request (from the Shelf package).
Implementation
Future<Response> handlePost(Request request) async {
if (!middlewareJwt(request)) {
return Response.forbidden(jsonEncode({'error': 'middlewareJwt'}));
}
try {
final content = await request.readAsString(); /*2*/
final data = jsonDecode(content) as Map;
final key = request.url.pathSegments[0];
final dynamic seg = await config.db.getAll(request.url.pathSegments[0]);
if (seg == null) {
return Response.notFound(jsonEncode({'error': 'Not found'}));
} else {
data['id'] = uuid.v1();
seg.add(data);
await config.db.save(key, seg);
return Response.ok(
jsonEncode(data),
headers: {'content-type': 'application/json'},
);
}
} catch (e) {
return Response.notFound(jsonEncode({'error': 'Internal Error. $e'}));
}
}