-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
149 additions
and
15 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
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); | ||
} |
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 |
---|---|---|
@@ -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); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -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()) | ||
|
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 |
---|---|---|
@@ -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)'; | ||
} | ||
} |