add future event only

This commit is contained in:
Valentin CZERYBA 2024-12-04 23:01:06 +01:00
parent eef7e44999
commit 76f1de27ec

View File

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