-
Notifications
You must be signed in to change notification settings - Fork 1
Feat/student discharge #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e8e9727
7c4c420
cd8c5fe
74fb4c3
2116706
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -37,6 +37,10 @@ class ApiClient { | |||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||
| _initializeCacheManager(); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| void _initializeCacheManager() { | ||||||||||||||||||||||||||
| CacheManager.getInstance().then((value) => _cacheManager = value); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
Comment on lines
+43
to
45
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add error handling for cache manager initialization The cache manager initialization lacks error handling, which could leave - void _initializeCacheManager() {
- CacheManager.getInstance().then((value) => _cacheManager = value);
+ Future<void> _initializeCacheManager() async {
+ try {
+ _cacheManager = await CacheManager.getInstance();
+ } catch (e) {
+ debugPrint('Failed to initialize cache manager: $e');
+ // Consider providing a fallback or rethrowing
+ rethrow;
+ }
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| class StudentDischarge { | ||
| final bool sitDep; | ||
| final bool sitBf; | ||
| final bool sitBc; | ||
| final bool sitRu; | ||
| final bool sitBrs; | ||
|
|
||
| StudentDischarge({ | ||
| this.sitDep = false, | ||
| this.sitBf = false, | ||
| this.sitBc = false, | ||
| this.sitRu = false, | ||
| this.sitBrs = false, | ||
| }); | ||
|
|
||
| factory StudentDischarge.fromJson(Map<String, dynamic> json) { | ||
| return StudentDischarge( | ||
| sitBc: (json['sitBc'] as int?) == 1, | ||
| sitBrs: (json['sitBrs'] as int?) == 1, | ||
| sitDep: (json['sitDep'] as int?) == 1, | ||
| sitBf: (json['sitBf'] as int?) == 1, | ||
| sitRu: (json['sitRu'] as int?) == 1, | ||
| ); | ||
| } | ||
|
|
||
| Map<String, dynamic> toJson() { | ||
| return { | ||
| 'sitDep': sitDep, | ||
| 'sitBf': sitBf, | ||
| 'sitBc': sitBc, | ||
| 'sitRu': sitRu, | ||
| 'sitBrs': sitBrs, | ||
| }; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import 'package:progres/features/discharge/data/models/discharge.dart'; | ||
| import 'package:progres/features/discharge/data/services/discharge_api_client.dart'; | ||
|
|
||
| class StudentDischargeRepositoryImpl { | ||
| final DischargeApiClient _apiClient; | ||
|
|
||
| StudentDischargeRepositoryImpl({DischargeApiClient? apiClient}) | ||
| : _apiClient = apiClient ?? DischargeApiClient(); | ||
|
|
||
| Future<StudentDischarge> getStudentDischarge() async { | ||
| try { | ||
| final uuid = await _apiClient.getUuid(); | ||
| final response = await _apiClient.get('/$uuid/qitus'); | ||
|
|
||
| final List<dynamic> dischargeJson = response.data; | ||
|
|
||
| if (dischargeJson.isEmpty) { | ||
| throw DischargeNotRequiredException( | ||
| 'Discharge is not required for this student', | ||
| ); | ||
| } | ||
|
|
||
| return StudentDischarge.fromJson(dischargeJson[0]); | ||
| } catch (e) { | ||
| rethrow; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class DischargeNotRequiredException implements Exception { | ||
| final String message; | ||
| DischargeNotRequiredException(this.message); | ||
|
|
||
| @override | ||
| String toString() => message; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Critical: Race condition in cache manager initialization
The asynchronous cache manager initialization in the constructor creates a race condition. Since
_initializeCacheManager()is not awaited, the constructor completes before_cacheManageris initialized, potentially causing runtime exceptions when methods likeget()are called immediately after object construction.Consider these solutions:
Option 1: Factory constructor pattern
Option 2: Make initialization method async and handle properly
🤖 Prompt for AI Agents