Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 62 additions & 6 deletions lib/features/todo/data/datasources/todo_remote_datasource.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import '../models/todo_dto.dart';
@injectable
class TodoRemoteDataSource {
final http.Client _client;
// TODO: Replace this with the URL we provide you
final String baseUrl = 'https://6825aa0f0f0188d7e72ddd9a.mockapi.io/api/v1';
final String baseUrl = 'https://6835745dcd78db2058c1928b.mockapi.io';

TodoRemoteDataSource(this._client);

Expand All @@ -24,17 +23,24 @@ class TodoRemoteDataSource {
log('API Response: ${response.body}');
return jsonList.map((json) => TodoDTO.fromJson(json)).toList();
} else {
throw Exception('Failed to load todos: ${response.statusCode}');
throw Exception('Failed to load todos: ${response.statusCode} \n ${response.body}');
}
} catch (e) {
log('Error fetching todos: $e');
throw Exception('Failed to load todos: $e');
}
}

Future<TodoDTO> addTodo(String title) async {
Future<TodoDTO> addTodo(String title, String description, String imageUrl) async {
try {
final newTodo = {'title': title, 'createdAtSeconds': DateTime.now().millisecondsSinceEpoch ~/ 1000};
final newTodo = {
'id': generateId(),
'title': title,
'createdAtSeconds': DateTime.now().millisecondsSinceEpoch ~/ 1000,
'isDone': false,
'imageUrl': imageUrl,
'description': description,
};

log('Sending todo: ${json.encode(newTodo)}');

Expand All @@ -53,14 +59,64 @@ class TodoRemoteDataSource {
// If the API response is not in the expected format, transform it
return TodoDTO.fromJson(responseData);
} else {
throw Exception('Failed to create todo: ${response.statusCode}');
throw Exception('Failed to create todo: ${response.statusCode} \n ${response.body}');
}
} catch (e) {
log('Error adding todo: $e');
throw Exception('Failed to create todo: $e');
}
}

Future<TodoDTO> updateDoneState(String id, bool isDone) async {
try {
log('Updating done in a todo with id: $id');

final response = await _client.patch(
Uri.parse('$baseUrl/todo/$id'),
headers: {'Content-Type': 'application/json'},
body: json.encode({'isDone': isDone}),
);

log('Response status: ${response.statusCode}');
log('Response body: ${response.body}');

if (response.statusCode == 201 || response.statusCode == 200) {
final Map<String, dynamic> responseData = json.decode(response.body);

// If the API response is not in the expected format, transform it
return TodoDTO.fromJson(responseData);
} else {
throw Exception('Failed to create todo: ${response.statusCode} \n ${response.body}');
}
} catch (e) {
log('error while updating done state: $e');
throw Exception('Failed to update done state');
}
}

Future<void> deleteTodo(String id) async {
try {
log('Deleting a todo with id: $id');

final response = await _client.delete(
Uri.parse('$baseUrl/todo/$id'),
);

log('Response status: ${response.statusCode}');
log('Response body: ${response.body}');

if (response.statusCode == 201 || response.statusCode == 200) {
final Map<String, dynamic> responseData = json.decode(response.body);
log('response: $responseData');
} else {
throw Exception('Failed to create todo: ${response.statusCode} \n ${response.body}');
}
} catch (e) {
log('error while updating done state: $e');
throw Exception('Failed to update done state');
}
}

// For local client-side ID generation
String generateId() {
return const Uuid().v4();
Expand Down
3 changes: 3 additions & 0 deletions lib/features/todo/data/models/todo_dto.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ class TodoDTO with _$TodoDTO {
required String id,
required String title,
int? createdAtSeconds,
required bool isDone,
required String description,
required String imageUrl,
}) = _TodoDTO;

factory TodoDTO.fromJson(Map<String, dynamic> json) => _$TodoDTOFromJson(json);
Expand Down
94 changes: 89 additions & 5 deletions lib/features/todo/data/models/todo_dto.freezed.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions lib/features/todo/data/models/todo_dto.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 21 additions & 4 deletions lib/features/todo/data/repositories/todo_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,32 @@ class TodoRepository {
}

// Add a new todo remotely
Future<void> addTodo(String title) async {
Future<void> addTodo(String title, String description, String imageUrl) async {
try {
// Add todo remotely
await remoteDataSource.addTodo(title);
await remoteDataSource.addTodo(title, description, imageUrl);
} catch (e) {
log('Error adding remote todo: $e');
}
}

Future<TodoModel> updateDoneState(String id, bool isDone) async {
try {
return _mapDtoToModel(await remoteDataSource.updateDoneState(id, isDone));
} catch (e) {
log('Error updating done state: $e');
throw Exception('Error updating done state');
}
}

Future<void> deleteTodo(String id) async {
try {
await remoteDataSource.deleteTodo(id);
} catch (e) {
log('Error while deleting the todo: $e');
}
}

// Helper method to map DTOs to domain models
TodoModel _mapDtoToModel(TodoDTO dto) {
try {
Expand All @@ -51,10 +68,10 @@ class TodoRepository {
createdAt = DateTime.fromMillisecondsSinceEpoch(dto.createdAtSeconds! * 1000);
}

return TodoModel(id: dto.id, title: dto.title, createdAt: createdAt);
return TodoModel(id: dto.id, title: dto.title, createdAt: createdAt, isDone: dto.isDone, imageUrl: dto.imageUrl, description: dto.description);
} catch (e) {
log('Error mapping DTO to model: $e');
return TodoModel(id: const Uuid().v4(), title: 'Unknown title', createdAt: DateTime.now());
return TodoModel(id: const Uuid().v4(), title: 'Unknown title', createdAt: DateTime.now(), isDone: false, imageUrl: '', description: '');
}
}
}
4 changes: 3 additions & 1 deletion lib/features/todo/domain/models/todo_model.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:ffi';

import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:intl/intl.dart';

Expand All @@ -8,7 +10,7 @@ part 'todo_model.g.dart';
class TodoModel with _$TodoModel {
const TodoModel._();

const factory TodoModel({required String id, required String title, DateTime? createdAt}) =
const factory TodoModel({required String id, required String title, DateTime? createdAt, required String imageUrl, required String description, required bool isDone}) =
_TodoModel;

DateTime get effectiveCreatedAt => createdAt ?? DateTime.now();
Expand Down
Loading