feat(frontend): connect borrow flow to api

This commit is contained in:
SaidSoighiri94
2026-07-07 13:22:53 +02:00
parent 27171562de
commit 7aba1a930d
7 changed files with 180 additions and 21 deletions
+25
View File
@@ -37,6 +37,31 @@ class ApiClient {
throw ApiException(_messageErreur(reponse));
}
static Future<dynamic> post(String chemin, Map<String, dynamic> body) async {
final uri = Uri.parse("$_baseUrl$chemin");
late final http.Response reponse;
try {
reponse = await http.post(
uri,
headers: const {
"content-type": "application/json",
"x-user-email": _utilisateurEmail,
},
body: jsonEncode(body),
);
} catch (_) {
throw const ApiException(
"Impossible de joindre le serveur. Vérifiez que le backend est démarré.",
);
}
if (reponse.statusCode >= 200 && reponse.statusCode < 300) {
return jsonDecode(reponse.body);
}
throw ApiException(_messageErreur(reponse));
}
static String _messageErreur(http.Response reponse) {
try {
final corps = jsonDecode(reponse.body);
@@ -0,0 +1,26 @@
import "../models/emprunt_response.dart";
import "api_client.dart";
/// Appels liés aux emprunts.
class EmpruntService {
EmpruntService._();
static const int posteEmpruntId = 1;
static Future<EmpruntResponse> creerEmprunt({
required int materielId,
required String commentaireDepart,
required List<Map<String, dynamic>> elements,
}) async {
final reponse = await ApiClient.post("/emprunts", {
"materielId": materielId,
"posteEmpruntId": posteEmpruntId,
"modeIdentification": "MAIL_ENSUP",
if (commentaireDepart.trim().isNotEmpty) "commentaireDepart": commentaireDepart.trim(),
"elements": elements,
});
final data = (reponse as Map<String, dynamic>)["data"] as Map<String, dynamic>;
return EmpruntResponse.fromJson(data);
}
}