diff --git a/lib/Modele/Api/i_data_strategy.dart b/lib/Modele/Api/i_data_strategy.dart index aff5893..e7c7286 100644 --- a/lib/Modele/Api/i_data_strategy.dart +++ b/lib/Modele/Api/i_data_strategy.dart @@ -1,32 +1,37 @@ import 'dart:io'; -import 'package:smartfit_app_mobile/Modele/user.dart'; +import 'package:tuple/tuple.dart'; abstract class IDataStrategy { // Create user - Future postUser(User user); + Future postUser(String email, String hash, String username); // Delete user Future deleteUser(String token); // Get Token validate - Future getToken(String uuid, String passwordHash); + Future connexion(String email, String hash); // Get all files for user - Future getFiles(String token); + //Future getFiles(String token); // Upload file on BDD - Future uploadFile(String token, File file); + Future> uploadFile(String token, File file); // Get one file by id - Future getFile(String token, String idFile); + Future> getFile(String token, String fileUuid); // Delete one file on BDD - Future deleteFile(String token, String idFile); + //Future deleteFile(String token, String idFile); + /* -> Modification attribut // Update email Future updateEmail(String token, String email); // Update username Future updateUsername(String token, String username); + */ + + Future modifAttribut( + String token, String nameAttribut, String newValue); } diff --git a/lib/Modele/Api/request_api.dart b/lib/Modele/Api/request_api.dart index f71aece..b35bfb5 100644 --- a/lib/Modele/Api/request_api.dart +++ b/lib/Modele/Api/request_api.dart @@ -3,15 +3,18 @@ import 'dart:io'; import 'package:smartfit_app_mobile/Modele/Api/i_data_strategy.dart'; import 'package:http/http.dart' as http; -import 'package:smartfit_app_mobile/Modele/user.dart'; +import 'package:tuple/tuple.dart'; class RequestApi extends IDataStrategy { // Faire attention au URL - String urlApi = ""; + String urlApi = + "https://codefirst.iut.uca.fr/containers/SmartFit-smartfit_api"; @override - Future getFile(String token, String idFile) async { - final response = await http.get(Uri.parse('$urlApi/$token/files/$idFile')); + Future> getFile(String token, String fileUuid) async { + final response = await http.get(Uri.parse('$urlApi/user/files/$fileUuid'), + headers: {'Authorization': token}); + if (response.statusCode == 200) { /*return Classe.fromJson(jsonDecode(response.body) as Map);*/ throw UnimplementedError(); @@ -32,18 +35,25 @@ class RequestApi extends IDataStrategy { } @override - Future deleteUser(String token) async { - final response = await http.delete(Uri.parse('$urlApi/$token')); + Future deleteUser(String token) async { + final response = await http.delete(Uri.parse('$urlApi/user'), + headers: {'Authorization': token}); if (response.statusCode == 200) { - return true; - } else { - return false; + return const Tuple2(true, "Successful"); + } else if (response.statusCode == 401) { + return const Tuple2( + false, "401 UNAUTHORIZED - Mauvais ou pas de token"); + } else if (response.statusCode == 404) { + return const Tuple2( + false, "404 NOT FOUND - Pas de compte lié"); } + return const Tuple2(false, "Fail"); } @override Future getFiles(String token) async { final response = await http.get(Uri.parse('$urlApi/$token/files')); + if (response.statusCode == 200) { /*return Classe.fromJson(jsonDecode(response.body) as Map);*/ throw UnimplementedError(); @@ -53,31 +63,44 @@ class RequestApi extends IDataStrategy { } @override - Future getToken(String uuid, String passwordHash) async { + Future connexion(String email, String hash) async { final response = - await http.get(Uri.parse('$urlApi/$uuid/$passwordHash/token')); + await http.get(Uri.parse('$urlApi/user/login/$email/$hash')); if (response.statusCode == 200) { - /*return Classe.fromJson(jsonDecode(response.body) as Map);*/ - throw UnimplementedError(); - } else { - throw UnimplementedError(); + Map json = + jsonDecode(response.body) as Map; + return Tuple2(true, json['token']!); + } else if (response.statusCode == 401) { + return const Tuple2(false, "UNAUTHORIZED"); } + return const Tuple2(false, "Fail"); } + /* @override - Future postUser(User user) async { - final response = await http.post(Uri.parse(urlApi), - headers: { - 'Content-Type': 'application/json; charset=UTF-8', - }, - body: jsonEncode({'user': user})); + Future postUser(String email, String hash, String username) async { + final response = + await http.post(Uri.parse('$urlApi/user'), headers: { + 'Content-Type': 'application/json; charset=UTF-8', + }, body: { + "email": "'$email'", + "hash": "'$hash'", + "username": "'$username'" + }); if (response.statusCode == 200) { - return true; - } else { - return false; + return const Tuple2(true, "Successful"); } - } + if (response.statusCode == 400) { + return const Tuple2(false, "400 BAD REQUEST - Json mal formaté"); + } + if (response.statusCode == 409) { + return const Tuple2( + false, "409 CONFLICT - Déja un compte avec cet email"); + } + return const Tuple2(false, "Fail"); + }*/ + /* @override Future updateEmail(String token, String email) async { final response = await http.put(Uri.parse('$urlApi/$token/email'), @@ -106,19 +129,63 @@ class RequestApi extends IDataStrategy { } else { return false; } - } + }*/ @override - Future uploadFile(String token, File file) async { - final response = await http.post(Uri.parse('$urlApi/$token/UploadFile'), + Future modifAttribut( + String token, String nameAttribut, String newValue) async { + final response = await http.put(Uri.parse('$urlApi/user/$nameAttribut'), headers: { 'Content-Type': 'application/json; charset=UTF-8', + 'Authorization': token }, - body: jsonEncode({'file': file})); + body: jsonEncode({nameAttribut: newValue})); + if (response.statusCode == 200) { - return true; + return const Tuple2(true, "Successful"); + } + if (response.statusCode == 400) { + return const Tuple2(false, "400 - BAD REQUEST"); + } + if (response.statusCode == 401) { + return const Tuple2(false, "400 - UNAUTHORIZED"); } else { - return false; + return const Tuple2(false, "Fail"); } } + + @override + Future> uploadFile(String token, File file) async { + final uri = Uri.parse('$urlApi/user/files'); + Map headers = {'Authorization': token}; + + var request = http.MultipartRequest('POST', uri); + final httpImage = http.MultipartFile.fromBytes( + 'file_FIT', await file.readAsBytes(), + filename: file.path.split('/').last); + request.files.add(httpImage); + request.headers.addAll(headers); + + final response = await request.send(); + + if (response.statusCode == 200) { + return const Tuple2(true, "Successful"); + } + if (response.statusCode == 400) { + return const Tuple2(false, "400 - BAD REQUEST"); + } + if (response.statusCode == 401) { + return const Tuple2(false, "401 - UNAUTHORIZED"); + } + if (response.statusCode == 409) { + return const Tuple2(false, "409 - CONFLICT"); + } + return const Tuple2(false, "Fail "); + } + + @override + Future postUser(String email, String hash, String username) { + // TODO: implement postUser + throw UnimplementedError(); + } } diff --git a/lib/Modele/manager_file.dart b/lib/Modele/manager_file.dart index fa13c6e..dd3ea85 100644 --- a/lib/Modele/manager_file.dart +++ b/lib/Modele/manager_file.dart @@ -1,4 +1,5 @@ import 'dart:convert'; +import 'dart:ffi'; import 'dart:io'; import 'package:csv/csv.dart'; import 'package:fit_tool/fit_tool.dart'; @@ -102,7 +103,8 @@ class ManagerFile { firtTimeStamp = ligne[4]; } //result.add([(ligne[4] - firtTimeStamp) ~/ 100, ligne[7].toInt()]); - result.add(FlSpot((ligne[4] - firtTimeStamp) ~/ 100, ligne[7])); + result + .add(FlSpot((ligne[4] - firtTimeStamp) / 100, ligne[7].toDouble())); } } return result; @@ -118,27 +120,27 @@ class ManagerFile { firtTimeStamp = ligne[4]; } //result.add([(ligne[4] - firtTimeStamp) ~/ 100, ligne[13].toInt()]); - result.add(FlSpot((ligne[4] - firtTimeStamp) ~/ 100, ligne[13])); + result.add( + FlSpot((ligne[4] - firtTimeStamp) / 100, ligne[13].toDouble())); } } return result; } - int getDistance(ActivityOfUser activity) { - int result = 0; + double getDistance(ActivityOfUser activity) { + double result = 0.0; for (int i = activity.contentActivity.length - 1; i >= 0; i--) { if (activity.contentActivity[i].length >= 8 && activity.contentActivity[i][0] == "Data" && activity.contentActivity[i][6] == "distance") { if (activity.contentActivity[i][7] > result) { - result = activity.contentActivity[i][7].toInt(); + result = activity.contentActivity[i][7].toDouble(); } } } return result; } - /* En Cours List getSpeedWithTime(ActivityOfUser activityOfUser) { List result = List.empty(growable: true); int firtTimeStamp = 0; @@ -148,11 +150,17 @@ class ManagerFile { if (firtTimeStamp == 0) { firtTimeStamp = ligne[4]; } - result - .add(FlSpot((ligne[4] - firtTimeStamp) ~/ 100, ligne[19].toInt())); - //result.add([(ligne[4] - firtTimeStamp) ~/ 100, ligne[19].toInt()]); + result.add( + FlSpot((ligne[4] - firtTimeStamp) / 100, ligne[19].toDouble())); + } + if (ligne[0] == "Data" && ligne[1] == 2) { + if (firtTimeStamp == 0) { + firtTimeStamp = ligne[4]; + } + result.add( + FlSpot((ligne[4] - firtTimeStamp) / 100, ligne[25].toDouble())); } } return result; - }*/ + } } diff --git a/pubspec.yaml b/pubspec.yaml index 4cb6e21..affc2bd 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -52,6 +52,7 @@ dependencies: flutter_form_builder: ^9.1.1 http: ^1.1.0 provider: ^6.0.5 + tuple: ^2.0.2 dev_dependencies: flutter_test: