diff --git a/community/dw-backend/bin/dao/dao.dart b/community/dw-backend/bin/dao/dao.dart new file mode 100644 index 0000000..0a635fe --- /dev/null +++ b/community/dw-backend/bin/dao/dao.dart @@ -0,0 +1,7 @@ +abstract class Dao { + Future create(T value); + Future findOne(int id); + Future> findAll(); + Future update(T value); + Future delete(int id); +} diff --git a/community/dw-backend/bin/dao/user_dao.dart b/community/dw-backend/bin/dao/user_dao.dart new file mode 100644 index 0000000..d8e6e42 --- /dev/null +++ b/community/dw-backend/bin/dao/user_dao.dart @@ -0,0 +1,57 @@ +import '../infra/database/db_configuration.dart'; +import '../models/user_model.dart'; +import 'dao.dart'; + +class UserDao implements Dao { + final DbConfiguration _dbConfiguration; + UserDao(this._dbConfiguration); + + @override + Future 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 delete(int id) async { + var result = await _dbConfiguration + .execQuery('DELETE from usuarios where id = ?', [id]); + return result.affectedRows > 0; + } + + @override + Future> findAll() async { + var result = await _dbConfiguration.execQuery('SELECT * FROM usuarios'); + return result + .map((r) => UserModel.fromMap(r.fields)) + .toList() + .cast(); + } + + @override + Future 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 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 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); + } +} diff --git a/community/dw-backend/bin/dw_backend.dart b/community/dw-backend/bin/dw_backend.dart index da03027..b5faf9f 100644 --- a/community/dw-backend/bin/dw_backend.dart +++ b/community/dw-backend/bin/dw_backend.dart @@ -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()); + + var user = UserModel() + ..id = 10 + ..name = 'John' + ..email = 'john@gmail.com' + ..password = '123'; + + dao.create(user); + dao.findAll().then(print); + dao.findAll().then(print); var cascadeHandler = Cascade() .add(_di.get().getHandler()) diff --git a/community/dw-backend/bin/infra/database/db_configuration.dart b/community/dw-backend/bin/infra/database/db_configuration.dart index 3387892..bb3769e 100644 --- a/community/dw-backend/bin/infra/database/db_configuration.dart +++ b/community/dw-backend/bin/infra/database/db_configuration.dart @@ -2,4 +2,6 @@ abstract class DbConfiguration { Future createConnection(); Future get connection; + + execQuery(String sql, [List? params]); } diff --git a/community/dw-backend/bin/infra/database/mysql_db_configuration.dart b/community/dw-backend/bin/infra/database/mysql_db_configuration.dart index 3be69e6..2ed6a79 100644 --- a/community/dw-backend/bin/infra/database/mysql_db_configuration.dart +++ b/community/dw-backend/bin/infra/database/mysql_db_configuration.dart @@ -9,22 +9,29 @@ class MySqlDbConfiguration implements DbConfiguration { @override Future get connection async { _connection ??= await createConnection(); + if (_connection == null) { throw Exception('[ERROR/DB] Failed create connection'); } + return _connection!; } @override - Future createConnection() async { - return await MySqlConnection.connect( - ConnectionSettings( - host: await CustomEnv.get(key: 'db_host'), - port: await CustomEnv.get(key: 'db_port'), - user: await CustomEnv.get(key: 'db_user'), - password: await CustomEnv.get(key: 'db_pass'), - db: await CustomEnv.get(key: 'db_schema'), - ), - ); + Future createConnection() async => + await MySqlConnection.connect( + ConnectionSettings( + host: await CustomEnv.get(key: 'db_host'), + port: await CustomEnv.get(key: 'db_port'), + user: await CustomEnv.get(key: 'db_user'), + password: await CustomEnv.get(key: 'db_pass'), + db: await CustomEnv.get(key: 'db_schema'), + ), + ); + + @override + execQuery(String sql, [List? params]) async { + var conn = await connection; + return await conn.query(sql, params); } } diff --git a/community/dw-backend/bin/infra/dependency_injector/dependency_injector.dart b/community/dw-backend/bin/infra/dependency_injector/dependency_injector.dart index e1cb5fc..65034ed 100644 --- a/community/dw-backend/bin/infra/dependency_injector/dependency_injector.dart +++ b/community/dw-backend/bin/infra/dependency_injector/dependency_injector.dart @@ -1,13 +1,12 @@ typedef T InstanceCreator(); class DependencyInjector { - DependencyInjector._(); - static final _singleton = DependencyInjector._(); + final _instanceMap = Map>(); - factory DependencyInjector() => _singleton; + DependencyInjector._(); - final _instanceMap = Map>(); + factory DependencyInjector() => _singleton; void register( InstanceCreator instance, { @@ -31,7 +30,6 @@ class DependencyInjector { class _InstanceGenerator { T? _instance; bool _isFirstGet = false; - final InstanceCreator _instanceCreator; _InstanceGenerator(this._instanceCreator, bool isSingleton) diff --git a/community/dw-backend/bin/models/user_model.dart b/community/dw-backend/bin/models/user_model.dart new file mode 100644 index 0000000..72cd1c8 --- /dev/null +++ b/community/dw-backend/bin/models/user_model.dart @@ -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 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)'; + } +}