|
import 'dart:convert';
|
|
|
|
import 'package:sunnex/model/responses/base_result.dart';
|
|
import 'package:sunnex/model/responses/profile_result.dart';
|
|
import 'package:sunnex/repository/base_repository.dart';
|
|
import '../core/utils/authenticated_client.dart';
|
|
import '../core/constants/constants.dart';
|
|
|
|
class ProfileRepository extends AuthenticatedApiClient implements BaseRepository {
|
|
final String profileURL, sendLanguageURL;
|
|
|
|
ProfileRepository({
|
|
required this.profileURL,
|
|
required this.sendLanguageURL,
|
|
required super.authRepository,
|
|
required super.databaseService,
|
|
});
|
|
|
|
Future<ProfileResult?> getProfile() async {
|
|
try {
|
|
var response = await get(profileURL);
|
|
var jsonString = response.body;
|
|
Constants.logger.i(jsonString);
|
|
|
|
if (!Constants.isSuccessCode(response.statusCode)) {
|
|
Constants.logger.e(
|
|
'Get profile failed with status: ${response.statusCode}',
|
|
);
|
|
return null;
|
|
}
|
|
|
|
return ProfileResult.fromRawJson(jsonString);
|
|
} catch (e) {
|
|
Constants.logger.e('Get profile error: $e');
|
|
return null;
|
|
}
|
|
}
|
|
|
|
Future<ProfileResult> createProfile(ProfileData data) async {
|
|
try {
|
|
var response = await post(profileURL, data.toRawJson());
|
|
var jsonString = response.body;
|
|
Constants.logger.i(jsonString);
|
|
|
|
ProfileResult result = ProfileResult.fromRawJson(jsonString);
|
|
if (result.data == null || !result.success) {
|
|
Constants.logger.e(
|
|
'Create profile failed: ${result.error?.code}: ${result.error?.toString()}',
|
|
);
|
|
throw Exception(result.error?.toString() ?? 'Create profile failed');
|
|
}
|
|
|
|
return result;
|
|
} catch (e) {
|
|
Constants.logger.e('Create profile error: $e');
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
Future<ProfileResult> updateProfile(ProfileData data) async {
|
|
try {
|
|
var response = await put(profileURL, data.toRawJson());
|
|
var jsonString = response.body;
|
|
Constants.logger.i(jsonString);
|
|
|
|
ProfileResult result = ProfileResult.fromRawJson(jsonString);
|
|
if (result.data == null || !result.success) {
|
|
Constants.logger.e(
|
|
'Update profile failed: ${result.error?.code}: ${result.error?.toString()}',
|
|
);
|
|
throw Exception(result.error?.toString() ?? 'Update profile failed');
|
|
}
|
|
|
|
return result;
|
|
} catch (e) {
|
|
Constants.logger.e('Update profile error: $e');
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
Future<DummyResult?> sendMyLanguage() async {
|
|
try {
|
|
var response = await post(
|
|
sendLanguageURL,
|
|
json.encode({'language': 'en'}),
|
|
);
|
|
var jsonString = response.body;
|
|
Constants.logger.i(jsonString);
|
|
|
|
if (!Constants.isSuccessCode(response.statusCode)) {
|
|
Constants.logger.e(
|
|
'Send language failed with status: ${response.statusCode}',
|
|
);
|
|
return null;
|
|
}
|
|
|
|
return DummyResult.fromRawJson(jsonString);
|
|
} catch (e) {
|
|
Constants.logger.e('Send language error: $e');
|
|
return null;
|
|
}
|
|
}
|
|
}
|