diff --git a/README.md b/README.md index 4a260d8..70ba3a7 100644 --- a/README.md +++ b/README.md @@ -1,39 +1,3 @@ - - -TODO: Put a short description of the package here that helps potential users -know whether this package might be useful for them. - -## Features - -TODO: List what your package can do. Maybe include images, gifs, or videos. - -## Getting started - -TODO: List prerequisites and provide or point to information on how to -start using the package. - -## Usage - -TODO: Include short and useful examples for package users. Add longer examples -to `/example` folder. - -```dart -const like = 'sample'; -``` - -## Additional information - -TODO: Tell users more about the package: where to find more information, how to -contribute to the package, how to file issues, what response they can expect -from the package authors, and more. +// Тесты в server_test.dart +// Запускать встроенными экшенами +// в apis Лежат все апи структурированно \ No newline at end of file diff --git a/lib/apis/auth.dart b/lib/apis/auth.dart index 82274a9..1dcd906 100644 --- a/lib/apis/auth.dart +++ b/lib/apis/auth.dart @@ -4,12 +4,14 @@ import 'package:server/server.dart'; class Auth { String lastError = ''; + login(String email, String password) async { try { - final response = api.post( + final response = await api.post( '/collections/users/auth-with-password', - data: {identity: email, password: password}, + data: {identityKey: email, passwordKey: password}, ); + return response.data; } on DioException catch (e) { if (e.type == DioExceptionType.connectionError) { lastError = 'Internet Connection Error'; @@ -19,12 +21,121 @@ class Auth { } } - register(String email, String password) async { + updateUser( + String userId, + String firstName, + String lastname, + String secondName, + String datebirthday, + String gender, + ) async { try { - final response = api.post( - '/collections/users/auth-with-password', - data: {identity: email, password: password}, + final response = await api.patch( + '/collections/users/record/$userId', + queryParameters: { + 'firstname': firstName, + 'lastname': lastname, + 'secondname': secondName, + 'datebirthday': datebirthday, + 'gender': gender, + }, ); + if (response.data != null) { + return response.data; + } + } on DioException catch (e) { + if (e.type == DioExceptionType.connectionError) { + lastError = 'Internet Connection Error'; + } else { + lastError = e.response!.data['message']; + } + } + } + + 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['record']['id']; + } + } + + register( + String email, + String password, + String confirmPassword, + String firstName, + String lastname, + String secondName, + String datebirthday, + String gender, + ) async { + if (password != confirmPassword) { + lastError = 'Confirm password != password'; + } else { + try { + final response = await api.post( + '/collections/users/records', + data: { + emailKey: email, + passwordKey: password, + confirmPasswordKey: password, + }, + ); + if (response.data != null) { + final responseData = response.data['id']; + final result = await updateUser( + responseData, + firstName, + lastname, + secondName, + datebirthday, + gender, + ); + if (response.data) { + return result; + } + } + } on DioException catch (e) { + if (e.type == DioExceptionType.connectionError) { + lastError = 'Internet Connection Error'; + } else { + lastError = e.response!.data['message']; + } + } + } + } + + getCurrentUser() async { + try { + final id = await setToken(); + final response = await api.get('/collections/users/records/$id'); + if (response.data != null) { + return response.data; + } + } on DioException catch (e) { + if (e.type == DioExceptionType.connectionError) { + lastError = 'Internet Connection Error'; + } else { + lastError = e.response!.data['message']; + } + } + } + + userLogout(String tokenId) async { + try { + final response = await api.delete( + '/collections/_authOrigins/records/$tokenId', + ); + if (response.data != null) { + return response.data; + } } on DioException catch (e) { if (e.type == DioExceptionType.connectionError) { lastError = 'Internet Connection Error'; @@ -34,3 +145,5 @@ class Auth { } } } + +final auth = Auth(); diff --git a/lib/apis/cart.dart b/lib/apis/cart.dart new file mode 100644 index 0000000..8253734 --- /dev/null +++ b/lib/apis/cart.dart @@ -0,0 +1,55 @@ +import 'package:dio/dio.dart'; +import 'package:server/apis/auth.dart'; +import 'package:server/client.dart'; + +class Cart { + String lastError = ''; + + addToCart(String productId, double count) async { + try { + final id = await auth.setToken(); + if (id != null) { + final response = await api.post( + '/collections/cart/records', + data: {'user_id': id, 'product_id': productId, 'count': count}, + ); + 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(); + } + } + + changeCartItem(String productId, double count) async { + try { + final id = await auth.setToken(); + if (id != null) { + final response = await api.post( + '/collections/cart/records/{idBucket}', + data: {'user_id': id, 'product_id': productId, 'count': count}, + ); + 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(); + } + } +} + +final cart = Cart(); diff --git a/lib/apis/orders.dart b/lib/apis/orders.dart new file mode 100644 index 0000000..cf81614 --- /dev/null +++ b/lib/apis/orders.dart @@ -0,0 +1,27 @@ +import 'package:dio/dio.dart'; +import 'package:server/apis/auth.dart'; +import 'package:server/client.dart'; + +class Orders { + String lastError = ''; + createOrder(productId, count) async { + final userId = await auth.setToken(); + try { + final response = await api.post( + '/collections/orders/records', + data: {'user_id': userId, 'product_id': productId, 'count': count}, + ); + if (response.data != null) { + return response.data; + } + } on DioException catch (e) { + if (e.type == DioExceptionType.connectionError) { + lastError = 'Internet Connection Error'; + } else { + lastError = e.response!.data['message']; + } + } + } +} + +final orders = Orders(); diff --git a/lib/apis/projects.dart b/lib/apis/projects.dart new file mode 100644 index 0000000..b3f280c --- /dev/null +++ b/lib/apis/projects.dart @@ -0,0 +1,67 @@ +import 'package:dio/dio.dart'; +import 'package:server/apis/auth.dart'; +import 'package:server/server.dart'; + +class Projects { + getProjects() async { + try { + final id = await auth.setToken(); + if (id != null) { + final response = await api.get('/collections/project/records'); + if (response.data != null) { + return response.data; + } + } + } on DioException catch (e) { + if (e.type == DioExceptionType.connectionError) { + return "Internet Connection Error"; + } + return e.response!.data['message']; + } catch (e) { + print(e); + return Error(); + } + } + + createProject( + title, + typeProject, + dateStart, + dateEnd, + gender, + descriptionSource, + category, + ) async { + try { + final id = await auth.setToken(); + if (id != null) { + final response = await api.post( + '/collections/project/records', + data: { + "title": title, + "typeProject": typeProject, + "user_id": id, + "dateStart": dateStart, + "dateEnd": dateEnd, + "gender": gender, + "description_source": descriptionSource, + "category": category, + }, + ); + if (response.data != null) { + return response.data; + } + } + } on DioException catch (e) { + if (e.type == DioExceptionType.connectionError) { + return "Internet Connection Error"; + } + return e.response!.data['message']; + } catch (e) { + print(e); + return Error(); + } + } +} + +final projects = Projects(); diff --git a/lib/apis/shop.dart b/lib/apis/shop.dart new file mode 100644 index 0000000..7d2f3ed --- /dev/null +++ b/lib/apis/shop.dart @@ -0,0 +1,69 @@ +// ignore_for_file: unnecessary_brace_in_string_interps + +import 'package:dio/dio.dart'; +import 'package:server/apis/auth.dart'; +import 'package:server/client.dart'; + +class Shop { + String lastError = ''; + + getNews() async { + await auth.setToken(); + try { + final response = await api.get('/collections/news/records'); + if (response.data != null) { + return response.data; + } + } on DioException catch (e) { + if (e.type == DioExceptionType.connectionError) { + lastError = 'Internet Connection errror'; + } + lastError = e.response!.data['message']; + } catch (e) { + print('empty error'); + return 'empty errror'; + } + } + + getCatalog() async { + await auth.setToken(); + try { + final response = await api.get('collections/products/records'); + if (response.data != null) { + return response.data; + } else { + return Error(); + } + } on DioException catch (e) { + if (e.type == DioExceptionType.connectionError) { + lastError = 'Internet Connection errror'; + } + lastError = e.response!.data['message']; + } catch (e) { + print('empty error'); + return 'empty errror'; + } + } + + getProductDesc(productId) async { + await auth.setToken(); + try { + final response = await api.get('collections/products/records/$productId'); + if (response.data != null) { + return response.data; + } else { + return Error(); + } + } on DioException catch (e) { + if (e.type == DioExceptionType.connectionError) { + lastError = 'Internet Connection errror'; + } + lastError = e.response!.data['message']; + } catch (e) { + print('empty error'); + return 'empty errror'; + } + } +} + +final shop = Shop(); diff --git a/lib/apis/tokens.dart b/lib/apis/tokens.dart new file mode 100644 index 0000000..9401294 --- /dev/null +++ b/lib/apis/tokens.dart @@ -0,0 +1,21 @@ +import 'package:dio/dio.dart'; +import 'package:server/client.dart'; + +class Tokens { + String lastError = ''; + + getAccessToken() async { + try { + final response = await api.get('/collections/_authOrigins/records'); + return response.data['items'][0]['id']; + } on DioException catch (e) { + if (e.type == DioExceptionType.connectionError) { + lastError = 'Internet Connection Error'; + } else { + lastError = e.response!.data['message']; + } + } + } +} + +final tokens = Tokens(); diff --git a/lib/client.dart b/lib/client.dart index d505360..f472eeb 100644 --- a/lib/client.dart +++ b/lib/client.dart @@ -45,3 +45,13 @@ setToken(value) async { final prefs = await getLocalStorageInstance(); prefs.setString(token, value); } + +setKey(key, value) async { + final prefs = await getLocalStorageInstance(); + prefs.setString(key, value); +} + +getKey(key) async { + final prefs = await getLocalStorageInstance(); + prefs.get(key); +} diff --git a/lib/constants.dart b/lib/constants.dart index 5fe9d38..ed779e3 100644 --- a/lib/constants.dart +++ b/lib/constants.dart @@ -1,4 +1,7 @@ const token = "token"; -const identity = 'identity'; -const password = 'password'; +const emailKey = 'email'; +const identityKey = 'identity'; +const passwordKey = 'password'; +const confirmPasswordKey = 'passwordConfirm'; +const id = 'user_id'; diff --git a/test/server_test.dart b/test/server_test.dart index 0da434d..f8160f7 100644 --- a/test/server_test.dart +++ b/test/server_test.dart @@ -1,5 +1,154 @@ import 'package:flutter_test/flutter_test.dart'; +import 'package:server/apis/auth.dart'; +import 'package:server/apis/cart.dart'; +import 'package:server/apis/orders.dart'; +import 'package:server/apis/projects.dart'; +import 'package:server/apis/shop.dart'; +import 'package:server/apis/tokens.dart'; void main() { - test('adds one to input values', () {}); + test('Спринт 2. Авторизация', () async { + try { + final response = await auth.login( + 'example123123333333@mail.ru', + 'string213123', + ); + if (response != null) { + expect(response['record']['email'], 'example123123333333@mail.ru'); + } + } catch (e) { + // ignore: avoid_print + print(e); + } + }); + + test('Создание заказа', () async { + final productsList = await shop.getCatalog(); + final firstProduct = productsList['items'][0]; + + try { + final response = await orders.createOrder(firstProduct['id'], 10); + if (response != null) { + expect(response['count'], 10); + } + } catch (e) { + // ignore: avoid_print + print(e); + } + }); + + test('Спринт 2. Создание пользователя', () async { + try { + final response = await auth.register( + 'example123123333333@mail2.ru', + 'string213123', + 'string213123', + 'testtest', + 'testtest', + 'testtest', + 'testtest', + 'testtest', + ); + if (response != null) { + expect(response['record']['firstname'], 'testtest'); + } else { + expect('1', '2'); + } + } catch (e) { + // ignore: avoid_print + print(e); + } + }); + + test('Спринт 2. Получение каталога-3', () async { + try { + final response = await shop.getCatalog(); + if (response != null) { + expect(response['perPage'], 30); + } + } catch (e) { + print(e); + return Error(); + } + }); + + test('Спринт 2. Акции и новости', () async { + try { + final response = await shop.getNews(); + if (response != null) { + expect(response['perPage'], 30); + } + } catch (e) { + print(e); + return Error(); + } + }); + + test('Спринт 2. Добавление в корзину', () async { + try { + final response = await cart.addToCart('string', 10); + expect(response['count'], 10); + } catch (e) { + print(e); + return Error(); + } + }); + + test('Спринт 2.Получение информации о профиле', () async { + try { + final response = await auth.getCurrentUser(); + if (response != null) { + expect(response['firstname'], 'testtest'); + } + } catch (e) { + print(e); + return Error(); + } + }); + + test('Спринт 2.Логаут пользователя', () async { + try { + final currentId = await tokens.getAccessToken(); + if (currentId != null) { + final response = await auth.userLogout(currentId); + return response.data; + } + } catch (e) { + print(e); + return Error(); + } + }); + + test('Создание проекта', () async { + try { + final response = await projects.createProject( + '1233213', + '123312321', + '123321123', + '123321', + '123231', + '123312', + '123312', + ); + if (response != null) { + expect(response['title'], '1233213'); + } + } catch (e) { + // ignore: avoid_print + print(e); + } + }); + test('Список проектов', () async { + try { + final response = await projects.getProjects(); + if (response != null) { + expect(response['perPage'], 30); + } else { + expect('1', '2'); + } + } catch (e) { + // ignore: avoid_print + print(e); + } + }); }