Created
September 21, 2021 11:42
-
-
Save guilhermecarvalhocarneiro/cbcb75e1b90bf1b03f3910db9df0c65c to your computer and use it in GitHub Desktop.
UsuarioData Nullsafety
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
class UsuarioData { | |
UsuarioModel _usuarioModel = UsuarioModel(); | |
static final UsuarioData _instance = UsuarioData.internal(); | |
factory UsuarioData() => _instance; | |
UsuarioData.internal(); | |
// smbt.Database? _db; | |
final String _storeName = "UsuarioStoreDB"; | |
/// Método para inicialiar o banco de dados criando a tabela. | |
Future<smbt.Database?> initDb() async { | |
try { | |
var dir = await getApplicationDocumentsDirectory(); | |
await dir.create(recursive: true); | |
var dbPath = join(dir.path, 'agendeweb.db'); | |
return await smbt_io.databaseFactoryIo.openDatabase(dbPath); | |
} catch (error, exception) { | |
ExceptionCustom.data( | |
"Ocorreu no método initDb do UsuarioData", | |
"Error: ${error.toString()}\nException: ${exception.toString()}", | |
); | |
} | |
return null; | |
} | |
/// Método para recuperar todos os dados de Usuario | |
/// | |
/// returns: | |
/// Instância do UsuarioModel | |
Future<List<UsuarioModel>> fetchAll() async { | |
var _localList = <UsuarioModel>[]; | |
smbt.Database? _db; | |
try { | |
_db = await initDb(); | |
if (_db != null) { | |
var _store = smbt.intMapStoreFactory.store(_storeName); | |
var _data = await _store.find(_db); | |
_localList = _data.map((snapshot) { | |
return UsuarioModel.fromMap(snapshot.value); | |
}).toList(); | |
} | |
} catch (error, exception) { | |
ExceptionCustom.data( | |
"Ocorreu no método fetchAll do UsuarioData", | |
"Error: ${error.toString()}\nException: ${exception.toString()}", | |
); | |
} finally { | |
_db?.close(); | |
} | |
return _localList; | |
} | |
/// Método para recuperar o primeiro registro de Usuario | |
/// | |
/// returns: | |
/// Instância do UsuarioModel | |
Future<UsuarioModel?> get(int id) async { | |
smbt.Database? _db; | |
try { | |
_db = await initDb(); | |
if (_db != null) { | |
var _store = smbt.intMapStoreFactory.store(_storeName); | |
var _data = await _store.findFirst(_db); | |
if(_data != null){ | |
return UsuarioModel.fromMap(_data.value); | |
} | |
} | |
} catch (error, exception) { | |
ExceptionCustom.data( | |
"Ocorreu no método get do UsuarioData", | |
"Error: ${error.toString()}\nException: ${exception.toString()}", | |
); | |
} finally { | |
_db?.close(); | |
} | |
return null; | |
} | |
/// Método para salvar localmente um(a) Usuario | |
/// | |
/// returns: | |
/// bool -> true salvo com sucesso, false ocorreu um erro | |
Future<bool> save(UsuarioModel usuario) async { | |
smbt.Database? _db; | |
try { | |
_db = await initDb(); | |
if (_db != null) { | |
var _store = smbt.intMapStoreFactory.store(_storeName); | |
final _map = usuario.toMap(); | |
if(_map != null){ | |
// Salvando o dado do novo usuario | |
await _store.add(_db, _map); | |
return true; | |
} | |
} | |
} catch (error, exception) { | |
ExceptionCustom.data( | |
"Ocorreu no método save do UsuarioData", | |
"Error: ${error.toString()}\nException: ${exception.toString()}", | |
); | |
} finally { | |
_db?.close(); | |
} | |
return false; | |
} | |
/// Método para deletar todos os registros do(a) Usuario | |
/// | |
/// returns: | |
/// bool -> true salvo com sucesso, false ocorreu um erro | |
Future<bool> deleteAll() async { | |
smbt.Database? _db; | |
try { | |
_db = await initDb(); | |
if (_db != null) { | |
var _store = smbt.intMapStoreFactory.store(_storeName); | |
// Apagando todos os registros anteriores | |
await _store.delete(_db); | |
return true; | |
} | |
} catch (error, exception) { | |
ExceptionCustom.data( | |
"Ocorreu no método deletar do UsuarioData", | |
"Error: ${error.toString()}\nException: ${exception.toString()}", | |
); | |
} finally { | |
_db?.close(); | |
} | |
return false; | |
} | |
/// Método para deletar um registro do(a) Usuario | |
/// | |
/// returns: | |
/// bool -> true salvo com sucesso, false ocorreu um erro | |
Future<bool> delete(int id) async { | |
smbt.Database? _db; | |
try { | |
_db = await initDb(); | |
if (_db != null) { | |
var _store = smbt.intMapStoreFactory.store(_storeName); | |
await _store.delete(_db); | |
return true; | |
} | |
} catch (error, exception) { | |
ExceptionCustom.data( | |
"Ocorreu no método deletar do UsuarioData", | |
"Error: ${error.toString()}\nException: ${exception.toString()}", | |
); | |
} finally { | |
_db?.close(); | |
} | |
return false; | |
} | |
/// Método para atualizar um registro do(a) Usuario | |
/// | |
/// returns: | |
/// bool -> true salvo com sucesso, false ocorreu um erro | |
Future<bool> update(UsuarioModel usuario) async { | |
smbt.Database? _db; | |
try { | |
_db = await initDb(); | |
if (_db != null) { | |
var _store = smbt.intMapStoreFactory.store(_storeName); | |
final finder = smbt.Finder(filter: smbt.Filter.byKey(usuario.id)); | |
final _map = usuario.toMap(); | |
if(_map != null){ | |
await _store.update(_db, _map, finder: finder); | |
return true; | |
} | |
} | |
} catch (error, exception) { | |
ExceptionCustom.data( | |
"Ocorreu no método update do UsuarioData", | |
"Error: ${error.toString()}\nException: ${exception.toString()}", | |
); | |
} finally { | |
_db?.close(); | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment