62 lines
2.1 KiB
Dart
62 lines
2.1 KiB
Dart
import 'package:logging/logging.dart';
|
|
|
|
import 'package:share_plus/share_plus.dart';
|
|
|
|
import 'package:pshared/api/requests/signup.dart';
|
|
import 'package:pshared/api/responses/account.dart';
|
|
import 'package:pshared/api/requests/change_password.dart';
|
|
import 'package:pshared/data/mapper/account/account.dart';
|
|
import 'package:pshared/models/account/account.dart';
|
|
import 'package:pshared/service/authorization/service.dart';
|
|
import 'package:pshared/service/files.dart';
|
|
import 'package:pshared/service/services.dart';
|
|
import 'package:pshared/utils/http/requests.dart';
|
|
|
|
|
|
class AccountService {
|
|
static final _logger = Logger('service.account');
|
|
static const String _objectType = Services.account;
|
|
|
|
static Future<Account> login(String email, String password, String locale) async {
|
|
_logger.fine('Logging in');
|
|
return AuthorizationService.login(_objectType, email, password, locale);
|
|
}
|
|
|
|
static Future<Account> restore() async {
|
|
return AuthorizationService.restore();
|
|
}
|
|
|
|
static Future<void> signup(SignupRequest request) async {
|
|
await getPOSTResponse(_objectType, 'signup', request.toJson());
|
|
}
|
|
|
|
static Future<void> logout() async {
|
|
_logger.fine('Logging out');
|
|
await AuthorizationService.logout();
|
|
}
|
|
|
|
static Future<Account> _getAccount(Future<Map<String, dynamic>> future) async {
|
|
final response = await future;
|
|
return AccountResponse.fromJson(response).account.toDomain();
|
|
}
|
|
|
|
static Future<Account> update(Account account) async {
|
|
_logger.fine('Patching account ${account.id}');
|
|
return _getAccount(AuthorizationService.getPUTResponse(_objectType, '', account.toDTO().toJson()));
|
|
}
|
|
|
|
static Future<Account> changePassword(String oldPassword, String newPassword) async {
|
|
_logger.fine('Changing password');
|
|
return _getAccount(AuthorizationService.getPATCHResponse(
|
|
_objectType,
|
|
'password',
|
|
ChangePassword(oldPassword: oldPassword, newPassword: newPassword).toJson(),
|
|
));
|
|
}
|
|
|
|
static Future<String> uploadAvatar(String id, XFile avatarFile) async {
|
|
_logger.fine('Uploading avatar');
|
|
return FilesService.uploadImage(_objectType, id, avatarFile);
|
|
}
|
|
}
|