Initial commit
This commit is contained in:
79
flutter_app/lib/services/todo_service.dart
Normal file
79
flutter_app/lib/services/todo_service.dart
Normal file
@@ -0,0 +1,79 @@
|
||||
import "../config/api_config.dart";
|
||||
import "../models/todo_item.dart";
|
||||
import "api_client.dart";
|
||||
import "mock_data.dart";
|
||||
|
||||
class TodoService {
|
||||
final ApiClient _client;
|
||||
|
||||
TodoService(this._client);
|
||||
|
||||
Future<List<TodoItem>> fetchTodos() async {
|
||||
if (ApiConfig.useMockData) {
|
||||
return List<TodoItem>.from(MockDataStore.todos);
|
||||
}
|
||||
final data = await _client.getList(ApiConfig.todos);
|
||||
return data
|
||||
.map((item) => TodoItem.fromJson(item as Map<String, dynamic>))
|
||||
.toList();
|
||||
}
|
||||
|
||||
Future<List<TodoItem>> fetchTodayTodos() async {
|
||||
if (ApiConfig.useMockData) {
|
||||
final today = DateTime.now();
|
||||
return MockDataStore.todos.where((todo) => todo.dueDate != null).where((
|
||||
todo,
|
||||
) {
|
||||
final date = todo.dueDate!;
|
||||
return date.year == today.year &&
|
||||
date.month == today.month &&
|
||||
date.day == today.day;
|
||||
}).toList();
|
||||
}
|
||||
final data = await _client.getList("${ApiConfig.todos}/today");
|
||||
return data
|
||||
.map((item) => TodoItem.fromJson(item as Map<String, dynamic>))
|
||||
.toList();
|
||||
}
|
||||
|
||||
Future<TodoItem> createTodo(TodoItem todo) async {
|
||||
if (ApiConfig.useMockData) {
|
||||
final created = TodoItem(
|
||||
id: "todo-${DateTime.now().millisecondsSinceEpoch}",
|
||||
familyMemberId: todo.familyMemberId,
|
||||
title: todo.title,
|
||||
completed: todo.completed,
|
||||
dueDate: todo.dueDate,
|
||||
);
|
||||
MockDataStore.todos.add(created);
|
||||
return created;
|
||||
}
|
||||
final data = await _client.post(ApiConfig.todos, todo.toJson());
|
||||
return TodoItem.fromJson(data);
|
||||
}
|
||||
|
||||
Future<TodoItem> updateTodo(TodoItem todo) async {
|
||||
if (ApiConfig.useMockData) {
|
||||
final index = MockDataStore.todos.indexWhere(
|
||||
(item) => item.id == todo.id,
|
||||
);
|
||||
if (index != -1) {
|
||||
MockDataStore.todos[index] = todo;
|
||||
}
|
||||
return todo;
|
||||
}
|
||||
final data = await _client.put(
|
||||
"${ApiConfig.todos}/${todo.id}",
|
||||
todo.toJson(),
|
||||
);
|
||||
return TodoItem.fromJson(data);
|
||||
}
|
||||
|
||||
Future<void> deleteTodo(String id) async {
|
||||
if (ApiConfig.useMockData) {
|
||||
MockDataStore.todos.removeWhere((item) => item.id == id);
|
||||
return;
|
||||
}
|
||||
await _client.delete("${ApiConfig.todos}/$id");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user