cdb_ui/lib/api.dart

202 lines
4.8 KiB
Dart
Raw Normal View History

2024-09-04 18:05:04 +00:00
import 'dart:convert';
import 'package:http/http.dart' as http;
class API {
2024-09-07 21:56:52 +00:00
// todo : rework with auth
static String instance = "http://192.168.2.20:8080";
2024-09-04 18:05:04 +00:00
Future<String> getRequest(String url) async {
var resp = await http.get(Uri.parse(url), headers: <String, String>{
'Access-Control-Allow-Origin': '*',
});
return resp.body;
}
Future<String> postRequest(String url, Map<String, dynamic> data) async {
var resp = await http.post(Uri.parse(url),
headers: <String, String>{
'Access-Control-Allow-Origin': '*',
2024-09-07 21:56:52 +00:00
'Content-Type': 'application/json'
2024-09-04 18:05:04 +00:00
},
2024-09-07 21:56:52 +00:00
body: jsonEncode(data));
2024-09-04 18:05:04 +00:00
return resp.body;
}
Future<List<dynamic>> getItems() async {
var resp = jsonDecode(await getRequest("$instance/items"));
var lst = resp["items"];
return lst as List<dynamic>;
}
2024-09-07 21:56:52 +00:00
Future<List<String>> getUniqueField(
2024-09-05 07:09:51 +00:00
String item, String variant, String field) async {
2024-09-07 21:56:52 +00:00
var resp = jsonDecode(
await getRequest("$instance/item/$item/$variant/unique?field=$field"));
List<String> ret = [];
for (var e in resp as List<dynamic>) {
ret.add(e);
}
return ret;
2024-09-05 07:09:51 +00:00
}
2024-09-07 21:56:52 +00:00
Future<List<dynamic>> getLocations() async {
var resp = jsonDecode(await getRequest("$instance/locations"));
return resp as List<dynamic>;
2024-09-05 07:09:51 +00:00
}
2024-09-04 18:05:04 +00:00
Future<Item> getItem(String item) async {
return Item(jsonDecode(await getRequest("$instance/item/$item")));
}
Future<Transaction> getTransaction(String id) async {
return Transaction(
jsonDecode(await getRequest("$instance/transaction/$id")));
}
2024-09-05 08:25:18 +00:00
Future<List<Transaction>> getInventory(String item) async {
var resp = jsonDecode(await getRequest("$instance/item/$item/inventory"))
as List<dynamic>;
return resp.map((x) => Transaction(x)).toList();
}
Future<List<Transaction>> getInventoryOfVariant(
String item, String variant) async {
var resp =
jsonDecode(await getRequest("$instance/item/$item/$variant/inventory"))
as List<dynamic>;
return resp.map((x) => Transaction(x)).toList();
2024-09-04 18:05:04 +00:00
}
2024-09-05 07:09:51 +00:00
Future<String> supplyItem(String item, String variant, String price,
String? origin, String? location) async {
if (origin!.isEmpty) {
origin = null;
}
if (location!.isEmpty) {
location = null;
}
2024-09-07 21:56:52 +00:00
var req = await postRequest("$instance/supply", {
2024-09-04 18:05:04 +00:00
"item": item,
"variant": variant,
"price": price,
2024-09-05 07:09:51 +00:00
"origin": origin,
"location": location
2024-09-07 21:56:52 +00:00
});
var resp = jsonDecode(req);
print(resp);
return resp["uuid"];
2024-09-04 18:05:04 +00:00
}
2024-09-07 22:24:13 +00:00
Future<void> consumeItem(
String transaction, String destination, String price) async {
await postRequest("$instance/demand",
{"uuid": transaction, "destination": destination, "price": price});
}
2024-09-07 22:51:18 +00:00
Future<ItemVariantStat> getStat(String item, String variant) async {
return ItemVariantStat(
jsonDecode(await getRequest("$instance/item/$item/$variant/stat")));
}
2024-09-04 18:05:04 +00:00
String getImageURL(String item) {
return "$instance/$item/image";
}
}
class Item {
late String id;
late String name;
late String category;
late Map<String, ItemVariant> variants;
Item(Map<String, dynamic> json) {
id = json["uuid"];
name = json["name"];
category = json["category"];
variants = <String, ItemVariant>{};
json["variants"].forEach((key, value) {
variants[key] = ItemVariant(value);
});
}
}
class ItemVariant {
late String item;
late String variant;
late String name;
int? min;
int? expiry;
ItemVariant(Map<String, dynamic> json) {
item = json["item"];
variant = json["variant"];
name = json["name"];
min = json["min"];
expiry = json["expiry"];
}
}
class Price {
late double value;
late String currency;
Price(Map<String, dynamic> json) {
value = json["value"];
currency = json["currency"];
}
}
class Transaction {
late String uuid;
late String item;
late String variant;
late Price price;
late String? origin;
late int timestamp;
2024-09-07 21:56:52 +00:00
late ConsumeInfo? consumed;
2024-09-04 18:05:04 +00:00
late bool expired;
Transaction(Map<String, dynamic> json) {
uuid = json["uuid"];
item = json["item"];
variant = json["variant"];
price = Price(json["price"]);
origin = json["origin"];
timestamp = json["timestamp"];
expired = json["expired"];
2024-09-07 21:56:52 +00:00
consumed = json["consumed"] != null ? ConsumeInfo(json["consumed"]) : null;
2024-09-04 18:05:04 +00:00
}
}
class ConsumeInfo {
late String destination;
late Price price;
late int timestamp;
ConsumeInfo(Map<String, dynamic> json) {
destination = json["destination"];
price = Price(json["price"]);
timestamp = json["timestamp"];
}
}
2024-09-07 22:51:18 +00:00
class ItemVariantStat {
late int amount;
late double total_price;
ItemVariantStat(Map<String, dynamic> json) {
amount = json["amount"];
total_price = json["total_price"];
}
}