-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
232 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import 'package:academia/exports/barrel.dart'; | ||
import 'package:get/get.dart'; | ||
import '../models/models.dart'; | ||
import 'package:dartz/dartz.dart'; | ||
|
||
class ChirpController extends GetxController { | ||
final PostService _service = PostService(); | ||
UserController userController = Get.find<UserController>(); | ||
RxList<Post> posts = RxList<Post>(); | ||
RxInt currentPage = 0.obs; | ||
|
||
@override | ||
void onInit() { | ||
super.onInit(); | ||
debugPrint("Chirp Controller Loaded"); | ||
} | ||
|
||
Future<Either<String, List<Post>>> fetchPosts() async { | ||
final result = await _service.fetchPosts(userController.authHeaders); | ||
|
||
return result.fold((l) { | ||
return left(l); | ||
}, (r) { | ||
posts.value = r["posts"]; | ||
currentPage.value = r["nextPage"]; | ||
return right(posts); | ||
}); | ||
} | ||
} |
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 +1,2 @@ | ||
export 'core/core.dart'; | ||
export 'services/services.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,3 @@ | ||
class ChirpService { | ||
static const urlPrefix = "http://localhost:8080"; | ||
} |
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,45 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:dartz/dartz.dart'; | ||
import 'package:http/http.dart' as http; | ||
import '../models.dart'; | ||
|
||
class PostService with ChirpService { | ||
Future<Either<String, Map<String, dynamic>>> fetchPosts( | ||
Map<String, String> authHeaders, { | ||
int page = 1, | ||
}) async { | ||
try { | ||
final response = await http.get( | ||
Uri.parse("${ChirpService.urlPrefix}/posts/all?pages=$page"), | ||
headers: authHeaders, | ||
); | ||
|
||
if (response.statusCode == 200) { | ||
int nextPage = page; | ||
// parse the posts and the next page | ||
final Map<String, dynamic> rawData = json.decode(response.body); | ||
if (rawData.containsKey("next") && rawData["next"] != null) { | ||
nextPage += 1; | ||
} | ||
|
||
final List<Post> posts = rawData["results"] | ||
.map((e) => Post.fromJson(e)) | ||
.toList() | ||
.cast<Post>(); | ||
|
||
return right({ | ||
"posts": posts, | ||
"nextPage": nextPage, | ||
}); | ||
} | ||
return left( | ||
json.decode(response.body)["error"], | ||
); | ||
} catch (e) { | ||
return left( | ||
"Encountered an error we did, check your connection you must, ${e.toString()}", | ||
); | ||
} | ||
} | ||
} |
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,2 @@ | ||
export 'chirp_service.dart'; | ||
export 'post_service.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