diff --git a/.idea/libraries/Dart_Packages.xml b/.idea/libraries/Dart_Packages.xml deleted file mode 100644 index 279019c..0000000 --- a/.idea/libraries/Dart_Packages.xml +++ /dev/null @@ -1,596 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/libraries/Dart_SDK.xml b/.idea/libraries/Dart_SDK.xml index b6e6985..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..7da6b68 100644 --- a/Sources/dafl_project_flutter/lib/controller/controller.dart +++ b/Sources/dafl_project_flutter/lib/controller/controller.dart @@ -1,19 +1,24 @@ 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{ 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("", ""); - User currentUser = User(null, null); factory Controller(){ if (_this == null) _this = Controller._(); @@ -23,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; + Future load(String username, String password) async{ + changeCurrentUser(await loader.load(username, password)); } User createUser(String username, String password){ @@ -40,12 +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 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 56a269f..7a16c58 100644 --- a/Sources/dafl_project_flutter/lib/model/user.dart +++ b/Sources/dafl_project_flutter/lib/model/user.dart @@ -8,19 +8,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={}; @@ -42,12 +63,14 @@ class User{ List Spots2= []; Map conversations={}; + + 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); } @@ -73,5 +96,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/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_connexion.dart b/Sources/dafl_project_flutter/lib/persistence/database_connexion.dart index 7635688..d7fe1ed 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,16 +8,23 @@ 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); @@ -35,12 +41,20 @@ class DatabaseConnexion{ } - //Initialise connexion to the database - Future initConnexion() async{ - await _loadLogs(); - var uri = 'postgres://$_psqlUser:$_psqlPswd@$_psqlHost:5442/$_psqlDataBase'; + //Initialise and open a connection to the database + static Future initConnexion() async{ + if(_psqlHost == null || _psqlPswd == null || _psqlUser == null || _psqlDataBase == null){ + await _loadLogs(); + } - return connect(uri); + try{ + var uri = 'postgres://$_psqlUser:$_psqlPswd@$_psqlHost:5442/$_psqlDataBase'; + + 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 a632976..4c3bc53 100644 --- a/Sources/dafl_project_flutter/lib/persistence/database_loader.dart +++ b/Sources/dafl_project_flutter/lib/persistence/database_loader.dart @@ -5,17 +5,23 @@ import '../model/user.dart'; import 'database_connexion.dart'; class DatabaseLoader extends Loader{ - DatabaseConnexion dbConnexion = DatabaseConnexion(); + // Load an user from database @override - Future load(String? username, String? password) async { - final connection = await dbConnexion.initConnexion(); + 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) { - print(result); }); + print(result); + if(result.isNotEmpty) return User(username, password); + else return User("", ""); + }).whenComplete(() { + connection.close();}); + + return queryResult; } } diff --git a/Sources/dafl_project_flutter/lib/persistence/database_saver.dart b/Sources/dafl_project_flutter/lib/persistence/database_saver.dart index 53d70fe..25ab875 100644 --- a/Sources/dafl_project_flutter/lib/persistence/database_saver.dart +++ b/Sources/dafl_project_flutter/lib/persistence/database_saver.dart @@ -5,15 +5,21 @@ import '../model/user.dart'; class DatabaseSaver extends Saver{ - DatabaseConnexion dbConnexion = DatabaseConnexion(); + + // Save user in the database @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((_) {}); + 'password' : userToSave.passwDafl}) + + .whenComplete(() { + connection.close(); + }); } + } \ No newline at end of file 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..a01721b --- /dev/null +++ b/Sources/dafl_project_flutter/lib/persistence/database_searcher.dart @@ -0,0 +1,26 @@ +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; } + + + + // Search an user in the database by username + @override + Future searchByUsername(String? usernameToSearch) async{ + final connection = await DatabaseConnexion.initConnexion(); + + bool queryResult = await connection.query('select * from utilisateur where username = @username',{ 'username' : usernameToSearch}) + .toList() + .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 cf64cee..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/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..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 @@ -298,11 +298,15 @@ class _SignUpPageState extends State { ); } - void checkInformations(String username,String password, String confirmPassword){ - if(username ==""){ + + Future checkInformations(String username, String password, String confirmPassword) async { + if(username == ""){ Notify(2, context); } - else if(password =="" || confirmPassword == ""){ + else if(! await MyApp().controller.searchByUsername(username)){ + Notify(0, context); + } + if(password == "" || confirmPassword == ""){ Notify(4, context); } else if(password.length <8){ @@ -311,11 +315,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 12d9cee..78bb0ff 100644 --- a/Sources/dafl_project_flutter/pubspec.yaml +++ b/Sources/dafl_project_flutter/pubspec.yaml @@ -91,6 +91,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 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