From 77d5557743faa4c05f9e511004becfb52b6fc680 Mon Sep 17 00:00:00 2001 From: "audric.sabatier" Date: Thu, 3 Nov 2022 18:20:48 +0100 Subject: [PATCH 1/5] =?UTF-8?q?ADD=20:=20ajout=20d'une=20classe=20de=20rec?= =?UTF-8?q?herche=20d'un=20utilisateur=20en=20base=20de=20donn=C3=A9es.=20?= =?UTF-8?q?Notification=20=C3=A0=20l'utilisateur=20quand=20il=20rentre=20u?= =?UTF-8?q?n=20username=20d=C3=A9ja=20utilis=C3=A9.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/libraries/Dart_Packages.xml | 588 ------------------ .idea/libraries/Dart_SDK.xml | 38 +- .../lib/controller/controller.dart | 7 + .../lib/persistence/database_search.dart | 0 .../lib/persistence/database_searcher.dart | 25 + .../lib/persistence/search.dart | 6 - .../lib/persistence/searcher.dart | 6 +- .../lib/views/pages/sign_up/p_sign_up.dart | 7 +- 8 files changed, 60 insertions(+), 617 deletions(-) delete mode 100644 .idea/libraries/Dart_Packages.xml delete mode 100644 Sources/dafl_project_flutter/lib/persistence/database_search.dart create mode 100644 Sources/dafl_project_flutter/lib/persistence/database_searcher.dart delete mode 100644 Sources/dafl_project_flutter/lib/persistence/search.dart diff --git a/.idea/libraries/Dart_Packages.xml b/.idea/libraries/Dart_Packages.xml deleted file mode 100644 index 86b1c24..0000000 --- a/.idea/libraries/Dart_Packages.xml +++ /dev/null @@ -1,588 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/libraries/Dart_SDK.xml b/.idea/libraries/Dart_SDK.xml index 6ae284f..008f0f6 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 053e331..498f729 100644 --- a/Sources/dafl_project_flutter/lib/controller/controller.dart +++ b/Sources/dafl_project_flutter/lib/controller/controller.dart @@ -1,10 +1,12 @@ import '../persistence/database_loader.dart'; import '../persistence/database_saver.dart'; +import '../persistence/database_searcher.dart'; import '../persistence/loader.dart'; import '../persistence/saver.dart'; import '../persistence/loader.dart'; import '../model/user.dart'; +import '../persistence/searcher.dart'; class Controller{ @@ -12,6 +14,7 @@ class Controller{ static Saver? saver = DatabaseSaver(); static Loader? loader = DatabaseLoader(); + static Searcher _searcher = DatabaseSearcher(); User currentUser = User(null, null); @@ -48,4 +51,8 @@ class Controller{ this.currentUser?.passwDafl = newPass; } } + + Future searchByUsername(String username) async{ + return _searcher.searchByUsername(username); + } } diff --git a/Sources/dafl_project_flutter/lib/persistence/database_search.dart b/Sources/dafl_project_flutter/lib/persistence/database_search.dart deleted file mode 100644 index e69de29..0000000 diff --git a/Sources/dafl_project_flutter/lib/persistence/database_searcher.dart b/Sources/dafl_project_flutter/lib/persistence/database_searcher.dart new file mode 100644 index 0000000..6148e8a --- /dev/null +++ b/Sources/dafl_project_flutter/lib/persistence/database_searcher.dart @@ -0,0 +1,25 @@ +import 'package:dafl_project_flutter/persistence/database_connexion.dart'; + +import 'searcher.dart'; + +class DatabaseSearcher extends Searcher{ + DatabaseConnexion dbConnexion = DatabaseConnexion(); + + Future searchUser(String? username, String? password) async { return true; } + + + @override + Future searchByUsername(String? username) async{ + final connection = await dbConnexion.initConnexion(); + + connection.query('select * from utilisateur where username = $username').toList().then((rows) { + if(rows.isEmpty){ + connection.close(); + return false; + } + }); + + connection.close(); + return true; + } +} \ No newline at end of file diff --git a/Sources/dafl_project_flutter/lib/persistence/search.dart b/Sources/dafl_project_flutter/lib/persistence/search.dart deleted file mode 100644 index 76bb714..0000000 --- a/Sources/dafl_project_flutter/lib/persistence/search.dart +++ /dev/null @@ -1,6 +0,0 @@ -import '../model/user.dart'; - -abstract class Search{ - bool searchUser(String? username, String? password); - bool searchUsername(String? username); -} \ No newline at end of file diff --git a/Sources/dafl_project_flutter/lib/persistence/searcher.dart b/Sources/dafl_project_flutter/lib/persistence/searcher.dart index 76bb714..276a197 100644 --- a/Sources/dafl_project_flutter/lib/persistence/searcher.dart +++ b/Sources/dafl_project_flutter/lib/persistence/searcher.dart @@ -1,6 +1,6 @@ import '../model/user.dart'; -abstract class Search{ - bool searchUser(String? username, String? password); - bool searchUsername(String? username); +abstract class Searcher{ + Future searchUser(String? username, String? password); + Future searchByUsername(String? username); } \ No newline at end of file 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 e09ee2a..af4f2d4 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 @@ -298,10 +298,14 @@ class _SignUpPageState extends State { ); } - void checkInformations(String username,String password, String confirmPassword){ + Future checkInformations(String username,String password, String confirmPassword) async { if(username ==""){ Notify(2, context); } + else if(! await MyApp().controller.searchByUsername(username)){ + Notify(0, context); + } + /* else if(password =="" || confirmPassword == ""){ Notify(4, context); } @@ -314,6 +318,7 @@ class _SignUpPageState extends State { else if(password != confirmPassword){ Notify(1, context); } + */ else{ MyApp().controller.save(User(userNameTextField.text, passwordConfirmTextField.text)); Navigator.of(context).push( From 36e8d5faba0ea95f042eb6d453aecdf15b9989a0 Mon Sep 17 00:00:00 2001 From: "audric.sabatier" Date: Thu, 3 Nov 2022 20:39:59 +0100 Subject: [PATCH 2/5] =?UTF-8?q?ADD=20:=20Am=C3=A9lioration=20classe=20Data?= =?UTF-8?q?baseConnection.=20Commentaires=20+=20fonctions=20static=20(appe?= =?UTF-8?q?l=20possible=20sans=20instanci=C3=A9=20d'objet)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lib/persistence/database_connexion.dart | 27 ++++++++++++------- .../lib/persistence/database_loader.dart | 5 ++-- .../lib/persistence/database_saver.dart | 3 ++- .../lib/persistence/database_searcher.dart | 4 +-- .../test/testFileRead.dart | 2 +- 5 files changed, 24 insertions(+), 17 deletions(-) diff --git a/Sources/dafl_project_flutter/lib/persistence/database_connexion.dart b/Sources/dafl_project_flutter/lib/persistence/database_connexion.dart index 7635688..4983ac3 100644 --- a/Sources/dafl_project_flutter/lib/persistence/database_connexion.dart +++ b/Sources/dafl_project_flutter/lib/persistence/database_connexion.dart @@ -1,6 +1,5 @@ import 'dart:convert'; import 'dart:io'; - import 'package:flutter/services.dart'; import 'package:path_provider/path_provider.dart'; @@ -9,19 +8,26 @@ import 'package:postgresql2/constants.dart'; import 'package:postgresql2/pool.dart'; import 'package:postgresql2/postgresql.dart'; + class DatabaseConnexion{ - final String filePath = 'assets/logs.txt'; + // Path to the file containing the database identifiers + static final String filePath = 'assets/logs.txt'; + - String? _psqlUser; - String? _psqlPswd; - String? _psqlHost; - String? _psqlDataBase; + // Database identifiers + static String? _psqlUser; + static String? _psqlPswd; + static String? _psqlHost; + static String? _psqlDataBase; - Future _loadLogs() async{ + // Read the database connection identifiers in a file + static Future _loadLogs() async{ try{ final _loadedData = await rootBundle.loadString(filePath); + print('appel de -readLogs'); + final _logs = LineSplitter.split(_loadedData).toList(); _psqlUser = _logs[0]; @@ -35,9 +41,12 @@ class DatabaseConnexion{ } + //Initialise connexion to the database - Future initConnexion() async{ - await _loadLogs(); + static Future initConnexion() async{ + if(_psqlHost == null || _psqlPswd == null || _psqlUser == null || _psqlDataBase == null){ + await _loadLogs(); + } var uri = 'postgres://$_psqlUser:$_psqlPswd@$_psqlHost:5442/$_psqlDataBase'; diff --git a/Sources/dafl_project_flutter/lib/persistence/database_loader.dart b/Sources/dafl_project_flutter/lib/persistence/database_loader.dart index a632976..94416d4 100644 --- a/Sources/dafl_project_flutter/lib/persistence/database_loader.dart +++ b/Sources/dafl_project_flutter/lib/persistence/database_loader.dart @@ -5,12 +5,10 @@ import '../model/user.dart'; import 'database_connexion.dart'; class DatabaseLoader extends Loader{ - DatabaseConnexion dbConnexion = DatabaseConnexion(); - @override Future load(String? username, String? password) async { - final connection = await dbConnexion.initConnexion(); + final connection = await DatabaseConnexion.initConnexion(); connection.query('select * from utilisateur where username = @username AND password = @password', {'username': username, @@ -18,4 +16,5 @@ class DatabaseLoader extends Loader{ .then((result) { print(result); }); } + } diff --git a/Sources/dafl_project_flutter/lib/persistence/database_saver.dart b/Sources/dafl_project_flutter/lib/persistence/database_saver.dart index 53d70fe..e900980 100644 --- a/Sources/dafl_project_flutter/lib/persistence/database_saver.dart +++ b/Sources/dafl_project_flutter/lib/persistence/database_saver.dart @@ -9,11 +9,12 @@ class DatabaseSaver extends Saver{ @override void save(User userToSave) async{ - final connection = await dbConnexion.initConnexion(); + final connection = await DatabaseConnexion.initConnexion(); connection.execute('insert into utilisateur (username, password) values (@username, @password)', { 'id' : '', 'username': userToSave.usernameDafl, 'password' : userToSave.passwDafl}).then((_) {}); } + } \ No newline at end of file diff --git a/Sources/dafl_project_flutter/lib/persistence/database_searcher.dart b/Sources/dafl_project_flutter/lib/persistence/database_searcher.dart index 6148e8a..5bf2d40 100644 --- a/Sources/dafl_project_flutter/lib/persistence/database_searcher.dart +++ b/Sources/dafl_project_flutter/lib/persistence/database_searcher.dart @@ -3,14 +3,12 @@ import 'package:dafl_project_flutter/persistence/database_connexion.dart'; import 'searcher.dart'; class DatabaseSearcher extends Searcher{ - DatabaseConnexion dbConnexion = DatabaseConnexion(); - Future searchUser(String? username, String? password) async { return true; } @override Future searchByUsername(String? username) async{ - final connection = await dbConnexion.initConnexion(); + final connection = await DatabaseConnexion.initConnexion(); connection.query('select * from utilisateur where username = $username').toList().then((rows) { if(rows.isEmpty){ diff --git a/Sources/dafl_project_flutter/test/testFileRead.dart b/Sources/dafl_project_flutter/test/testFileRead.dart index d5b2113..5904fdd 100644 --- a/Sources/dafl_project_flutter/test/testFileRead.dart +++ b/Sources/dafl_project_flutter/test/testFileRead.dart @@ -10,6 +10,6 @@ Future main() async { DatabaseConnexion d = DatabaseConnexion(); - d.initConnexion(); + DatabaseConnexion.initConnexion(); } \ No newline at end of file From 0784d8f07461fed9b65f65e9b9c9cc5ebaacdc9e Mon Sep 17 00:00:00 2001 From: "audric.sabatier" Date: Fri, 4 Nov 2022 19:28:39 +0100 Subject: [PATCH 3/5] Fix : Modification des attributs nullable et non nullable, late pour instanciation future. --- .../lib/controller/controller.dart | 2 +- .../dafl_project_flutter/lib/model/user.dart | 41 +++++++++++++++---- .../lib/persistence/database_connexion.dart | 15 ++++--- .../lib/persistence/database_loader.dart | 14 +++++-- .../lib/persistence/database_saver.dart | 1 - .../lib/persistence/database_searcher.dart | 12 +++--- .../lib/persistence/loader.dart | 2 +- .../lib/views/pages/sign_in/p_sign_in.dart | 3 +- .../lib/views/pages/sign_up/p_sign_up.dart | 17 ++++---- Sources/dafl_project_flutter/pubspec.yaml | 1 + 10 files changed, 74 insertions(+), 34 deletions(-) diff --git a/Sources/dafl_project_flutter/lib/controller/controller.dart b/Sources/dafl_project_flutter/lib/controller/controller.dart index 498f729..835b87a 100644 --- a/Sources/dafl_project_flutter/lib/controller/controller.dart +++ b/Sources/dafl_project_flutter/lib/controller/controller.dart @@ -16,7 +16,7 @@ class Controller{ static Loader? loader = DatabaseLoader(); static Searcher _searcher = DatabaseSearcher(); - User currentUser = User(null, null); + User currentUser = User("", ""); factory Controller(){ if (_this == null) _this = Controller._(); diff --git a/Sources/dafl_project_flutter/lib/model/user.dart b/Sources/dafl_project_flutter/lib/model/user.dart index f1063a4..22bcb95 100644 --- a/Sources/dafl_project_flutter/lib/model/user.dart +++ b/Sources/dafl_project_flutter/lib/model/user.dart @@ -6,19 +6,40 @@ import 'music.dart'; class User{ //attributes from DAFL - int? idDafl; - String? usernameDafl; - String? passwDafl; + late int _idDafl; + late String _usernameDafl; + late String _passwDafl; + //attributes to link with API - String? usernameAPI; - String? passwAPI; + late String _usernameAPI; + late String _passwAPI; + + + + // Getters for attributes + int get idDafl => _idDafl; + String get passwAPI => _passwAPI; + String get usernameDafl => _usernameDafl; + String get passwDafl => _passwDafl; + String get usernameAPI => _usernameAPI; + + // Setters for attributes + set idDafl(int value) { _idDafl = value; } + set usernameDafl(String value) { _usernameDafl = value; } + set passwDafl(String value) { _passwDafl = value; } + set usernameAPI(String value) { _usernameAPI = value; } + set passwAPI(String value) { _passwAPI = value; } + + //constructors - User(this.usernameDafl, this.passwDafl); + User(this._usernameDafl, this._passwDafl); + + User.name(this._usernameDafl); + + User.fromDatabase(this._idDafl, this._usernameDafl); - User.name(this.usernameDafl); - User.fromDatabase(this.idDafl, this.usernameDafl); //lists Set likedUsers={}; @@ -38,6 +59,10 @@ class User{ Music('Paradis','Sopico','https://cdns-images.dzcdn.net/images/cover/17a9747927ac3e5ea56f92f635d9180c/500x500.jpg')].reversed.toList(); Map conversations={}; + + + + void addDiscovery(Music newmusic){ if(MyApp().controller?.currentUser?.Discovery == null){ diff --git a/Sources/dafl_project_flutter/lib/persistence/database_connexion.dart b/Sources/dafl_project_flutter/lib/persistence/database_connexion.dart index 4983ac3..d7fe1ed 100644 --- a/Sources/dafl_project_flutter/lib/persistence/database_connexion.dart +++ b/Sources/dafl_project_flutter/lib/persistence/database_connexion.dart @@ -21,13 +21,13 @@ class DatabaseConnexion{ static String? _psqlDataBase; + + // Read the database connection identifiers in a file static Future _loadLogs() async{ try{ final _loadedData = await rootBundle.loadString(filePath); - print('appel de -readLogs'); - final _logs = LineSplitter.split(_loadedData).toList(); _psqlUser = _logs[0]; @@ -42,14 +42,19 @@ class DatabaseConnexion{ - //Initialise connexion to the database + //Initialise and open a connection to the database static Future initConnexion() async{ if(_psqlHost == null || _psqlPswd == null || _psqlUser == null || _psqlDataBase == null){ await _loadLogs(); } - var uri = 'postgres://$_psqlUser:$_psqlPswd@$_psqlHost:5442/$_psqlDataBase'; + try{ + var uri = 'postgres://$_psqlUser:$_psqlPswd@$_psqlHost:5442/$_psqlDataBase'; - return connect(uri); + return connect(uri); + } + catch(e){ + throw Exception('Connection to database : IMPOSSIBLE'); + } } } \ No newline at end of file diff --git a/Sources/dafl_project_flutter/lib/persistence/database_loader.dart b/Sources/dafl_project_flutter/lib/persistence/database_loader.dart index 94416d4..a258774 100644 --- a/Sources/dafl_project_flutter/lib/persistence/database_loader.dart +++ b/Sources/dafl_project_flutter/lib/persistence/database_loader.dart @@ -6,15 +6,23 @@ import 'database_connexion.dart'; class DatabaseLoader extends Loader{ + + // Load an user from database @override - Future load(String? username, String? password) async { + Future load(String username, String password) async { + User? userToReturn = null; final connection = await DatabaseConnexion.initConnexion(); connection.query('select * from utilisateur where username = @username AND password = @password', {'username': username, 'password': password}).toList() .then((result) { - print(result); }); - } + if(result.isNotEmpty){ + userToReturn = User(username, password); + } + }).whenComplete(() { + connection.close();}); + return userToReturn; + } } diff --git a/Sources/dafl_project_flutter/lib/persistence/database_saver.dart b/Sources/dafl_project_flutter/lib/persistence/database_saver.dart index e900980..d8385a5 100644 --- a/Sources/dafl_project_flutter/lib/persistence/database_saver.dart +++ b/Sources/dafl_project_flutter/lib/persistence/database_saver.dart @@ -5,7 +5,6 @@ import '../model/user.dart'; class DatabaseSaver extends Saver{ - DatabaseConnexion dbConnexion = DatabaseConnexion(); @override void save(User userToSave) async{ diff --git a/Sources/dafl_project_flutter/lib/persistence/database_searcher.dart b/Sources/dafl_project_flutter/lib/persistence/database_searcher.dart index 5bf2d40..af15b59 100644 --- a/Sources/dafl_project_flutter/lib/persistence/database_searcher.dart +++ b/Sources/dafl_project_flutter/lib/persistence/database_searcher.dart @@ -3,21 +3,23 @@ import 'package:dafl_project_flutter/persistence/database_connexion.dart'; import 'searcher.dart'; class DatabaseSearcher extends Searcher{ + Future searchUser(String? username, String? password) async { return true; } @override Future searchByUsername(String? username) async{ final connection = await DatabaseConnexion.initConnexion(); + bool isHere = true; connection.query('select * from utilisateur where username = $username').toList().then((rows) { if(rows.isEmpty){ - connection.close(); - return false; + isHere = false; } + }).whenComplete(() { + print('close'); }); - connection.close(); - return true; + return isHere; } -} \ No newline at end of file +} diff --git a/Sources/dafl_project_flutter/lib/persistence/loader.dart b/Sources/dafl_project_flutter/lib/persistence/loader.dart index cf64cee..43a6fde 100644 --- a/Sources/dafl_project_flutter/lib/persistence/loader.dart +++ b/Sources/dafl_project_flutter/lib/persistence/loader.dart @@ -3,5 +3,5 @@ import 'dart:async'; import '../model/user.dart'; abstract class Loader{ - Future load(String? username, String? password); + Future load(String username, String password); } \ No newline at end of file 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 7b29836..ea0700d 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 @@ -229,6 +229,7 @@ class _SignInPageState extends State { ); } + void checkInformations(String username,String password){ if(username ==""){ Notify(2, context); @@ -238,7 +239,7 @@ class _SignInPageState extends State { } else{ //MyApp().controller.load(userNameTextField.text, passwordTextField.text); - MyApp().controller.currentUser = User(userNameTextField.text, passwordTextField.text); + MyApp().controller.currentUser = User(username, password); Navigator.of(context).push( PageTransition( type: PageTransitionType.fade, 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 af4f2d4..70b9afb 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 @@ -298,15 +298,17 @@ class _SignUpPageState extends State { ); } - Future checkInformations(String username,String password, String confirmPassword) async { - if(username ==""){ + + Future checkInformations(String username, String password, String confirmPassword) async { + + if(username == ""){ Notify(2, context); } else if(! await MyApp().controller.searchByUsername(username)){ Notify(0, context); } - /* - else if(password =="" || confirmPassword == ""){ + + if(password == "" || confirmPassword == ""){ Notify(4, context); } else if(password.length <8){ @@ -315,12 +317,9 @@ class _SignUpPageState extends State { else if(password != confirmPassword){ Notify(1, context); } - else if(password != confirmPassword){ - Notify(1, context); - } - */ else{ - MyApp().controller.save(User(userNameTextField.text, passwordConfirmTextField.text)); + MyApp().controller.save(User(username, password)); + Navigator.of(context).push( PageTransition( duration: Duration(milliseconds: 300), diff --git a/Sources/dafl_project_flutter/pubspec.yaml b/Sources/dafl_project_flutter/pubspec.yaml index 728a5f8..f06d927 100644 --- a/Sources/dafl_project_flutter/pubspec.yaml +++ b/Sources/dafl_project_flutter/pubspec.yaml @@ -89,6 +89,7 @@ flutter: assets: - assets/images/ - assets/fonts/ + - assets/ # An image asset can refer to one or more resolution-specific "variants", see # https://flutter.dev/assets-and-images/#resolution-aware From 92c46322ff2b4bae9384125999bb38a4a1c7fabc Mon Sep 17 00:00:00 2001 From: "audric.sabatier" Date: Sun, 13 Nov 2022 16:08:21 +0100 Subject: [PATCH 4/5] Fix : fix asynchronous bug in searchByUsername Function. (the return was before the asynchronous call) --- .../lib/controller/controller.dart | 16 ++++++++------ .../dafl_project_flutter/lib/model/user.dart | 6 ++--- .../lib/model/user_creator.dart | 8 ------- .../lib/persistence/.logs/.gitkeep | 0 .../lib/persistence/.logs/logs.txt | 5 ----- .../lib/persistence/database_saver.dart | 8 ++++++- .../lib/persistence/database_searcher.dart | 22 +++++++++---------- .../lib/views/pages/sign_in/p_sign_in.dart | 1 - .../lib/views/pages/sign_up/p_sign_up.dart | 1 - 9 files changed, 29 insertions(+), 38 deletions(-) delete mode 100644 Sources/dafl_project_flutter/lib/model/user_creator.dart delete mode 100644 Sources/dafl_project_flutter/lib/persistence/.logs/.gitkeep delete mode 100644 Sources/dafl_project_flutter/lib/persistence/.logs/logs.txt diff --git a/Sources/dafl_project_flutter/lib/controller/controller.dart b/Sources/dafl_project_flutter/lib/controller/controller.dart index 835b87a..7a9ea57 100644 --- a/Sources/dafl_project_flutter/lib/controller/controller.dart +++ b/Sources/dafl_project_flutter/lib/controller/controller.dart @@ -12,12 +12,14 @@ import '../persistence/searcher.dart'; class Controller{ static Controller? _this; - static Saver? saver = DatabaseSaver(); - static Loader? loader = DatabaseLoader(); + static Saver saver = DatabaseSaver(); + static Loader loader = DatabaseLoader(); static Searcher _searcher = DatabaseSearcher(); + User currentUser = User("", ""); + factory Controller(){ if (_this == null) _this = Controller._(); return _this!; @@ -26,11 +28,11 @@ class Controller{ Controller._(); void save(User userToSave){ - saver?.save(userToSave); + saver.save(userToSave); } void load(String username, String password) async{ - currentUser = await loader?.load(username, password) as User; + currentUser = await loader.load(username, password) as User; } User createUser(String username, String password){ @@ -43,16 +45,16 @@ class Controller{ void changeUsernameCourant(String newName){ if(newName !=null){ - this.currentUser?.usernameDafl = newName; + this.currentUser.usernameDafl = newName; } } void changePasswordCourant(String newPass){ if(newPass !=null){ - this.currentUser?.passwDafl = newPass; + this.currentUser.passwDafl = newPass; } } Future searchByUsername(String username) async{ - return _searcher.searchByUsername(username); + return await _searcher.searchByUsername(username); } } diff --git a/Sources/dafl_project_flutter/lib/model/user.dart b/Sources/dafl_project_flutter/lib/model/user.dart index 22bcb95..864f018 100644 --- a/Sources/dafl_project_flutter/lib/model/user.dart +++ b/Sources/dafl_project_flutter/lib/model/user.dart @@ -61,14 +61,12 @@ class User{ - - void addDiscovery(Music newmusic){ - if(MyApp().controller?.currentUser?.Discovery == null){ + if(MyApp().controller.currentUser.Discovery == null){ } else{ - MyApp().controller.currentUser?.Discovery.add(newmusic); + MyApp().controller.currentUser.Discovery.add(newmusic); } diff --git a/Sources/dafl_project_flutter/lib/model/user_creator.dart b/Sources/dafl_project_flutter/lib/model/user_creator.dart deleted file mode 100644 index 07029e6..0000000 --- a/Sources/dafl_project_flutter/lib/model/user_creator.dart +++ /dev/null @@ -1,8 +0,0 @@ -import 'user.dart'; - -class UserCreator{ - - User? createUser(int id, String username){ - - } -} \ No newline at end of file diff --git a/Sources/dafl_project_flutter/lib/persistence/.logs/.gitkeep b/Sources/dafl_project_flutter/lib/persistence/.logs/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/Sources/dafl_project_flutter/lib/persistence/.logs/logs.txt b/Sources/dafl_project_flutter/lib/persistence/.logs/logs.txt deleted file mode 100644 index f589036..0000000 --- a/Sources/dafl_project_flutter/lib/persistence/.logs/logs.txt +++ /dev/null @@ -1,5 +0,0 @@ -postgres -mdpDaflBd -89.83.54.48 -BD-DaflMusic - diff --git a/Sources/dafl_project_flutter/lib/persistence/database_saver.dart b/Sources/dafl_project_flutter/lib/persistence/database_saver.dart index d8385a5..25ab875 100644 --- a/Sources/dafl_project_flutter/lib/persistence/database_saver.dart +++ b/Sources/dafl_project_flutter/lib/persistence/database_saver.dart @@ -6,6 +6,8 @@ import '../model/user.dart'; class DatabaseSaver extends Saver{ + + // Save user in the database @override void save(User userToSave) async{ final connection = await DatabaseConnexion.initConnexion(); @@ -13,7 +15,11 @@ class DatabaseSaver extends Saver{ connection.execute('insert into utilisateur (username, password) values (@username, @password)', { 'id' : '', 'username': userToSave.usernameDafl, - 'password' : userToSave.passwDafl}).then((_) {}); + 'password' : userToSave.passwDafl}) + + .whenComplete(() { + connection.close(); + }); } } \ No newline at end of file diff --git a/Sources/dafl_project_flutter/lib/persistence/database_searcher.dart b/Sources/dafl_project_flutter/lib/persistence/database_searcher.dart index af15b59..612b19b 100644 --- a/Sources/dafl_project_flutter/lib/persistence/database_searcher.dart +++ b/Sources/dafl_project_flutter/lib/persistence/database_searcher.dart @@ -2,24 +2,24 @@ import 'package:dafl_project_flutter/persistence/database_connexion.dart'; import 'searcher.dart'; -class DatabaseSearcher extends Searcher{ + +class DatabaseSearcher extends Searcher{ Future searchUser(String? username, String? password) async { return true; } + + // Search an user in the database by username @override - Future searchByUsername(String? username) async{ + Future searchByUsername(String? usernameToSearch) async{ final connection = await DatabaseConnexion.initConnexion(); - bool isHere = true; - connection.query('select * from utilisateur where username = $username').toList().then((rows) { - if(rows.isEmpty){ - isHere = false; - } - }).whenComplete(() { - print('close'); - }); + bool queryResult = await connection.query('select * from utilisateur where username = @username',{ 'username' : usernameToSearch}) + .toList() + .then((rows) { return rows.isEmpty; }); + - return isHere; + return queryResult; } + } 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 ea0700d..958f632 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 @@ -1,6 +1,5 @@ import 'dart:ui'; import 'package:dafl_project_flutter/model/user.dart'; -import 'package:dafl_project_flutter/views/pages/main/p_main.dart'; import 'package:flutter/material.dart'; import 'package:page_transition/page_transition.dart'; import '../../../main.dart'; 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 70b9afb..569336b 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 @@ -307,7 +307,6 @@ class _SignUpPageState extends State { else if(! await MyApp().controller.searchByUsername(username)){ Notify(0, context); } - if(password == "" || confirmPassword == ""){ Notify(4, context); } From b1b1f507f286cd58f132cac72d6c7c931bc81e5b Mon Sep 17 00:00:00 2001 From: "audric.sabatier" Date: Sun, 13 Nov 2022 17:08:55 +0100 Subject: [PATCH 5/5] ADD : User login (password verification + username) and notification if there is a problem --- .../lib/controller/controller.dart | 4 +-- .../dafl_project_flutter/lib/model/user.dart | 2 +- .../lib/persistence/database_loader.dart | 13 +++++----- .../lib/persistence/database_searcher.dart | 3 ++- .../lib/persistence/loader.dart | 2 +- .../lib/views/pages/sign_in/p_sign_in.dart | 25 ++++++++++++------- .../lib/views/pages/sign_up/p_sign_up.dart | 1 - 7 files changed, 28 insertions(+), 22 deletions(-) diff --git a/Sources/dafl_project_flutter/lib/controller/controller.dart b/Sources/dafl_project_flutter/lib/controller/controller.dart index 7a9ea57..7da6b68 100644 --- a/Sources/dafl_project_flutter/lib/controller/controller.dart +++ b/Sources/dafl_project_flutter/lib/controller/controller.dart @@ -31,8 +31,8 @@ class Controller{ saver.save(userToSave); } - void load(String username, String password) async{ - currentUser = await loader.load(username, password) as User; + Future load(String username, String password) async{ + changeCurrentUser(await loader.load(username, password)); } User createUser(String username, String password){ diff --git a/Sources/dafl_project_flutter/lib/model/user.dart b/Sources/dafl_project_flutter/lib/model/user.dart index 864f018..0509730 100644 --- a/Sources/dafl_project_flutter/lib/model/user.dart +++ b/Sources/dafl_project_flutter/lib/model/user.dart @@ -92,5 +92,5 @@ class User{ } @override - String toString() => "$usernameDafl ($idDafl)"; + String toString() => "$usernameDafl ($passwDafl)"; } \ No newline at end of file diff --git a/Sources/dafl_project_flutter/lib/persistence/database_loader.dart b/Sources/dafl_project_flutter/lib/persistence/database_loader.dart index a258774..4c3bc53 100644 --- a/Sources/dafl_project_flutter/lib/persistence/database_loader.dart +++ b/Sources/dafl_project_flutter/lib/persistence/database_loader.dart @@ -9,20 +9,19 @@ class DatabaseLoader extends Loader{ // Load an user from database @override - Future load(String username, String password) async { - User? userToReturn = null; + Future load(String username, String password) async { final connection = await DatabaseConnexion.initConnexion(); - connection.query('select * from utilisateur where username = @username AND password = @password', + var queryResult = await connection.query('select * from utilisateur where username = @username AND password = @password', {'username': username, 'password': password}).toList() .then((result) { - if(result.isNotEmpty){ - userToReturn = User(username, password); - } + print(result); + if(result.isNotEmpty) return User(username, password); + else return User("", ""); }).whenComplete(() { connection.close();}); - return userToReturn; + return queryResult; } } diff --git a/Sources/dafl_project_flutter/lib/persistence/database_searcher.dart b/Sources/dafl_project_flutter/lib/persistence/database_searcher.dart index 612b19b..a01721b 100644 --- a/Sources/dafl_project_flutter/lib/persistence/database_searcher.dart +++ b/Sources/dafl_project_flutter/lib/persistence/database_searcher.dart @@ -16,7 +16,8 @@ class DatabaseSearcher extends Searcher{ bool queryResult = await connection.query('select * from utilisateur where username = @username',{ 'username' : usernameToSearch}) .toList() - .then((rows) { return rows.isEmpty; }); + .then((rows) { return rows.isEmpty; }).whenComplete(() { + connection.close();}); return queryResult; diff --git a/Sources/dafl_project_flutter/lib/persistence/loader.dart b/Sources/dafl_project_flutter/lib/persistence/loader.dart index 43a6fde..2adf991 100644 --- a/Sources/dafl_project_flutter/lib/persistence/loader.dart +++ b/Sources/dafl_project_flutter/lib/persistence/loader.dart @@ -3,5 +3,5 @@ import 'dart:async'; import '../model/user.dart'; abstract class Loader{ - Future load(String username, String password); + Future load(String username, String password); } \ No newline at end of file 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 958f632..733c779 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 @@ -229,7 +229,7 @@ class _SignInPageState extends State { } - void checkInformations(String username,String password){ + void checkInformations(String username,String password) async{ if(username ==""){ Notify(2, context); } @@ -237,14 +237,21 @@ class _SignInPageState extends State { Notify(4, context); } else{ - //MyApp().controller.load(userNameTextField.text, passwordTextField.text); - MyApp().controller.currentUser = User(username, password); - Navigator.of(context).push( - PageTransition( - type: PageTransitionType.fade, - childCurrent: widget, - child: Splash()), - ); + await MyApp().controller.load(userNameTextField.text, passwordTextField.text); + + if(MyApp().controller.currentUser.usernameDafl != ""){ + Navigator.of(context).push( + PageTransition( + type: PageTransitionType.fade, + childCurrent: widget, + child: Splash()), + ); + } + else{ + Notify(2, context); + } + + } } 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 569336b..800c8c0 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 @@ -300,7 +300,6 @@ class _SignUpPageState extends State { Future checkInformations(String username, String password, String confirmPassword) async { - if(username == ""){ Notify(2, context); }