Skip to content

Commit

Permalink
Merge pull request #123 from javad-zobeidi/dev
Browse files Browse the repository at this point in the history
Add list item submission support, upgrade dependencies, and fix API responses
  • Loading branch information
javad-zobeidi authored Sep 20, 2024
2 parents 66f4b6e + ae8ae23 commit 81b6ce5
Show file tree
Hide file tree
Showing 41 changed files with 88 additions and 66 deletions.
11 changes: 8 additions & 3 deletions lib/src/database/postgresql_driver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,25 @@ class PostgreSQLDriver implements DatabaseDriver {
Future<DatabaseDriver?> init() async {
try {
var manager = Manager();
manager.addConnection({
Map<String, dynamic> config = {
'driver': 'pgsql',
'host': env<String>('DB_HOST', '127.0.0.1'),
'port': env<int>('DB_PORT', 5432),
'database': env<String>('DB_DATABASE', 'vania'),
'username': env<String>('DB_USERNAME', 'root'),
'password': env<String>('DB_PASSWORD', ''),
//'sslmode': env<bool>('DB_SSL_MODE',true) == true ? 'require' : '',
'pool': env<bool>('DB_POOL', false),
'poolsize': env<int>('DB_POOL_SIZE', 0),
'charset': env<String>('DB_CHARSET', 'utf8'),
'prefix': env<String>('DB_PREFIX', ''),
'schema': env<String>('DB_SCHEMA', 'public'),
});
};

if (env<bool>('DB_SSL_MODE', false) == true) {
config['sslmode'] = 'require';
}

manager.addConnection(config);
manager.setAsGlobal();
_connection = await manager.connection();
return this;
Expand Down
4 changes: 2 additions & 2 deletions lib/src/exception/base_http_exception.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ class BaseHttpResponseException {
});

Response response(bool isJson) => isJson
? Response.json({'message': message}, code)
: Response.html(message);
? Response.html(message)
: Response.json(message is Map ? message : {'message': message}, code);
}
10 changes: 6 additions & 4 deletions lib/src/http/controller/controller_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ class ControllerHandler {

response.makeResponse(request.response);
} on ValidationException catch (error) {
print(request.headers['accept']);
print(request.headers['accept'].contains('html'));
error
.response(request.headers['accept'] == "application/json")
.response(request.headers['accept'].toString().contains('html'))
.makeResponse(request.response);
} catch (error) {
_response(request, error.toString());
Expand All @@ -38,14 +40,14 @@ class ControllerHandler {
}

void _response(Request req, message, [statusCode = 400]) {
if (req.headers['accept'] == "application/json") {
if (req.headers['accept'].toString().contains('html')) {
Response.html(message).makeResponse(req.response);
} else {
Response.json(
{
"message": message,
},
statusCode,
).makeResponse(req.response);
} else {
Response.html(message).makeResponse(req.response);
}
}
22 changes: 18 additions & 4 deletions lib/src/http/request/request_form_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,37 @@ class RequestFormData {
if (inputName != null) {
if (data['filename'] == null || data['filename']!.isEmpty) {
var value = utf8.decode(await formItem.first);
inputs[inputName] =
int.tryParse(value.toString()) ?? value.toString();
if (inputName.contains('[]')) {
String clearedInputName = inputName.replaceAll('[]', '');
if (inputs.containsKey(clearedInputName)) {
if (inputs[clearedInputName] is List) {
inputs[clearedInputName]
.add(int.tryParse(value.toString()) ?? value.toString());
}
} else {
List valueList = [];
valueList.add(int.tryParse(value.toString()) ?? value.toString());
inputs[clearedInputName] = valueList;
}
} else {
inputs[inputName] =
int.tryParse(value.toString()) ?? value.toString();
}
} else {
RequestFile file = RequestFile(
filename: data['filename'].toString(),
filetype: contentType.toString(),
stream: formItem,
);
if (inputName.contains('[]')) {
List<RequestFile> files = [];
files.add(file);
String clearedInputName = inputName.replaceAll('[]', '');
if (inputs.containsKey(clearedInputName)) {
if (inputs[clearedInputName] is List<RequestFile>) {
inputs[clearedInputName].add(file);
}
} else {
List<RequestFile> files = [];
files.add(file);
inputs[clearedInputName] = files;
}
} else {
Expand Down
8 changes: 4 additions & 4 deletions lib/src/http/request/request_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Future httpRequestHandler(HttpRequest req) async {
} on BaseHttpResponseException catch (error) {
error
.response(
req.headers.value('accept') == "application/json",
req.headers.value('accept').toString().contains('html'),
)
.makeResponse(req.response);
} on InvalidArgumentException catch (e) {
Expand All @@ -49,14 +49,14 @@ Future httpRequestHandler(HttpRequest req) async {
}

void _response(req, message) {
if (req.headers.value('accept') == "application/json") {
if (req.headers.value('accept').toString().contains('html')) {
Response.html(message).makeResponse(req.response);
} else {
Response.json(
{
"message": message,
},
400,
).makeResponse(req.response);
} else {
Response.html(message).makeResponse(req.response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Between extends ValidationRule {
Between(
{required this.lowerBoundary,
required this.higherBoundary,
super.customErrorMessage});
super.message});

@override
bool validate(value, data) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:vania/src/http/validation/validation_chain/validation_rule.dart';

class Confirmed extends ValidationRule {
Confirmed({super.customErrorMessage});
Confirmed({super.message});

@override
bool validate(value, data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:vania/src/http/validation/validation_chain/validation_rule.dart'

class EndWith extends ValidationRule {
final String end;
EndWith({required this.end, super.customErrorMessage});
EndWith({required this.end, super.message});

@override
bool validate(value, data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:vania/src/http/validation/validation_chain/validation_rule.dart'

class GreaterThan extends ValidationRule {
final num compare;
GreaterThan({required this.compare, super.customErrorMessage});
GreaterThan({required this.compare, super.message});
@override
bool validate(value, data) {
value = num.parse(value.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:vania/src/http/validation/validation_chain/validation_rule.dart'

class InArray<T> extends ValidationRule {
final List<T> array;
InArray({required this.array, super.customErrorMessage});
InArray({required this.array, super.message});

@override
bool validate(value, data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:vania/src/config/defined_regexp.dart';
import 'package:vania/src/http/validation/validation_chain/validation_rule.dart';

class IsAlpha extends ValidationRule {
IsAlpha({super.customErrorMessage});
IsAlpha({super.message});

@override
bool validate(value, data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:vania/src/config/defined_regexp.dart';
import 'package:vania/src/http/validation/validation_chain/validation_rule.dart';

class IsAlphaDash extends ValidationRule {
IsAlphaDash({super.customErrorMessage});
IsAlphaDash({super.message});

@override
bool validate(value, data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:vania/src/config/defined_regexp.dart';
import 'package:vania/src/http/validation/validation_chain/validation_rule.dart';

class IsAlphaNumeric extends ValidationRule {
IsAlphaNumeric({super.customErrorMessage});
IsAlphaNumeric({super.message});

@override
bool validate(value, data) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:vania/src/http/validation/validation_chain/validation_rule.dart';

class IsArray extends ValidationRule {
IsArray({super.customErrorMessage});
IsArray({super.message});

@override
bool validate(value, data) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:vania/src/http/validation/validation_chain/validation_rule.dart';

class IsBoolean extends ValidationRule {
IsBoolean({super.customErrorMessage});
IsBoolean({super.message});

@override
bool validate(value, data) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:vania/src/http/validation/validation_chain/validation_rule.dart';

class IsDate extends ValidationRule {
IsDate({super.customErrorMessage});
IsDate({super.message});

@override
bool validate(value, data) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:vania/src/http/validation/validation_chain/validation_rule.dart';

class IsDouble extends ValidationRule {
IsDouble({super.customErrorMessage});
IsDouble({super.message});

@override
bool validate(value, data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:vania/src/config/defined_regexp.dart';
import 'package:vania/src/http/validation/validation_chain/validation_rule.dart';

class IsEmail extends ValidationRule {
IsEmail({super.customErrorMessage});
IsEmail({super.message});

@override
bool validate(dynamic value, data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:vania/vania.dart';

class IsFile extends ValidationRule {
final String args;
IsFile({required this.args, super.customErrorMessage});
IsFile({required this.args, super.message});

@override
bool validate(value, data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:vania/vania.dart';

class IsImage extends ValidationRule {
final String args;
IsImage({required this.args, super.customErrorMessage});
IsImage({required this.args, super.message});

@override
bool validate(value, data) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:vania/src/http/validation/validation_chain/validation_rule.dart';

class IsInteger extends ValidationRule {
IsInteger({super.customErrorMessage});
IsInteger({super.message});

@override
bool validate(value, data) {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/http/validation/validation_chain/rules/is_ip.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:vania/src/config/defined_regexp.dart';
import 'package:vania/src/http/validation/validation_chain/validation_rule.dart';

class IsIp extends ValidationRule {
IsIp({super.customErrorMessage});
IsIp({super.message});

@override
bool validate(value, data) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:vania/src/http/validation/validation_chain/validation_rule.dart';

class IsJson extends ValidationRule {
IsJson({super.customErrorMessage});
IsJson({super.message});

@override
bool validate(value, data) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:vania/src/http/validation/validation_chain/validation_rule.dart';

class IsNumeric extends ValidationRule {
IsNumeric({super.customErrorMessage});
IsNumeric({super.message});

@override
bool validate(value, data) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:vania/src/http/validation/validation_chain/validation_rule.dart';

class IsRequired extends ValidationRule {
IsRequired({super.customErrorMessage});
IsRequired({super.message});

@override
bool validate(dynamic value, data) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:vania/src/http/validation/validation_chain/validation_rule.dart';

class IsString extends ValidationRule {
IsString({super.customErrorMessage});
IsString({super.message});

@override
bool validate(value, data) {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/http/validation/validation_chain/rules/is_url.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:vania/src/http/validation/validation_chain/validation_rule.dart';

class IsURL extends ValidationRule {
IsURL({super.customErrorMessage});
IsURL({super.message});

@override
bool validate(value, data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:vania/src/config/defined_regexp.dart';
import 'package:vania/src/http/validation/validation_chain/validation_rule.dart';

class IsUUID extends ValidationRule {
IsUUID({super.customErrorMessage});
IsUUID({super.message});

@override
bool validate(value, data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class LengthBetween extends ValidationRule {
final num lowerBoundary;
final num higherBoundary;
LengthBetween(
{super.customErrorMessage,
{super.message,
required this.lowerBoundary,
required this.higherBoundary});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:vania/src/http/validation/validation_chain/validation_rule.dart'

class LessThan extends ValidationRule {
final num compare;
LessThan({required this.compare, super.customErrorMessage});
LessThan({required this.compare, super.message});

@override
bool validate(value, data) {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/http/validation/validation_chain/rules/max.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:vania/src/http/validation/validation_chain/validation_rule.dart'

class Max extends ValidationRule {
final num maxValue;
Max({required this.maxValue, super.customErrorMessage});
Max({required this.maxValue, super.message});

@override
bool validate(value, data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:vania/src/http/validation/validation_chain/validation_rule.dart'

class MaxLength extends ValidationRule {
int maxLength;
MaxLength({required this.maxLength, super.customErrorMessage});
MaxLength({required this.maxLength, super.message});

@override
bool validate(value, data) {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/http/validation/validation_chain/rules/min.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:vania/src/http/validation/validation_chain/validation_rule.dart'

class Min extends ValidationRule {
final num minValue;
Min({required this.minValue, super.customErrorMessage});
Min({required this.minValue, super.message});

@override
bool validate(value, data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:vania/src/http/validation/validation_chain/validation_rule.dart'

class MinLength extends ValidationRule {
int minLength;
MinLength({required this.minLength, super.customErrorMessage});
MinLength({required this.minLength, super.message});

@override
bool validate(value, data) {
Expand Down
Loading

0 comments on commit 81b6ce5

Please sign in to comment.