99 lines
3.0 KiB
Dart
99 lines
3.0 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:gymlink_module_web/interfaces/items.dart';
|
|
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,
|
|
{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.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 response.body;
|
|
} catch (e) {
|
|
debugPrint('error: $e');
|
|
return await Future.delayed(
|
|
const Duration(seconds: 5), () => getItems(context));
|
|
}
|
|
}
|
|
context.read<GymLinkProvider>().onError();
|
|
return [];
|
|
}
|
|
|
|
Future<List<GymItem>> getItemsByIds(
|
|
BuildContext context, List<String> ids) async {
|
|
final token = context.read<GymLinkProvider>().token;
|
|
if (token != '') {
|
|
if (ids.isEmpty) {
|
|
return [];
|
|
}
|
|
final Uri url =
|
|
Uri.http('gymlink.freemyip.com:8080', 'api/product/get-products');
|
|
try {
|
|
final response = await http.post(url,
|
|
headers: {
|
|
'Authorization': 'Bearer $token',
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: jsonEncode({"ids": ids}));
|
|
if (response.statusCode == 201) {
|
|
final data =
|
|
jsonDecode(utf8.decode(response.bodyBytes)) as List<dynamic>;
|
|
final items = data.map((e) => GymItem.fromJson(e)).toList();
|
|
return items;
|
|
}
|
|
throw response.body;
|
|
} catch (e) {
|
|
debugPrint('error: $e');
|
|
return await Future.delayed(
|
|
const Duration(seconds: 5), () => getItemsByIds(context, ids));
|
|
}
|
|
}
|
|
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 [];
|
|
}
|