Add: new item interfaces and category interface

This commit is contained in:
2024-06-04 14:36:43 +03:00
parent c8965dab4e
commit db39169907
2 changed files with 112 additions and 10 deletions

View File

@@ -1,5 +1,27 @@
import 'dart:convert'; import 'dart:convert';
class ItemsDataResponse {
final List<GymItem> rows;
ItemsDataResponse({
required this.rows,
});
factory ItemsDataResponse.fromRawJson(String str) =>
ItemsDataResponse.fromJson(json.decode(str));
String toRawJson() => json.encode(toJson());
factory ItemsDataResponse.fromJson(Map<String, dynamic> json) =>
ItemsDataResponse(
rows: List<GymItem>.from(json["rows"].map((x) => GymItem.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"rows": List<dynamic>.from(rows.map((x) => x.toJson())),
};
}
class GymItem { class GymItem {
final String id; final String id;
final String externalId; final String externalId;
@@ -44,7 +66,7 @@ class GymItem {
"title": title, "title": title,
"description": description, "description": description,
"count": count, "count": count,
"price": price * 100, "price": price,
"categoryId": categoryId, "categoryId": categoryId,
"images": List<dynamic>.from(images.map((x) => x.toJson())), "images": List<dynamic>.from(images.map((x) => x.toJson())),
}; };
@@ -78,3 +100,51 @@ class GymImage {
"url": url, "url": url,
}; };
} }
class CategoryDataResponse {
final List<GymCategory> rows;
CategoryDataResponse({
required this.rows,
});
factory CategoryDataResponse.fromRawJson(String str) =>
CategoryDataResponse.fromJson(json.decode(str));
String toRawJson() => json.encode(toJson());
factory CategoryDataResponse.fromJson(Map<String, dynamic> json) =>
CategoryDataResponse(
rows: List<GymCategory>.from(
json["rows"].map((x) => GymCategory.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"rows": List<dynamic>.from(rows.map((x) => x.toJson())),
};
}
class GymCategory {
final String id;
final String name;
GymCategory({
required this.id,
required this.name,
});
factory GymCategory.fromRawJson(String str) =>
GymCategory.fromJson(json.decode(str));
String toRawJson() => json.encode(toJson());
factory GymCategory.fromJson(Map<String, dynamic> json) => GymCategory(
id: json["id"],
name: json["name"],
);
Map<String, dynamic> toJson() => {
"id": id,
"name": name,
};
}

View File

@@ -6,22 +6,30 @@ import 'package:gymlink_module_web/providers/main.dart';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
Future<List<GymItem>> getItems(BuildContext context) async { Future<List<GymItem>> getItems(BuildContext context,
{String searchText = '', String categoryId = ''}) async {
final token = context.read<GymLinkProvider>().token; final token = context.read<GymLinkProvider>().token;
if (token != '') { if (token != '') {
final Uri url = final Uri url =
Uri.http('gymlink.freemyip.com:8080', 'api/product/get-list'); Uri.http('gymlink.freemyip.com:8080', 'api/product/get-list');
try { try {
final response = await http.get(url, headers: { final response = await http.post(url,
'Authorization': 'Bearer $token', headers: {
}); 'Authorization': 'Bearer $token',
if (response.statusCode == 200) { 'Content-Type': 'application/json'
final data = },
jsonDecode(utf8.decode(response.bodyBytes)) as List<dynamic>; body: jsonEncode({
final items = data.map((e) => GymItem.fromJson(e)).toList(); "search": searchText,
"page": 0,
"pageSize": 0,
"filter": categoryId,
"direction": 1
}));
if (response.statusCode == 201) {
final items = ItemsDataResponse.fromRawJson(response.body).rows;
return items; return items;
} }
throw Error(); throw response.body;
} catch (e) { } catch (e) {
debugPrint('error: $e'); debugPrint('error: $e');
return await Future.delayed( return await Future.delayed(
@@ -64,3 +72,27 @@ Future<List<GymItem>> getItemsByIds(
context.read<GymLinkProvider>().onError(); context.read<GymLinkProvider>().onError();
return []; return [];
} }
Future<List<GymCategory>> getCategories(BuildContext context) async {
final token = context.read<GymLinkProvider>().token;
if (token != '') {
final Uri url = Uri.http(
'gymlink.freemyip.com:8080', 'api/category/get-internal-categories');
try {
final response = await http.get(url, headers: {
'Authorization': 'Bearer $token',
});
if (response.statusCode == 200) {
final categories = CategoryDataResponse.fromRawJson(response.body).rows;
return categories;
}
throw response.body;
} catch (e) {
debugPrint('error: $e');
return await Future.delayed(
const Duration(seconds: 5), () => getCategories(context));
}
}
context.read<GymLinkProvider>().onError();
return [];
}