From f1321871c978cf1453f8047160c7ba10ba69a904 Mon Sep 17 00:00:00 2001 From: Mikkel Troels Kongsted Date: Mon, 10 Mar 2025 14:57:24 +0100 Subject: [PATCH] server logic frontend --- mobile/lib/main.dart | 1 + mobile/lib/repos/product.dart | 18 ++++----- mobile/lib/repos/session.dart | 0 mobile/lib/server/client.dart | 70 +++++++++++++++++++++++++++++++++++ mobile/lib/server/server.dart | 29 +++++++++++++++ 5 files changed, 108 insertions(+), 10 deletions(-) create mode 100644 mobile/lib/repos/session.dart create mode 100644 mobile/lib/server/client.dart create mode 100644 mobile/lib/server/server.dart diff --git a/mobile/lib/main.dart b/mobile/lib/main.dart index d293389..06ea576 100644 --- a/mobile/lib/main.dart +++ b/mobile/lib/main.dart @@ -16,6 +16,7 @@ void main() { } class MyApp extends StatelessWidget { + final apiUrl = "10.135.51.114:8080/api"; const MyApp({super.key}); @override diff --git a/mobile/lib/repos/product.dart b/mobile/lib/repos/product.dart index 5047b47..ce6c842 100644 --- a/mobile/lib/repos/product.dart +++ b/mobile/lib/repos/product.dart @@ -1,10 +1,9 @@ -import 'dart:convert'; - import 'package:flutter/material.dart'; -import 'package:http/http.dart' as http; import 'package:mobile/models/coordinate.dart'; import 'package:mobile/models/product.dart'; import 'package:mobile/results.dart'; +import 'package:mobile/server/client.dart'; +import 'package:mobile/server/server.dart'; class ProductRepo extends ChangeNotifier { int _nextId = 0; @@ -141,7 +140,7 @@ class ProductRepo extends ChangeNotifier { } class ProductRepoByServer extends ChangeNotifier { - String apiUrl = "http://127.0.0.1:8080/products.json"; + final client = BackendServer(); List products = []; String query = ""; ProductRepoByServer() { @@ -149,12 +148,11 @@ class ProductRepoByServer extends ChangeNotifier { } Future fetchProductsFromServer() async { - final res = await http.get( - Uri.parse(apiUrl), - ); - final productsJson = List>.from(jsonDecode(res.body)); - products = - productsJson.map(((product) => Product.fromJson(product))).toList(); + final res = client.allProducts(); + if (res is Error) { + return; + } + products = (res as Success>).data; notifyListeners(); } diff --git a/mobile/lib/repos/session.dart b/mobile/lib/repos/session.dart new file mode 100644 index 0000000..e69de29 diff --git a/mobile/lib/server/client.dart b/mobile/lib/server/client.dart new file mode 100644 index 0000000..6ade9eb --- /dev/null +++ b/mobile/lib/server/client.dart @@ -0,0 +1,70 @@ +import 'dart:convert'; + +import 'package:http/http.dart' as http; +import 'package:mobile/models/product.dart'; +import 'package:mobile/server/server.dart'; + +class BackendServer implements Server { + final _apiUrl = "10.135.51.114:8080/api"; + + Future _post( + {required String endpoint, required Map body}) async { + final encoded = json.encode(body); + return await http.post( + Uri.parse("$_apiUrl/$endpoint"), + body: encoded, + headers: {"Content-Type": "application/json"}, + ); + } + + @override + Future>> allProducts() async { + final res = await http + .get( + Uri.parse("$_apiUrl/products/all"), + ) + .then((res) => json.decode(res.body)); + if (res["ok"]) { + return Error(message: res["message"]); + } else { + return Success( + data: res.map(((product) => Product.fromJson(product))).toList()); + } + } + + @override + Future> register( + String name, + String email, + String password, + ) async { + final res = await _post( + endpoint: "users/register", + body: {"name": name, "email": email, "password": password}, + ).then((res) => json.decode(res.body)); + + if (res["ok"]) { + return Success(data: null); + } else { + return Error(message: res["message"]); + } + } + + @override + Future> login( + String name, + String email, + String password, + ) async { + final res = await _post( + endpoint: "auth/login", + body: {"email": email, "password": password}, + ).then((res) => json.decode(res.body)); + + if (res["ok"]) { + return Success(data: null); + } else { + return Error(message: res["message"]); + } + } +} diff --git a/mobile/lib/server/server.dart b/mobile/lib/server/server.dart new file mode 100644 index 0000000..42e6852 --- /dev/null +++ b/mobile/lib/server/server.dart @@ -0,0 +1,29 @@ +import 'package:mobile/models/product.dart'; + +abstract class Server { + Future>> allProducts(); + + Future> register( + String name, + String email, + String password, + ); + + Future> login( + String name, + String email, + String password, + ); +} + +sealed class Response {} + +class Success extends Response { + Data data; + Success({required this.data}); +} + +class Error extends Response { + String message; + Error({required this.message}); +}