Add: new item interfaces and category interface
This commit is contained in:
@@ -6,22 +6,30 @@ import 'package:gymlink_module_web/providers/main.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
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;
|
||||
if (token != '') {
|
||||
final Uri url =
|
||||
Uri.http('gymlink.freemyip.com:8080', 'api/product/get-list');
|
||||
try {
|
||||
final response = await http.get(url, headers: {
|
||||
'Authorization': 'Bearer $token',
|
||||
});
|
||||
if (response.statusCode == 200) {
|
||||
final data =
|
||||
jsonDecode(utf8.decode(response.bodyBytes)) as List<dynamic>;
|
||||
final items = data.map((e) => GymItem.fromJson(e)).toList();
|
||||
final response = await http.post(url,
|
||||
headers: {
|
||||
'Authorization': 'Bearer $token',
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: jsonEncode({
|
||||
"search": searchText,
|
||||
"page": 0,
|
||||
"pageSize": 0,
|
||||
"filter": categoryId,
|
||||
"direction": 1
|
||||
}));
|
||||
if (response.statusCode == 201) {
|
||||
final items = ItemsDataResponse.fromRawJson(response.body).rows;
|
||||
return items;
|
||||
}
|
||||
throw Error();
|
||||
throw response.body;
|
||||
} catch (e) {
|
||||
debugPrint('error: $e');
|
||||
return await Future.delayed(
|
||||
@@ -64,3 +72,27 @@ Future<List<GymItem>> getItemsByIds(
|
||||
context.read<GymLinkProvider>().onError();
|
||||
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 [];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user