From 583ac3643bf18b354154c84377ab58df11321932 Mon Sep 17 00:00:00 2001 From: User3 Date: Tue, 27 May 2025 09:48:44 +0300 Subject: [PATCH 01/12] #sprint-1 --- lib/apis/auth.dart | 14 +++++++++----- lib/constants.dart | 6 ++++-- test/server_test.dart | 15 ++++++++++++++- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/lib/apis/auth.dart b/lib/apis/auth.dart index 82274a9..309ded0 100644 --- a/lib/apis/auth.dart +++ b/lib/apis/auth.dart @@ -6,10 +6,11 @@ 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'; @@ -21,10 +22,11 @@ class Auth { register(String email, String password) async { try { - final response = api.post( - '/collections/users/auth-with-password', - data: {identity: email, password: password}, + final response = await api.post( + '/collections/users/records', + data: {emailKey: email, passwordKey: password}, ); + return response.data; } on DioException catch (e) { if (e.type == DioExceptionType.connectionError) { lastError = 'Internet Connection Error'; @@ -34,3 +36,5 @@ class Auth { } } } + +final auth = Auth(); diff --git a/lib/constants.dart b/lib/constants.dart index 5fe9d38..754e33d 100644 --- a/lib/constants.dart +++ b/lib/constants.dart @@ -1,4 +1,6 @@ const token = "token"; -const identity = 'identity'; -const password = 'password'; +const emailKey = 'email'; +const identityKey = 'identity'; +const passwordKey = 'password'; +const confirmPasswordKey = 'passwordConfirm'; diff --git a/test/server_test.dart b/test/server_test.dart index 0da434d..639965d 100644 --- a/test/server_test.dart +++ b/test/server_test.dart @@ -1,5 +1,18 @@ import 'package:flutter_test/flutter_test.dart'; +import 'package:server/apis/auth.dart'; void main() { - test('adds one to input values', () {}); + test('Спринт 2. Авторизация', () async { + try { + final response = await auth.login( + 'example123123@test.ru', + 'string213123', + ); + if (response != null) { + expect(response['record']['email'], 'example123123@test.ru'); + } + } catch (e) { + print(e); + } + }); } From 577b746b8cd4aafa7855c8aff381d6b04d857d38 Mon Sep 17 00:00:00 2001 From: User3 Date: Tue, 27 May 2025 10:08:54 +0300 Subject: [PATCH 02/12] #sprint-1 --- lib/apis/auth.dart | 71 ++++++++++++++++++++++++++++++++++++++++--- test/server_test.dart | 24 +++++++++++++++ 2 files changed, 90 insertions(+), 5 deletions(-) diff --git a/lib/apis/auth.dart b/lib/apis/auth.dart index 309ded0..a2b23e7 100644 --- a/lib/apis/auth.dart +++ b/lib/apis/auth.dart @@ -20,13 +20,28 @@ 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 = await api.post( - '/collections/users/records', - data: {emailKey: email, passwordKey: password}, + final response = await api.patch( + '/collections/users/record/$userId', + data: { + 'firstname': firstName, + 'lastname': lastname, + 'secondname': secondName, + 'datebirthday': datebirthday, + 'gender': gender, + }, ); - return response.data; + if (response.data != null) { + return response.data; + } } on DioException catch (e) { if (e.type == DioExceptionType.connectionError) { lastError = 'Internet Connection Error'; @@ -35,6 +50,52 @@ class Auth { } } } + + 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']; + } + } + } + } } final auth = Auth(); diff --git a/test/server_test.dart b/test/server_test.dart index 639965d..f01538b 100644 --- a/test/server_test.dart +++ b/test/server_test.dart @@ -12,6 +12,30 @@ void main() { expect(response['record']['email'], 'example123123@test.ru'); } } catch (e) { + // ignore: avoid_print + print(e); + } + }); + + test('Спринт 2. Создание пользователя', () async { + try { + final response = await auth.register( + 'example123123333333@mail.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); } }); From ffed4b19883178504a8aeb70dbc3749ee8d72849 Mon Sep 17 00:00:00 2001 From: User3 Date: Tue, 27 May 2025 10:22:28 +0300 Subject: [PATCH 03/12] #sprint-1 --- lib/apis/shop.dart | 40 ++++++++++++++++++++++++++++++++++++++++ test/server_test.dart | 12 ++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 lib/apis/shop.dart diff --git a/lib/apis/shop.dart b/lib/apis/shop.dart new file mode 100644 index 0000000..98bb771 --- /dev/null +++ b/lib/apis/shop.dart @@ -0,0 +1,40 @@ +import 'package:dio/dio.dart'; +import 'package:server/apis/auth.dart'; +import 'package:server/client.dart'; + +class Shop { + String lastError = ''; + getCatalog() async { + String token = ''; + try { + final loginResponse = await auth.login( + 'example123123333333@mail.ru', + 'string213123', + ); + if (loginResponse != null) { + token = loginResponse['token']; + api.options.headers.addAll({'Authorization': token}); + } + if (token != '') { + final response = await api.get('collections/products/records'); + if (response.data) { + return response.data; + } else { + return Error(); + } + } 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/test/server_test.dart b/test/server_test.dart index f01538b..4d3bcec 100644 --- a/test/server_test.dart +++ b/test/server_test.dart @@ -1,5 +1,6 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:server/apis/auth.dart'; +import 'package:server/apis/shop.dart'; void main() { test('Спринт 2. Авторизация', () async { @@ -39,4 +40,15 @@ void main() { print(e); } }); + test('Спринт 2. Получение каталога-3', () async { + try { + final response = await shop.getCatalog(); + if (response.data != null) { + expect(response.data['perPage'], 30); + } + } catch (e) { + print(e); + return Error(); + } + }); } From 080d7d137efe0d334a1b2708a9a704e475edcab7 Mon Sep 17 00:00:00 2001 From: User3 Date: Tue, 27 May 2025 10:28:55 +0300 Subject: [PATCH 04/12] #sprint-1 --- lib/apis/shop.dart | 14 ++++++-------- test/server_test.dart | 4 ++-- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/lib/apis/shop.dart b/lib/apis/shop.dart index 98bb771..a522488 100644 --- a/lib/apis/shop.dart +++ b/lib/apis/shop.dart @@ -1,3 +1,5 @@ +// ignore_for_file: unnecessary_brace_in_string_interps + import 'package:dio/dio.dart'; import 'package:server/apis/auth.dart'; import 'package:server/client.dart'; @@ -13,15 +15,11 @@ class Shop { ); if (loginResponse != null) { token = loginResponse['token']; - api.options.headers.addAll({'Authorization': token}); + api.options.headers['Authorization'] = "Bearer ${token}"; } - if (token != '') { - final response = await api.get('collections/products/records'); - if (response.data) { - return response.data; - } else { - return Error(); - } + final response = await api.get('collections/products/records'); + if (response.data != null) { + return response.data; } else { return Error(); } diff --git a/test/server_test.dart b/test/server_test.dart index 4d3bcec..c49e720 100644 --- a/test/server_test.dart +++ b/test/server_test.dart @@ -43,8 +43,8 @@ void main() { test('Спринт 2. Получение каталога-3', () async { try { final response = await shop.getCatalog(); - if (response.data != null) { - expect(response.data['perPage'], 30); + if (response != null) { + expect(response['perPage'], 30); } } catch (e) { print(e); From a131c3d2413125ff478518eec4178e57b805a88e Mon Sep 17 00:00:00 2001 From: User3 Date: Tue, 27 May 2025 10:38:24 +0300 Subject: [PATCH 05/12] #sprint-1 --- lib/apis/auth.dart | 13 +++++++++++++ lib/apis/cart.dart | 30 ++++++++++++++++++++++++++++++ lib/apis/shop.dart | 10 +--------- 3 files changed, 44 insertions(+), 9 deletions(-) create mode 100644 lib/apis/cart.dart 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; From 8f3009b98cb953849a12908ff7cd1f932ec31e61 Mon Sep 17 00:00:00 2001 From: User3 Date: Tue, 27 May 2025 11:12:55 +0300 Subject: [PATCH 06/12] #sprint-1 --- lib/apis/auth.dart | 2 +- lib/apis/cart.dart | 6 ++++-- test/server_test.dart | 10 ++++++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/apis/auth.dart b/lib/apis/auth.dart index 74f81ea..350126b 100644 --- a/lib/apis/auth.dart +++ b/lib/apis/auth.dart @@ -60,7 +60,7 @@ class Auth { if (loginResponse != null) { token = loginResponse['token']; api.options.headers['Authorization'] = "Bearer $token"; - return loginResponse['records']['id']; + return loginResponse['record']['id']; } } diff --git a/lib/apis/cart.dart b/lib/apis/cart.dart index 2f24c67..0f92b88 100644 --- a/lib/apis/cart.dart +++ b/lib/apis/cart.dart @@ -5,13 +5,13 @@ import 'package:server/client.dart'; class Cart { String lastError = ''; - addToCart() async { + 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': 'string', 'count': 10}, + data: {'user_id': id, 'product_id': productId, 'count': count}, ); if (response.data != null) { return 'Well done'; @@ -28,3 +28,5 @@ class Cart { } } } + +final cart = Cart(); diff --git a/test/server_test.dart b/test/server_test.dart index c49e720..b353e08 100644 --- a/test/server_test.dart +++ b/test/server_test.dart @@ -1,5 +1,6 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:server/apis/auth.dart'; +import 'package:server/apis/cart.dart'; import 'package:server/apis/shop.dart'; void main() { @@ -51,4 +52,13 @@ void main() { return Error(); } }); + test('Спринт 2. Добавление в корзину', () async { + try { + final response = await cart.addToCart('string', 10); + expect(response['count'], 10); + } catch (e) { + print(e); + return Error(); + } + }); } From 37f74d5f9237e0a6e15bf8529e8695826bf12777 Mon Sep 17 00:00:00 2001 From: User3 Date: Tue, 27 May 2025 11:35:34 +0300 Subject: [PATCH 07/12] #user1 --- lib/apis/auth.dart | 19 ++++++++++++++++++- lib/client.dart | 10 ++++++++++ lib/constants.dart | 1 + test/server_test.dart | 17 ++++++++++++++--- 4 files changed, 43 insertions(+), 4 deletions(-) diff --git a/lib/apis/auth.dart b/lib/apis/auth.dart index 350126b..1eb97c6 100644 --- a/lib/apis/auth.dart +++ b/lib/apis/auth.dart @@ -31,7 +31,7 @@ class Auth { try { final response = await api.patch( '/collections/users/record/$userId', - data: { + queryParameters: { 'firstname': firstName, 'lastname': lastname, 'secondname': secondName, @@ -60,6 +60,7 @@ class Auth { if (loginResponse != null) { token = loginResponse['token']; api.options.headers['Authorization'] = "Bearer $token"; + return loginResponse['record']['id']; } } @@ -109,6 +110,22 @@ class Auth { } } } + + 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']; + } + } + } } final auth = Auth(); 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 754e33d..ed779e3 100644 --- a/lib/constants.dart +++ b/lib/constants.dart @@ -4,3 +4,4 @@ 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 b353e08..2b1746b 100644 --- a/test/server_test.dart +++ b/test/server_test.dart @@ -7,11 +7,11 @@ void main() { test('Спринт 2. Авторизация', () async { try { final response = await auth.login( - 'example123123@test.ru', + 'example123123333333@mail.ru', 'string213123', ); if (response != null) { - expect(response['record']['email'], 'example123123@test.ru'); + expect(response['record']['email'], 'example123123333333@mail.ru'); } } catch (e) { // ignore: avoid_print @@ -22,7 +22,7 @@ void main() { test('Спринт 2. Создание пользователя', () async { try { final response = await auth.register( - 'example123123333333@mail.ru', + 'example123123333333@mail2.ru', 'string213123', 'string213123', 'testtest', @@ -61,4 +61,15 @@ void main() { return Error(); } }); + test('Спринт 2.Получение информации о профиле', () async { + try { + final response = await auth.getCurrentUser(); + if (response != null) { + expect(response['firstname'], 'testtest'); + } + } catch (e) { + print(e); + return Error(); + } + }); } From 0673c54fb54aeb1fb74e5754c2e895c91ac379c2 Mon Sep 17 00:00:00 2001 From: User3 Date: Tue, 27 May 2025 11:56:58 +0300 Subject: [PATCH 08/12] #sprint-1 news and user logout --- lib/apis/auth.dart | 17 +++++++++++++++++ lib/apis/shop.dart | 19 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/lib/apis/auth.dart b/lib/apis/auth.dart index 1eb97c6..10664f0 100644 --- a/lib/apis/auth.dart +++ b/lib/apis/auth.dart @@ -126,6 +126,23 @@ class Auth { } } } + + 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'; + } else { + lastError = e.response!.data['message']; + } + } + } } final auth = Auth(); diff --git a/lib/apis/shop.dart b/lib/apis/shop.dart index 8d4838c..55bffc7 100644 --- a/lib/apis/shop.dart +++ b/lib/apis/shop.dart @@ -6,6 +6,25 @@ 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 { From 998d829da3544059639e2f4fdfa3e97d6bf7c703 Mon Sep 17 00:00:00 2001 From: User3 Date: Tue, 27 May 2025 12:07:34 +0300 Subject: [PATCH 09/12] #sprint-1 maked change cart,maked get product desc --- lib/apis/cart.dart | 23 +++++++++++++++ lib/apis/projects.dart | 65 ++++++++++++++++++++++++++++++++++++++++++ lib/apis/shop.dart | 20 +++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 lib/apis/projects.dart diff --git a/lib/apis/cart.dart b/lib/apis/cart.dart index 0f92b88..8253734 100644 --- a/lib/apis/cart.dart +++ b/lib/apis/cart.dart @@ -27,6 +27,29 @@ class Cart { 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/projects.dart b/lib/apis/projects.dart new file mode 100644 index 0000000..ffcd4ec --- /dev/null +++ b/lib/apis/projects.dart @@ -0,0 +1,65 @@ +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(); + } + } +} diff --git a/lib/apis/shop.dart b/lib/apis/shop.dart index 55bffc7..7d2f3ed 100644 --- a/lib/apis/shop.dart +++ b/lib/apis/shop.dart @@ -44,6 +44,26 @@ class Shop { 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(); From f5a75e3fdfbc5f93dada4cbce63838c29b2cf996 Mon Sep 17 00:00:00 2001 From: User3 Date: Tue, 27 May 2025 12:18:26 +0300 Subject: [PATCH 10/12] #sprint-1 changed readme added token service and maked handleLogout service --- README.md | 42 +++--------------------------------------- lib/apis/auth.dart | 1 + lib/apis/tokens.dart | 21 +++++++++++++++++++++ test/server_test.dart | 13 +++++++++++++ 4 files changed, 38 insertions(+), 39 deletions(-) create mode 100644 lib/apis/tokens.dart 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 10664f0..1dcd906 100644 --- a/lib/apis/auth.dart +++ b/lib/apis/auth.dart @@ -4,6 +4,7 @@ import 'package:server/server.dart'; class Auth { String lastError = ''; + login(String email, String password) async { try { final response = await api.post( 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/test/server_test.dart b/test/server_test.dart index 2b1746b..42b8ee3 100644 --- a/test/server_test.dart +++ b/test/server_test.dart @@ -2,6 +2,7 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:server/apis/auth.dart'; import 'package:server/apis/cart.dart'; import 'package:server/apis/shop.dart'; +import 'package:server/apis/tokens.dart'; void main() { test('Спринт 2. Авторизация', () async { @@ -72,4 +73,16 @@ void main() { 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(); + } + }); } From fae269868ab0cf19d7dce237c30809ccbf240c90 Mon Sep 17 00:00:00 2001 From: User3 Date: Tue, 27 May 2025 12:45:09 +0300 Subject: [PATCH 11/12] #sprint-1 added orders test --- lib/apis/orders.dart | 27 +++++++++++++++++++++ lib/apis/projects.dart | 2 ++ test/server_test.dart | 54 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 lib/apis/orders.dart 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 index ffcd4ec..b3f280c 100644 --- a/lib/apis/projects.dart +++ b/lib/apis/projects.dart @@ -63,3 +63,5 @@ class Projects { } } } + +final projects = Projects(); diff --git a/test/server_test.dart b/test/server_test.dart index 42b8ee3..333c6d3 100644 --- a/test/server_test.dart +++ b/test/server_test.dart @@ -1,6 +1,8 @@ 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'; @@ -20,6 +22,21 @@ void main() { } }); + 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( @@ -42,6 +59,7 @@ void main() { print(e); } }); + test('Спринт 2. Получение каталога-3', () async { try { final response = await shop.getCatalog(); @@ -53,6 +71,7 @@ void main() { return Error(); } }); + test('Спринт 2. Добавление в корзину', () async { try { final response = await cart.addToCart('string', 10); @@ -62,6 +81,7 @@ void main() { return Error(); } }); + test('Спринт 2.Получение информации о профиле', () async { try { final response = await auth.getCurrentUser(); @@ -73,6 +93,7 @@ void main() { return Error(); } }); + test('Спринт 2.Логаут пользователя', () async { try { final currentId = await tokens.getAccessToken(); @@ -85,4 +106,37 @@ void main() { 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); + } + }); } From 8d4691a48d917cf60ac7e648889bc682f539699f Mon Sep 17 00:00:00 2001 From: User3 Date: Tue, 27 May 2025 12:54:00 +0300 Subject: [PATCH 12/12] #sprint-1 maked news test --- test/server_test.dart | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/server_test.dart b/test/server_test.dart index 333c6d3..f8160f7 100644 --- a/test/server_test.dart +++ b/test/server_test.dart @@ -72,6 +72,18 @@ void main() { } }); + 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);