From 76f1de27eccc502ee9eb91dfcd04bd6c594020b3 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Wed, 4 Dec 2024 23:01:06 +0100 Subject: [PATCH] add future event only --- covas_mobile/lib/pages/UpdateEventImage.dart | 234 ++++++++++--------- 1 file changed, 119 insertions(+), 115 deletions(-) diff --git a/covas_mobile/lib/pages/UpdateEventImage.dart b/covas_mobile/lib/pages/UpdateEventImage.dart index 25ac063..f405afa 100644 --- a/covas_mobile/lib/pages/UpdateEventImage.dart +++ b/covas_mobile/lib/pages/UpdateEventImage.dart @@ -137,7 +137,6 @@ class _UpdateeventImageState extends State } Future _updateEvent(BuildContext context) async { - var url = Uri.parse("${globals.api}/token"); var name = inputName.text; var place = inputGeo.text; var description = inputDesc.text; @@ -146,141 +145,146 @@ class _UpdateeventImageState extends State List.from(_stringOrgaController.getTags as List); var startDateFormat = formatDate(startDatepicker.text); + DateTime startDateCompare = DateTime.parse(startDateFormat); + DateTime dateNow = DateTime.now(); var endDateFormat = formatDate(endDatepicker.text); var startDate = "${startDateFormat}T${startTimepicker.text.replaceAll('-', ':')}"; var endDate = "${endDateFormat}T${endTimepicker.text.replaceAll('-', ':')}"; - print("start date : ${startDate}"); - print("end date : ${endDate}"); - SharedPreferences prefs = await SharedPreferences.getInstance(); - var accessToken = prefs.getString("access_token") ?? ""; + if (startDateCompare.isAfter(dateNow)) { + SharedPreferences prefs = await SharedPreferences.getInstance(); + var accessToken = prefs.getString("access_token") ?? ""; - if (accessToken.isNotEmpty) { - try { - await dotenv.load(); - final mapboxAccessToken = dotenv.env['MAPBOX_ACCESS_TOKEN'] ?? ''; - print("place non encoded : ${place}"); - final url = - 'https://api.mapbox.com/geocoding/v5/mapbox.places/${place}.json?access_token=${mapboxAccessToken}&types=poi,address,place'; - var encoded = Uri.encodeFull(url); - print("encoded : ${encoded}"); - final response = await http.get(Uri.parse(encoded)); + if (accessToken.isNotEmpty) { + try { + await dotenv.load(); + final mapboxAccessToken = dotenv.env['MAPBOX_ACCESS_TOKEN'] ?? ''; + print("place non encoded : ${place}"); + final url = + 'https://api.mapbox.com/geocoding/v5/mapbox.places/${place}.json?access_token=${mapboxAccessToken}&types=poi,address,place'; + var encoded = Uri.encodeFull(url); + print("encoded : ${encoded}"); + final response = await http.get(Uri.parse(encoded)); - if (response.statusCode == 200) { - final data = json.decode(response.body); - print("data : ${data}"); + if (response.statusCode == 200) { + final data = json.decode(response.body); + print("data : ${data}"); - if (data['features'].isNotEmpty) { - place = data['features'][0]['place_name']; - final coordinates = data['features'][0]['geometry']['coordinates']; - final longitude = coordinates[0]; // Longitude - final latitude = coordinates[1]; // Latitude + if (data['features'].isNotEmpty) { + place = data['features'][0]['place_name']; + final coordinates = + data['features'][0]['geometry']['coordinates']; + final longitude = coordinates[0]; // Longitude + final latitude = coordinates[1]; // Latitude - final params = { - 'expiration': '15552000', - 'key': dotenv.env["IMGBB_API_KEY"], - }; - print("Post Img"); - final urlPost = Uri.parse('https://api.imgbb.com/1/upload') - .replace(queryParameters: params); - File image = File(widget.imagePath); - Uint8List _bytes = await image.readAsBytes(); - String _base64String = base64.encode(_bytes); + final params = { + 'expiration': '15552000', + 'key': dotenv.env["IMGBB_API_KEY"], + }; + print("Post Img"); + final urlPost = Uri.parse('https://api.imgbb.com/1/upload') + .replace(queryParameters: params); + File image = File(widget.imagePath); + Uint8List _bytes = await image.readAsBytes(); + String _base64String = base64.encode(_bytes); - final req = http.MultipartRequest('POST', urlPost) - ..fields['image'] = _base64String; + final req = http.MultipartRequest('POST', urlPost) + ..fields['image'] = _base64String; - final stream = await req.send(); - final res = await http.Response.fromStream(stream); + final stream = await req.send(); + final res = await http.Response.fromStream(stream); - final status = res.statusCode; - print("code status imgbb ${status}"); - if (status == 200) { - var body = json.decode(utf8.decode(res.bodyBytes)); - String imgUrl = body["data"]["url"]; + final status = res.statusCode; + print("code status imgbb ${status}"); + if (status == 200) { + var body = json.decode(utf8.decode(res.bodyBytes)); + String imgUrl = body["data"]["url"]; - //String credentials = "${pseudo}:${password}"; - //Codec stringToBase64 = utf8.fuse(base64); - //String encoded = stringToBase64.encode(credentials); - var urlPut = Uri.parse("${globals.api}/events"); - var responsePut = await http.put(urlPut, - headers: { - HttpHeaders.cookieHeader: 'access_token=${accessToken}', - HttpHeaders.acceptHeader: - 'application/json, text/plain, */*', - HttpHeaders.contentTypeHeader: 'application/json' - }, - body: jsonEncode({ - 'name': name, - 'place': place, - 'start_date': startDate, - 'end_date': endDate, - 'organizers': organizers, - 'latitude': latitude, - 'longitude': longitude, - 'description': description, - "imgUrl": imgUrl, - "tags": tags - })); - print(responsePut.statusCode); - if ((responsePut.statusCode == 200) || - (responsePut.statusCode == 201)) { - showEventDialog(context, "Evenement ${name} ajoute"); - } else { - var text = ""; - switch (responsePut.statusCode) { - case 400: - { - text = "Requête mal construite"; - } - break; - case 406: - { - text = "Mot de passe incorrect"; - } - break; - case 404: - { - text = "Utilisateur inconnu"; - } - break; - case 403: - { - text = "Utilisateur desactive"; - } - break; - case 410: - { - text = "Token invalide"; - } - break; - case 500: - { - text = "Probleme interne du serveur"; - } - break; - default: - { - text = "Probleme d'authentification inconnu"; - } - break; + //String credentials = "${pseudo}:${password}"; + //Codec stringToBase64 = utf8.fuse(base64); + //String encoded = stringToBase64.encode(credentials); + var urlPut = Uri.parse("${globals.api}/events"); + var responsePut = await http.put(urlPut, + headers: { + HttpHeaders.cookieHeader: 'access_token=${accessToken}', + HttpHeaders.acceptHeader: + 'application/json, text/plain, */*', + HttpHeaders.contentTypeHeader: 'application/json' + }, + body: jsonEncode({ + 'name': name, + 'place': place, + 'start_date': startDate, + 'end_date': endDate, + 'organizers': organizers, + 'latitude': latitude, + 'longitude': longitude, + 'description': description, + "imgUrl": imgUrl, + "tags": tags + })); + print(responsePut.statusCode); + if ((responsePut.statusCode == 200) || + (responsePut.statusCode == 201)) { + showEventDialog(context, "Evenement ${name} ajoute"); + } else { + var text = ""; + switch (responsePut.statusCode) { + case 400: + { + text = "Requête mal construite"; + } + break; + case 406: + { + text = "Mot de passe incorrect"; + } + break; + case 404: + { + text = "Utilisateur inconnu"; + } + break; + case 403: + { + text = "Utilisateur desactive"; + } + break; + case 410: + { + text = "Token invalide"; + } + break; + case 500: + { + text = "Probleme interne du serveur"; + } + break; + default: + { + text = "Probleme d'authentification inconnu"; + } + break; + } + showErrorDialog(context, text); } - showErrorDialog(context, text); + } else { + print("imgbb error : ${status}"); } } else { - print("imgbb error : ${status}"); + showErrorDialog(context, "Aucune donnée geographique"); } } else { - showErrorDialog(context, "Aucune donnée geographique"); + showErrorDialog(context, "Mapbox non accessible"); } - } else { - showErrorDialog(context, "Mapbox non accessible"); + } catch (e) { + showErrorDialog(context, "${e}"); } - } catch (e) { - showErrorDialog(context, "${e}"); + } else { + showErrorDialog(context, "Champ vide"); } } else { - showErrorDialog(context, "Champ vide"); + showErrorDialog(context, "Evenement non futur"); } }