From f619d906053b8d5a06a06b1a3197ee556805287f Mon Sep 17 00:00:00 2001 From: "audric.sabatier" Date: Wed, 23 Nov 2022 14:13:22 +0100 Subject: [PATCH] ADD : DatabaseService class to manage dataabse access --- .idea/libraries/Dart_Packages.xml | 692 ------------------ .idea/libraries/Dart_SDK.xml | 38 +- .../lib/controller/controller.dart | 18 +- .../{persistence => database}/.gitkeep | 0 .../database_connexion.dart | 0 .../database_loader.dart | 4 +- .../database_saver.dart | 0 .../database_searcher.dart | 7 +- .../services/database/database_service.dart | 38 + .../{persistence => database}/loader.dart | 2 +- .../{persistence => database}/saver.dart | 0 .../lib/services/database/searcher.dart | 3 + .../lib/services/persistence/searcher.dart | 5 - .../lib/views/pages/sign_in/p_sign_in.dart | 8 +- .../lib/views/pages/sign_up/p_sign_up.dart | 6 +- 15 files changed, 80 insertions(+), 741 deletions(-) delete mode 100644 .idea/libraries/Dart_Packages.xml rename Sources/dafl_project_flutter/lib/services/{persistence => database}/.gitkeep (100%) rename Sources/dafl_project_flutter/lib/services/{persistence => database}/database_connexion.dart (100%) rename Sources/dafl_project_flutter/lib/services/{persistence => database}/database_loader.dart (89%) rename Sources/dafl_project_flutter/lib/services/{persistence => database}/database_saver.dart (100%) rename Sources/dafl_project_flutter/lib/services/{persistence => database}/database_searcher.dart (77%) create mode 100644 Sources/dafl_project_flutter/lib/services/database/database_service.dart rename Sources/dafl_project_flutter/lib/services/{persistence => database}/loader.dart (58%) rename Sources/dafl_project_flutter/lib/services/{persistence => database}/saver.dart (100%) create mode 100644 Sources/dafl_project_flutter/lib/services/database/searcher.dart delete mode 100644 Sources/dafl_project_flutter/lib/services/persistence/searcher.dart diff --git a/.idea/libraries/Dart_Packages.xml b/.idea/libraries/Dart_Packages.xml deleted file mode 100644 index f8237c9..0000000 --- a/.idea/libraries/Dart_Packages.xml +++ /dev/null @@ -1,692 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/libraries/Dart_SDK.xml b/.idea/libraries/Dart_SDK.xml index cb79f3d..61aaa79 100644 --- a/.idea/libraries/Dart_SDK.xml +++ b/.idea/libraries/Dart_SDK.xml @@ -1,25 +1,25 @@ - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + diff --git a/Sources/dafl_project_flutter/lib/controller/controller.dart b/Sources/dafl_project_flutter/lib/controller/controller.dart index a7ea9dd..6cf1345 100644 --- a/Sources/dafl_project_flutter/lib/controller/controller.dart +++ b/Sources/dafl_project_flutter/lib/controller/controller.dart @@ -1,6 +1,7 @@ import 'dart:convert'; import 'package:dafl_project_flutter/model/music.dart'; import 'package:dafl_project_flutter/services/api/api_spotify.dart'; +import 'package:dafl_project_flutter/services/database/database_service.dart'; import 'package:dafl_project_flutter/services/position/area.dart'; import 'package:flutter/cupertino.dart'; import 'package:http/http.dart' as http; @@ -11,6 +12,7 @@ class Controller { ApiSpotify _api = ApiSpotify(); late User _currentUser; Area _area = Area(); + DataBaseService _dataBaseService = DataBaseService(); late BuildContext navigatorKey; @@ -74,14 +76,10 @@ class Controller { _api.requests.addToPlaylist(id); } -/* - static Saver saver = DatabaseSaver(); - static Loader loader = DatabaseLoader(); - static final Searcher _searcher = DatabaseSearcher(); - late BuildContext navigatorKey; + void save(User userToSave) { - saver.save(userToSave); + _dataBaseService.save(userToSave); } load(String username, String password) async { @@ -96,10 +94,14 @@ class Controller { //TODO : call database method } - Future searchByUsername(String username) async { - return await _searcher.searchByUsername(username); + Future searchUser(String username) async { + return await _dataBaseService.searchUser(username); } + + + + Future sendEmail( User reporter, User reported, String reason, String message) async { const serviceId = 'service_dzyndyb'; diff --git a/Sources/dafl_project_flutter/lib/services/persistence/.gitkeep b/Sources/dafl_project_flutter/lib/services/database/.gitkeep similarity index 100% rename from Sources/dafl_project_flutter/lib/services/persistence/.gitkeep rename to Sources/dafl_project_flutter/lib/services/database/.gitkeep diff --git a/Sources/dafl_project_flutter/lib/services/persistence/database_connexion.dart b/Sources/dafl_project_flutter/lib/services/database/database_connexion.dart similarity index 100% rename from Sources/dafl_project_flutter/lib/services/persistence/database_connexion.dart rename to Sources/dafl_project_flutter/lib/services/database/database_connexion.dart diff --git a/Sources/dafl_project_flutter/lib/services/persistence/database_loader.dart b/Sources/dafl_project_flutter/lib/services/database/database_loader.dart similarity index 89% rename from Sources/dafl_project_flutter/lib/services/persistence/database_loader.dart rename to Sources/dafl_project_flutter/lib/services/database/database_loader.dart index 7edb28c..3b2d1b5 100644 --- a/Sources/dafl_project_flutter/lib/services/persistence/database_loader.dart +++ b/Sources/dafl_project_flutter/lib/services/database/database_loader.dart @@ -7,7 +7,7 @@ import 'dart:developer' as dev; class DatabaseLoader extends Loader { // Load an user from database @override - Future load(String username, String password) async { + Future load(String username, String password) async { final connection = await DatabaseConnexion.initConnexion(); var queryResult = await connection @@ -20,7 +20,7 @@ class DatabaseLoader extends Loader { if (result.isNotEmpty) { return User(username, password); } else { - return User("", ""); + return null; } }) .whenComplete(() { diff --git a/Sources/dafl_project_flutter/lib/services/persistence/database_saver.dart b/Sources/dafl_project_flutter/lib/services/database/database_saver.dart similarity index 100% rename from Sources/dafl_project_flutter/lib/services/persistence/database_saver.dart rename to Sources/dafl_project_flutter/lib/services/database/database_saver.dart diff --git a/Sources/dafl_project_flutter/lib/services/persistence/database_searcher.dart b/Sources/dafl_project_flutter/lib/services/database/database_searcher.dart similarity index 77% rename from Sources/dafl_project_flutter/lib/services/persistence/database_searcher.dart rename to Sources/dafl_project_flutter/lib/services/database/database_searcher.dart index e10328a..04c66b2 100644 --- a/Sources/dafl_project_flutter/lib/services/persistence/database_searcher.dart +++ b/Sources/dafl_project_flutter/lib/services/database/database_searcher.dart @@ -2,14 +2,11 @@ import 'database_connexion.dart'; import 'searcher.dart'; class DatabaseSearcher extends Searcher { - @override - Future searchUser(String? username, String? password) async { - return true; - } + // Search an user in the database by username @override - Future searchByUsername(String? username) async { + Future searchuser(String? username) async { final connection = await DatabaseConnexion.initConnexion(); bool queryResult = await connection diff --git a/Sources/dafl_project_flutter/lib/services/database/database_service.dart b/Sources/dafl_project_flutter/lib/services/database/database_service.dart new file mode 100644 index 0000000..f20fcd9 --- /dev/null +++ b/Sources/dafl_project_flutter/lib/services/database/database_service.dart @@ -0,0 +1,38 @@ +import 'package:dafl_project_flutter/services/database/database_loader.dart'; +import 'package:dafl_project_flutter/services/database/database_saver.dart'; +import 'package:dafl_project_flutter/services/database/database_searcher.dart'; +import 'package:dafl_project_flutter/services/database/loader.dart'; +import 'package:dafl_project_flutter/services/database/saver.dart'; +import 'package:dafl_project_flutter/services/database/searcher.dart'; + +import '../../model/user.dart'; + +class DataBaseService{ + static final Loader _loader = DatabaseLoader(); + static final Searcher _searcher = DatabaseSearcher(); + static final Saver _saver = DatabaseSaver(); + + + void save(User userToSave) { + _saver.save(userToSave); + } + + Future load(String username, String password) async { + return _loader.load(username, password); + } + + Future searchUser(String username) async { + return await _searcher.searchUser(username); + } + + changeUsername(String newName) { + //TODO : call database method + } + + changeCurrentPassword(String newPass) { + //TODO : call database method + } + + + +} \ No newline at end of file diff --git a/Sources/dafl_project_flutter/lib/services/persistence/loader.dart b/Sources/dafl_project_flutter/lib/services/database/loader.dart similarity index 58% rename from Sources/dafl_project_flutter/lib/services/persistence/loader.dart rename to Sources/dafl_project_flutter/lib/services/database/loader.dart index 768426c..4898fdc 100644 --- a/Sources/dafl_project_flutter/lib/services/persistence/loader.dart +++ b/Sources/dafl_project_flutter/lib/services/database/loader.dart @@ -2,5 +2,5 @@ import 'dart:async'; import '../../model/user.dart'; abstract class Loader { - Future load(String username, String password); + Future load(String username, String password); } diff --git a/Sources/dafl_project_flutter/lib/services/persistence/saver.dart b/Sources/dafl_project_flutter/lib/services/database/saver.dart similarity index 100% rename from Sources/dafl_project_flutter/lib/services/persistence/saver.dart rename to Sources/dafl_project_flutter/lib/services/database/saver.dart diff --git a/Sources/dafl_project_flutter/lib/services/database/searcher.dart b/Sources/dafl_project_flutter/lib/services/database/searcher.dart new file mode 100644 index 0000000..afb4319 --- /dev/null +++ b/Sources/dafl_project_flutter/lib/services/database/searcher.dart @@ -0,0 +1,3 @@ +abstract class Searcher { + Future searchUser(String? username); +} diff --git a/Sources/dafl_project_flutter/lib/services/persistence/searcher.dart b/Sources/dafl_project_flutter/lib/services/persistence/searcher.dart deleted file mode 100644 index f27315d..0000000 --- a/Sources/dafl_project_flutter/lib/services/persistence/searcher.dart +++ /dev/null @@ -1,5 +0,0 @@ -abstract class Searcher { - Future searchUser(String? username, String? password); - - Future searchByUsername(String? username); -} diff --git a/Sources/dafl_project_flutter/lib/views/pages/sign_in/p_sign_in.dart b/Sources/dafl_project_flutter/lib/views/pages/sign_in/p_sign_in.dart index 750f061..c2a9e23 100644 --- a/Sources/dafl_project_flutter/lib/views/pages/sign_in/p_sign_in.dart +++ b/Sources/dafl_project_flutter/lib/views/pages/sign_in/p_sign_in.dart @@ -257,11 +257,8 @@ class _SignInPageState extends State { notify(2, context); } else if (password == "") { notify(4, context); - } else { - /*await MyApp.controller - .load(userNameTextField.text, passwordTextField.text);*/ - - if (MyApp.controller.getIdDafl().toString() != "") { + } else if(await MyApp.controller + .load(userNameTextField.text, passwordTextField.text) != null) { Navigator.of(context).push( PageTransition( type: PageTransitionType.fade, @@ -273,4 +270,3 @@ class _SignInPageState extends State { } } } -} diff --git a/Sources/dafl_project_flutter/lib/views/pages/sign_up/p_sign_up.dart b/Sources/dafl_project_flutter/lib/views/pages/sign_up/p_sign_up.dart index f50fa7e..04e979b 100644 --- a/Sources/dafl_project_flutter/lib/views/pages/sign_up/p_sign_up.dart +++ b/Sources/dafl_project_flutter/lib/views/pages/sign_up/p_sign_up.dart @@ -335,9 +335,9 @@ class _SignUpPageState extends State { if (username == "") { notify(2, context); } - /* else if (!await MyApp.controller.searchByUsername(username)) { + else if (! await MyApp.controller.searchUser(username)) { notify(0, context); - }*/ + } if (password == "" || confirmPassword == "") { notify(4, context); } else if (password.length < 8) { @@ -345,7 +345,7 @@ class _SignUpPageState extends State { } else if (password != confirmPassword) { notify(1, context); } else { - //MyApp.controller.save(User(username, password)); + MyApp.controller.save(User(username, password)); Navigator.of(context).push( PageTransition(