mirror of
https://github.com/Mercantec-GHC/h4-projekt-gruppe-0-sm.git
synced 2025-04-27 16:24:07 +02:00
server logic frontend
This commit is contained in:
parent
71e6c48cd5
commit
f1321871c9
@ -16,6 +16,7 @@ void main() {
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
final apiUrl = "10.135.51.114:8080/api";
|
||||
const MyApp({super.key});
|
||||
|
||||
@override
|
||||
|
@ -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<Product> products = [];
|
||||
String query = "";
|
||||
ProductRepoByServer() {
|
||||
@ -149,12 +148,11 @@ class ProductRepoByServer extends ChangeNotifier {
|
||||
}
|
||||
|
||||
Future<void> fetchProductsFromServer() async {
|
||||
final res = await http.get(
|
||||
Uri.parse(apiUrl),
|
||||
);
|
||||
final productsJson = List<Map<String, dynamic>>.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<List<Product>>).data;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
|
0
mobile/lib/repos/session.dart
Normal file
0
mobile/lib/repos/session.dart
Normal file
70
mobile/lib/server/client.dart
Normal file
70
mobile/lib/server/client.dart
Normal file
@ -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<http.Response> _post(
|
||||
{required String endpoint, required Map<String, dynamic> body}) async {
|
||||
final encoded = json.encode(body);
|
||||
return await http.post(
|
||||
Uri.parse("$_apiUrl/$endpoint"),
|
||||
body: encoded,
|
||||
headers: {"Content-Type": "application/json"},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Response<List<Product>>> 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<Response<Null>> 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<Response<Null>> 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"]);
|
||||
}
|
||||
}
|
||||
}
|
29
mobile/lib/server/server.dart
Normal file
29
mobile/lib/server/server.dart
Normal file
@ -0,0 +1,29 @@
|
||||
import 'package:mobile/models/product.dart';
|
||||
|
||||
abstract class Server {
|
||||
Future<Response<List<Product>>> allProducts();
|
||||
|
||||
Future<Response<Null>> register(
|
||||
String name,
|
||||
String email,
|
||||
String password,
|
||||
);
|
||||
|
||||
Future<Response<Null>> login(
|
||||
String name,
|
||||
String email,
|
||||
String password,
|
||||
);
|
||||
}
|
||||
|
||||
sealed class Response<Data> {}
|
||||
|
||||
class Success<Data> extends Response<Data> {
|
||||
Data data;
|
||||
Success({required this.data});
|
||||
}
|
||||
|
||||
class Error<Data> extends Response<Data> {
|
||||
String message;
|
||||
Error({required this.message});
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user