diff --git a/lib/apis/auth.dart b/lib/apis/auth.dart index a2b23e7..74f81ea 100644 --- a/lib/apis/auth.dart +++ b/lib/apis/auth.dart @@ -51,6 +51,19 @@ class Auth { } } + setToken() async { + String token = ''; + final loginResponse = await auth.login( + 'example123123333333@mail.ru', + 'string213123', + ); + if (loginResponse != null) { + token = loginResponse['token']; + api.options.headers['Authorization'] = "Bearer $token"; + return loginResponse['records']['id']; + } + } + register( String email, String password, diff --git a/lib/apis/cart.dart b/lib/apis/cart.dart new file mode 100644 index 0000000..2f24c67 --- /dev/null +++ b/lib/apis/cart.dart @@ -0,0 +1,30 @@ +import 'package:dio/dio.dart'; +import 'package:server/apis/auth.dart'; +import 'package:server/client.dart'; + +class Cart { + String lastError = ''; + + addToCart() async { + try { + final id = await auth.setToken(); + if (id != null) { + final response = await api.post( + '/collections/cart/records', + data: {'user_id': id, 'product_id': 'string', 'count': 10}, + ); + if (response.data != null) { + return 'Well done'; + } + } + } on DioException catch (e) { + if (e.type == DioExceptionType.connectionError) { + return "Internet Connection Error"; + } + return e.response!.data['message']; + } catch (e) { + print(e); + return Error(); + } + } +} diff --git a/lib/apis/shop.dart b/lib/apis/shop.dart index a522488..8d4838c 100644 --- a/lib/apis/shop.dart +++ b/lib/apis/shop.dart @@ -7,16 +7,8 @@ import 'package:server/client.dart'; class Shop { String lastError = ''; getCatalog() async { - String token = ''; + await auth.setToken(); try { - final loginResponse = await auth.login( - 'example123123333333@mail.ru', - 'string213123', - ); - if (loginResponse != null) { - token = loginResponse['token']; - api.options.headers['Authorization'] = "Bearer ${token}"; - } final response = await api.get('collections/products/records'); if (response.data != null) { return response.data;