-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from javad-zobeidi/dev
0.3.0
- Loading branch information
Showing
13 changed files
with
382 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,51 @@ | ||
import 'dart:convert'; | ||
import 'dart:io'; | ||
import 'package:vania/src/exception/invalid_argument_exception.dart'; | ||
import 'package:vania/src/exception/validation_exception.dart'; | ||
import 'package:vania/src/route/route_data.dart'; | ||
import 'package:vania/vania.dart'; | ||
|
||
class ControllerHandler { | ||
final RouteData route; | ||
final Request request; | ||
ControllerHandler({required this.route, required this.request}) { | ||
handler(); | ||
} | ||
|
||
void handler() async { | ||
void create({ | ||
required RouteData route, | ||
required Request request, | ||
}) async { | ||
List<dynamic> positionalArguments = []; | ||
Function? function; | ||
if (route.action is Function) { | ||
function = route.action; | ||
} | ||
|
||
if (function == null) { | ||
request.response.statusCode = HttpStatus.internalServerError; | ||
if (request.request.headers.contentType.toString() == | ||
"application/json") { | ||
request.response.headers.contentType = ContentType.json; | ||
|
||
request.response.write(jsonEncode({'message': 'Method not found'})); | ||
} else { | ||
request.response.headers.contentType = ContentType.html; | ||
request.response.write('<b>Method not found</b>'); | ||
} | ||
request.response.close(); | ||
return; | ||
} | ||
|
||
Map<String, dynamic> params = {}; | ||
|
||
params.addAll(route.params ?? {}); | ||
|
||
var argsList = extractFunctionArgs(function); | ||
|
||
var requestArgIndex = argsList.indexOf("Request"); | ||
|
||
if (requestArgIndex > -1) { | ||
argsList.removeAt(requestArgIndex); | ||
} | ||
|
||
if (argsList.isNotEmpty) { | ||
positionalArguments = params.values | ||
.map((item) => int.tryParse(item.toString()) ?? item) | ||
if (route.params != null) { | ||
positionalArguments = route.params!.values | ||
.map((param) => int.tryParse(param) ?? param) | ||
.toList(); | ||
} | ||
|
||
if (requestArgIndex > -1) { | ||
positionalArguments.insert(requestArgIndex, request); | ||
if (route.hasRequest) { | ||
positionalArguments.insert(0, request); | ||
} | ||
|
||
try { | ||
Response data = await Function.apply(function, positionalArguments, {}); | ||
data.makeResponse(request.response); | ||
} on BaseHttpResponseException catch (e) { | ||
e.call().makeResponse(request.response); | ||
} on InvalidArgumentException catch (_) { | ||
rethrow; | ||
} | ||
} | ||
|
||
List extractFunctionArgs(Function function) { | ||
String functionString = function.toString(); | ||
String paramsString = functionString.split(RegExp(r'\(|\)'))[1].trim(); | ||
|
||
RegExp regex = RegExp(r'(.+?)\s*\{(.+?)\}'); | ||
Match? match = | ||
regex.firstMatch(paramsString.replaceAll('[', '').replaceAll(']', '')); | ||
|
||
if (match == null) { | ||
return removeDynamicPart(paramsString) | ||
.replaceAll('[', '') | ||
.replaceAll(']', '') | ||
.split(','); | ||
} else { | ||
return removeDynamicPart(match.group(1).toString()).split(','); | ||
Response response = await Function.apply( | ||
route.action, | ||
positionalArguments, | ||
{}, | ||
); | ||
|
||
response.makeResponse(request.response); | ||
} on ValidationException catch (error) { | ||
error | ||
.response(request.headers['accept'] == "application/json") | ||
.makeResponse(request.response); | ||
} catch (error) { | ||
_response(request, error.toString()); | ||
} | ||
} | ||
} | ||
|
||
String removeDynamicPart(String input) { | ||
RegExp regExp = RegExp(r'\{[^\}]*\}'); | ||
return input.replaceAll(regExp, ''); | ||
void _response(Request req, message, [statusCode = 400]) { | ||
if (req.headers['accept'] == "application/json") { | ||
Response.json( | ||
{ | ||
"message": message, | ||
}, | ||
statusCode, | ||
).makeResponse(req.response); | ||
} else { | ||
Response.html(message).makeResponse(req.response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.