-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from tiagodread/api
Added API integration support
- Loading branch information
Showing
11 changed files
with
236 additions
and
125 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,4 +20,4 @@ class AppColors { | |
} | ||
|
||
const AppColors(); | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,123 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:flutter/cupertino.dart'; | ||
import 'package:http/http.dart' as http; | ||
|
||
import '../models/task.dart'; | ||
import 'task_repository.dart'; | ||
|
||
class APITaskRepository extends ChangeNotifier implements TaskRepository { | ||
@override | ||
Future<List<Task>> getAllTasks() async { | ||
List<Task> tasks = []; | ||
try { | ||
final response = await http.get(Uri.parse('http://localhost:8080/tasks'), | ||
headers: <String, String>{ | ||
'Content-Type': 'application/json', | ||
}); | ||
|
||
if (response.statusCode == 200) { | ||
Iterable jsonResponse = jsonDecode(response.body); | ||
tasks = jsonResponse.map((task) => Task.fromJson(task)).toList(); | ||
} | ||
return tasks; | ||
} catch (error) { | ||
throw Exception(error); | ||
} | ||
} | ||
|
||
@override | ||
Future<List<Task>> getCompletedTasks() async { | ||
List<Task> tasks = []; | ||
try { | ||
final response = await http.get( | ||
Uri.parse('http://localhost:8080/tasks?completed=true'), | ||
headers: <String, String>{ | ||
'Content-Type': 'application/json', | ||
}); | ||
|
||
if (response.statusCode == 200) { | ||
Iterable jsonResponse = jsonDecode(response.body); | ||
tasks = jsonResponse.map((task) => Task.fromJson(task)).toList(); | ||
} | ||
|
||
return tasks; | ||
} catch (error) { | ||
throw Exception(error); | ||
} | ||
} | ||
|
||
@override | ||
Future<Task?> getTaskById(String id) async { | ||
Task? task; | ||
try { | ||
final response = await http.get( | ||
Uri.parse('http://localhost:8080/task/$id'), | ||
headers: <String, String>{ | ||
'Content-Type': 'application/json', | ||
}); | ||
|
||
if (response.statusCode == 200) { | ||
Iterable jsonResponse = jsonDecode(response.body); | ||
task = jsonResponse.map((task) => Task.fromJson(task)) as Task; | ||
} | ||
return task; | ||
} catch (error) { | ||
throw Exception(error); | ||
} | ||
} | ||
|
||
@override | ||
Future<void> addTask(Task task) async { | ||
try { | ||
await http.post(Uri.parse('http://localhost:8080/task'), | ||
headers: <String, String>{ | ||
'Content-Type': 'application/json', | ||
}, | ||
body: jsonEncode(<String, dynamic>{ | ||
'title': task.title, | ||
'description': 'test', | ||
'created_at': task.createdAt.toUtc().toIso8601String(), | ||
'is_completed': task.isCompleted, | ||
'reward_in_sats': task.rewardInSatoshis | ||
})); | ||
} catch (error) { | ||
throw Exception(error); | ||
} | ||
notifyListeners(); | ||
} | ||
|
||
@override | ||
Future<void> updateTask(Task task) async { | ||
String taskId = task.id; | ||
try { | ||
await http.put(Uri.parse('http://localhost:8080/task/$taskId'), | ||
headers: <String, String>{ | ||
'Content-Type': 'application/json', | ||
}, | ||
body: jsonEncode(<String, dynamic>{ | ||
'title': task.title, | ||
'description': task.description, | ||
'created_at': task.createdAt.toUtc().toIso8601String(), | ||
'is_completed': task.isCompleted, | ||
'reward_in_sats': task.rewardInSatoshis | ||
})); | ||
} catch (error) { | ||
throw Exception(error); | ||
} | ||
notifyListeners(); | ||
} | ||
|
||
@override | ||
Future<void> deleteTask(String id) async { | ||
try { | ||
await http.delete(Uri.parse('http://localhost:8080/task/$id'), | ||
headers: <String, String>{ | ||
'Content-Type': 'application/json', | ||
}); | ||
} catch (error) { | ||
throw Exception(error); | ||
} | ||
notifyListeners(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.