Merge pull request 'sprint-1' (#1) from sprint-1 into main

Reviewed-on: #1
This commit is contained in:
user3 2025-05-27 09:54:57 +00:00
commit c5531ae366
10 changed files with 526 additions and 48 deletions

View File

@ -1,39 +1,3 @@
<!--
This README describes the package. If you publish this package to pub.dev,
this README's contents appear on the landing page for your package.
For information about how to write a good package README, see the guide for
[writing package pages](https://dart.dev/tools/pub/writing-package-pages).
For general information about developing packages, see the Dart guide for
[creating packages](https://dart.dev/guides/libraries/create-packages)
and the Flutter guide for
[developing packages and plugins](https://flutter.dev/to/develop-packages).
-->
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 Лежат все апи структурированно

View File

@ -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();

55
lib/apis/cart.dart Normal file
View File

@ -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();

27
lib/apis/orders.dart Normal file
View File

@ -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();

67
lib/apis/projects.dart Normal file
View File

@ -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();

69
lib/apis/shop.dart Normal file
View File

@ -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();

21
lib/apis/tokens.dart Normal file
View File

@ -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();

View File

@ -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);
}

View File

@ -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';

View File

@ -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);
}
});
}