diff --git a/mobile/lib/models/coordinate.dart b/mobile/lib/models/coordinate.dart index fd285ff..69e18d2 100644 --- a/mobile/lib/models/coordinate.dart +++ b/mobile/lib/models/coordinate.dart @@ -3,4 +3,8 @@ class Coordinate { final double y; Coordinate({required this.x, required this.y}); + + Coordinate.fromJson(Map json) + : x = json["x"], + y = json["y"]; } diff --git a/mobile/lib/server/backend_server.dart b/mobile/lib/server/backend_server.dart index 4f68ba4..6bddbfb 100644 --- a/mobile/lib/server/backend_server.dart +++ b/mobile/lib/server/backend_server.dart @@ -3,6 +3,7 @@ import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; import 'package:mobile/models/cart_item.dart'; +import 'package:mobile/models/coordinate.dart'; import 'package:mobile/models/product.dart'; import 'package:mobile/models/receipt.dart'; import 'package:mobile/models/user.dart'; @@ -232,4 +233,26 @@ class BackendServer implements Server { Image productImage(int productId) { return Image.network("$_apiUrl/products/image.png?product_id=$productId"); } + + @override + Future> productCoords(int id) async { + final res = await _getJson( + path: "/products/coords?product_id=$id", + headers: { + "Content-Type": "application/json", + "Accept": "application/json", + }, + ); + return res.flatMap((body) { + if (body["ok"]) { + if (body["found"]) { + return Ok( + (Coordinate.fromJson(body["coords"] as Map))); + } + return const Err("Product has no coordinate"); + } else { + return Err(body["msg"]); + } + }); + } } diff --git a/mobile/lib/server/mock_server.dart b/mobile/lib/server/mock_server.dart index fe0a3ee..8ab8ac9 100644 --- a/mobile/lib/server/mock_server.dart +++ b/mobile/lib/server/mock_server.dart @@ -152,4 +152,9 @@ class MockServer implements Server { Image productImage(int productId) { return Image.asset("assets/placeholder.png"); } + + @override + Future> productCoords(int id) async { + return Ok(Coordinate(x: 200, y: 100)); + } } diff --git a/mobile/lib/server/server.dart b/mobile/lib/server/server.dart index a934447..a946d37 100644 --- a/mobile/lib/server/server.dart +++ b/mobile/lib/server/server.dart @@ -1,6 +1,7 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:mobile/models/cart_item.dart'; +import 'package:mobile/models/coordinate.dart'; import 'package:mobile/models/product.dart'; import 'package:mobile/models/receipt.dart'; import 'package:mobile/models/user.dart'; @@ -31,6 +32,8 @@ abstract class Server { Future, String>> allReceipts(String token); Future> oneReceipt(String token, int id); + Future> productCoords(int id); + Image productImage(int productId); }