From 8726468c5506606ce321c3a33c07ad2a34fbe2e0 Mon Sep 17 00:00:00 2001 From: SimonFJ20 Date: Mon, 24 Mar 2025 11:14:16 +0100 Subject: [PATCH] fix app issues --- mobile/lib/pages/product_page.dart | 5 ++- mobile/lib/server/backend_server.dart | 45 +++++++++++++-------------- mobile/lib/server/server.dart | 2 ++ 3 files changed, 27 insertions(+), 25 deletions(-) diff --git a/mobile/lib/pages/product_page.dart b/mobile/lib/pages/product_page.dart index cee7dde..46a8680 100644 --- a/mobile/lib/pages/product_page.dart +++ b/mobile/lib/pages/product_page.dart @@ -55,7 +55,10 @@ class ProductPage extends StatelessWidget { child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ - image, + SizedBox( + height: 300, + child: image, + ), Text( product.name, style: Theme.of(context).textTheme.bodyLarge, diff --git a/mobile/lib/server/backend_server.dart b/mobile/lib/server/backend_server.dart index cff805a..4f68ba4 100644 --- a/mobile/lib/server/backend_server.dart +++ b/mobile/lib/server/backend_server.dart @@ -13,14 +13,14 @@ class BackendServer implements Server { final _apiUrl = "https://h4-sm.mercantec.tech/api"; Future> _postJson({ - required String endpoint, + required String path, required Body body, Map? headers, }) async { final encoded = json.encode(body); return Future>.sync(() { return http.post( - Uri.parse("$_apiUrl/$endpoint"), + Uri.parse("$_apiUrl$path"), body: encoded, headers: { "Content-Type": "application/json", @@ -40,12 +40,12 @@ class BackendServer implements Server { } Future> _getJson({ - required String endpoint, + required String path, Map? headers, }) async { return Future>.sync(() { return http.get( - Uri.parse("$_apiUrl/$endpoint"), + Uri.parse("$_apiUrl$path"), headers: { "Content-Type": "application/json", ...?headers, @@ -66,7 +66,7 @@ class BackendServer implements Server { @override Future, String>> allProducts() async { final res = await _getJson( - endpoint: "$_apiUrl/products/all", + path: "/products/all", ); return res.flatMap((body) { @@ -87,7 +87,7 @@ class BackendServer implements Server { String password, ) async { final res = await _postJson( - endpoint: "users/register", + path: "/users/register", body: {"name": name, "email": email, "password": password}, ); @@ -106,7 +106,7 @@ class BackendServer implements Server { String password, ) async { final res = (await _postJson( - endpoint: "sessions/login", + path: "/sessions/login", body: {"email": email, "password": password}, )); @@ -121,7 +121,7 @@ class BackendServer implements Server { @override Future> logout(String token) async { - final res = await _postJson(endpoint: "sessions/logout", body: {}); + final res = await _postJson(path: "sessions/logout", body: {}); return res.flatMap((body) { if (body["ok"]) { @@ -135,7 +135,7 @@ class BackendServer implements Server { @override Future> sessionUser(String token) async { final res = await _getJson( - endpoint: "$_apiUrl/sessions/user", + path: "/sessions/user", headers: {"Session-Token": token}, ); return res.flatMap((body) { @@ -150,17 +150,15 @@ class BackendServer implements Server { @override Future> purchaseCart( String token, List cartItems) async { - final res = await _postJson( - endpoint: "$_apiUrl/carts/purchase", - headers: {"Content-Type": "application/json", "Session-Token": token}, - body: json.encode({ - "items": cartItems - .map((cartItem) => { - "product_id": cartItem.product.id, - "amount": cartItem.amount - }) - .toList() - })); + final res = await _postJson(path: "/carts/purchase", headers: { + "Content-Type": "application/json", + "Session-Token": token + }, body: { + "items": cartItems + .map((cartItem) => + {"product_id": cartItem.product.id, "amount": cartItem.amount}) + .toList() + }); return res.flatMap((body) { if (body["ok"]) { @@ -173,8 +171,7 @@ class BackendServer implements Server { @override Future> addBalance(String token) async { - final res = - await _postJson(endpoint: "$_apiUrl/users/balance/add", headers: { + final res = await _postJson(path: "/users/balance/add", headers: { "Content-Type": "application/json", "Accept": "application/json", "Session-Token": token @@ -192,7 +189,7 @@ class BackendServer implements Server { @override Future, String>> allReceipts(String token) async { final res = await _getJson( - endpoint: "$_apiUrl/receipts/all", + path: "/receipts/all", headers: { "Content-Type": "application/json", "Accept": "application/json", @@ -215,7 +212,7 @@ class BackendServer implements Server { @override Future> oneReceipt(String token, int id) async { final res = await _getJson( - endpoint: "$_apiUrl/receipts/one?receipt_id=$id", + path: "/receipts/one?receipt_id=$id", headers: { "Content-Type": "application/json", "Accept": "application/json", diff --git a/mobile/lib/server/server.dart b/mobile/lib/server/server.dart index 5f9bcff..a934447 100644 --- a/mobile/lib/server/server.dart +++ b/mobile/lib/server/server.dart @@ -70,6 +70,8 @@ class ErrorMessages { switch (message) { case "insufficient funds": return "Du har desværre ikke nok penge på kontoen."; + case "bad request": + return "Fejl i kommunikation mellem app og server."; default: if (kDebugMode) {