-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor user profile management: add update functionality and improv…
…e error handling
- Loading branch information
Showing
7 changed files
with
199 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 43 additions & 6 deletions
49
src/mobile-v3/lib/src/app/profile/repository/user_repository.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,63 @@ | ||
import 'dart:convert'; | ||
import 'package:airqo/src/app/profile/models/profile_response_model.dart'; | ||
import 'package:airqo/src/app/shared/repository/base_repository.dart'; | ||
import 'package:airqo/src/app/shared/repository/base_repository_extension.dart'; | ||
import 'package:airqo/src/app/shared/repository/hive_repository.dart'; | ||
import 'package:http/http.dart'; | ||
import 'package:http/http.dart' as http; | ||
|
||
abstract class UserRepository extends BaseRepository { | ||
Future<ProfileResponseModel> loadUserProfile(); | ||
Future<ProfileResponseModel> updateUserProfile({ | ||
required String firstName, | ||
required String lastName, | ||
required String email, | ||
}); | ||
} | ||
|
||
class UserImpl extends UserRepository { | ||
@override | ||
Future<ProfileResponseModel> loadUserProfile() async { | ||
final userId = await HiveRepository.getData("userId", HiveBoxNames.authBox); | ||
|
||
if (userId == null) { | ||
throw Exception("User ID not found"); | ||
} | ||
|
||
Response profileResponse = | ||
http.Response profileResponse = | ||
await createAuthenticatedGetRequest("/api/v2/users/${userId}", {}); | ||
|
||
ProfileResponseModel model = | ||
profileResponseModelFromJson(profileResponse.body); | ||
return model; | ||
} | ||
} | ||
|
||
@override | ||
Future<ProfileResponseModel> updateUserProfile({ | ||
required String firstName, | ||
required String lastName, | ||
required String email, | ||
}) async { | ||
final userId = await HiveRepository.getData("userId", HiveBoxNames.authBox); | ||
if (userId == null) { | ||
throw Exception("User ID not found"); | ||
} | ||
|
||
// Prepare the request body with the updated fields | ||
final Map<String, dynamic> requestBody = { | ||
"firstName": firstName, | ||
"lastName": lastName, | ||
"email": email, | ||
}; | ||
|
||
// The extension should handle error responses | ||
http.Response updateResponse = await createAuthenticatedPutRequest( | ||
data: requestBody, | ||
path: "/api/v2/users/$userId", | ||
); | ||
|
||
// Parse the updated user data | ||
ProfileResponseModel model = | ||
profileResponseModelFromJson(updateResponse.body); | ||
|
||
return model; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/mobile-v3/lib/src/app/shared/repository/base_repository_extension.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import 'dart:convert'; | ||
import 'package:airqo/src/app/shared/repository/base_repository.dart'; | ||
import 'package:airqo/src/app/shared/repository/hive_repository.dart'; | ||
import 'package:airqo/src/meta/utils/api_utils.dart'; | ||
import 'package:http/http.dart' as http; | ||
|
||
// Extension to add PUT request functionality to BaseRepository | ||
extension BaseRepositoryExtension on BaseRepository { | ||
// Method to create authenticated PUT requests | ||
Future<http.Response> createAuthenticatedPutRequest({ | ||
required String path, | ||
required dynamic data | ||
}) async { | ||
final token = await HiveRepository.getData("token", HiveBoxNames.authBox); | ||
if (token == null) { | ||
throw Exception('Authentication token not found'); | ||
} | ||
|
||
String url = ApiUtils.baseUrl + path; | ||
print(url); | ||
|
||
http.Response response = await http.put( | ||
Uri.parse(url), | ||
body: json.encode(data), | ||
headers: { | ||
"Authorization": "Bearer ${token}", | ||
"Accept": "*/*", | ||
"Content-Type": "application/json" | ||
} | ||
); | ||
|
||
print(response.statusCode); | ||
|
||
if (response.statusCode != 200) { | ||
final responseBody = json.decode(response.body); | ||
final errorMessage = responseBody is Map && responseBody.containsKey('message') | ||
? responseBody['message'] | ||
: 'An error occurred'; | ||
throw new Exception(errorMessage); | ||
} | ||
|
||
return response; | ||
} | ||
} |