Skip to content

Commit

Permalink
feat: add dw backend part 9
Browse files Browse the repository at this point in the history
  • Loading branch information
mdssjc committed May 20, 2022
1 parent 613bde8 commit 4f60ea3
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 15 deletions.
7 changes: 7 additions & 0 deletions community/dw-backend/bin/dao/dao.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
abstract class Dao<T> {
Future<bool> create(T value);
Future<T?> findOne(int id);
Future<List<T>> findAll();
Future<bool> update(T value);
Future<bool> delete(int id);
}
57 changes: 57 additions & 0 deletions community/dw-backend/bin/dao/user_dao.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import '../infra/database/db_configuration.dart';
import '../models/user_model.dart';
import 'dao.dart';

class UserDao implements Dao<UserModel> {
final DbConfiguration _dbConfiguration;
UserDao(this._dbConfiguration);

@override
Future<bool> create(UserModel value) async {
var result = await _dbConfiguration.execQuery(
'INSERT INTO usuarios (nome, email, password) VALUES (?, ?, ?)',
[value.name, value.email, value.password],
);
return result.affectedRows > 0;
}

@override
Future<bool> delete(int id) async {
var result = await _dbConfiguration
.execQuery('DELETE from usuarios where id = ?', [id]);
return result.affectedRows > 0;
}

@override
Future<List<UserModel>> findAll() async {
var result = await _dbConfiguration.execQuery('SELECT * FROM usuarios');
return result
.map((r) => UserModel.fromMap(r.fields))
.toList()
.cast<UserModel>();
}

@override
Future<UserModel?> findOne(int id) async {
var result = await _dbConfiguration
.execQuery('SELECT * FROM usuarios WHERE id = ?', [id]);
return result.affectedRows == 0
? null
: UserModel.fromMap(result.first.fields);
}

@override
Future<bool> update(UserModel value) async {
var result = await _dbConfiguration.execQuery(
'UPDATE usuarios SET nome = ?, password = ? WHERE id = ?',
[value.name, value.password, value.id],
);
return result.affectedRows > 0;
}

Future<UserModel?> findByEmail(String email) async {
var r = await _dbConfiguration
.execQuery('SELECT * FROM usuarios WHERE email = ?', [email]);
return r.affectedRows == 0 ? null : UserModel.fromEmail(r.first.fields);
}
}
14 changes: 14 additions & 0 deletions community/dw-backend/bin/dw_backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,29 @@ import 'package:shelf/shelf.dart';

import 'apis/blog_api.dart';
import 'apis/login_api.dart';
import 'dao/user_dao.dart';
import 'infra/custom_server.dart';
import 'infra/database/db_configuration.dart';
import 'infra/dependency_injector/injects.dart';
import 'infra/middleware_interception.dart';
import 'models/user_model.dart';
import 'utils/custom_env.dart';

void main() async {
CustomEnv.fromFile('.env-dev');

final _di = Injects.initialize();
UserDao dao = UserDao(_di<DbConfiguration>());

var user = UserModel()
..id = 10
..name = 'John'
..email = '[email protected]'
..password = '123';

dao.create(user);
dao.findAll().then(print);
dao.findAll().then(print);

var cascadeHandler = Cascade()
.add(_di.get<LoginApi>().getHandler())
Expand Down
2 changes: 2 additions & 0 deletions community/dw-backend/bin/infra/database/db_configuration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ abstract class DbConfiguration {
Future<dynamic> createConnection();

Future<dynamic> get connection;

execQuery(String sql, [List? params]);
}
27 changes: 17 additions & 10 deletions community/dw-backend/bin/infra/database/mysql_db_configuration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,29 @@ class MySqlDbConfiguration implements DbConfiguration {
@override
Future<MySqlConnection> get connection async {
_connection ??= await createConnection();

if (_connection == null) {
throw Exception('[ERROR/DB] Failed create connection');
}

return _connection!;
}

@override
Future<MySqlConnection> createConnection() async {
return await MySqlConnection.connect(
ConnectionSettings(
host: await CustomEnv.get<String>(key: 'db_host'),
port: await CustomEnv.get<int>(key: 'db_port'),
user: await CustomEnv.get<String>(key: 'db_user'),
password: await CustomEnv.get<String>(key: 'db_pass'),
db: await CustomEnv.get<String>(key: 'db_schema'),
),
);
Future<MySqlConnection> createConnection() async =>
await MySqlConnection.connect(
ConnectionSettings(
host: await CustomEnv.get<String>(key: 'db_host'),
port: await CustomEnv.get<int>(key: 'db_port'),
user: await CustomEnv.get<String>(key: 'db_user'),
password: await CustomEnv.get<String>(key: 'db_pass'),
db: await CustomEnv.get<String>(key: 'db_schema'),
),
);

@override
execQuery(String sql, [List? params]) async {
var conn = await connection;
return await conn.query(sql, params);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
typedef T InstanceCreator<T>();

class DependencyInjector {
DependencyInjector._();

static final _singleton = DependencyInjector._();
final _instanceMap = Map<Type, _InstanceGenerator<Object>>();

factory DependencyInjector() => _singleton;
DependencyInjector._();

final _instanceMap = Map<Type, _InstanceGenerator<Object>>();
factory DependencyInjector() => _singleton;

void register<T extends Object>(
InstanceCreator<T> instance, {
Expand All @@ -31,7 +30,6 @@ class DependencyInjector {
class _InstanceGenerator<T> {
T? _instance;
bool _isFirstGet = false;

final InstanceCreator<T> _instanceCreator;

_InstanceGenerator(this._instanceCreator, bool isSingleton)
Expand Down
49 changes: 49 additions & 0 deletions community/dw-backend/bin/models/user_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class UserModel {
int? id;
String? name;
String? email;
String? password;
bool? isActived;
DateTime? dtCreated;
DateTime? dtUpdated;

UserModel();

UserModel.create(
this.id,
this.name,
this.email,
this.isActived,
this.dtCreated,
this.dtUpdated,
);

factory UserModel.fromMap(Map<String, dynamic> map) {
return UserModel.create(
map['id']?.toInt() ?? 0,
map['nome'] ?? '',
map['email'] ?? '',
map['is_ativo'] == 1,
map['dt_criacao'],
map['dt_autalizacao'],
);
}

factory UserModel.fromEmail(Map map) {
return UserModel()
..id = map['id']?.toInt()
..password = map['password'];
}

factory UserModel.fromRequest(Map map) {
return UserModel()
..name = map['name']
..email = map['email']
..password = map['password'];
}

@override
String toString() {
return 'UserModel(id: $id, name: $name, email: $email, isActived: $isActived, dtCreated: $dtCreated, dtUpdated: $dtUpdated)';
}
}

0 comments on commit 4f60ea3

Please sign in to comment.