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
43 changes: 37 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,14 +10,13 @@ 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://6835753ccd78db2058c196b3.mockapi.io/';

TodoRemoteDataSource(this._client);

Future<List<TodoDTO>> getTodos() async {
try {
final response = await _client.get(Uri.parse('$baseUrl/todos'));
final response = await _client.get(Uri.parse('$baseUrl/todo'));

if (response.statusCode == 200) {
final List<dynamic> jsonList = json.decode(response.body);
Expand All @@ -32,14 +31,21 @@ class TodoRemoteDataSource {
}
}

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,
'description': description,
'imageUrl': imageUrl,
'createdAtSeconds': DateTime.now().millisecondsSinceEpoch ~/ 1000,
'isDone': false,
};
Comment on lines +36 to +43

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (non-blocking): You could have request body DTO with a to json method to avoid creating/parsing JSON directly here


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

final response = await _client.post(
Uri.parse('$baseUrl/todos'),
Uri.parse('$baseUrl/todo'),
headers: {'Content-Type': 'application/json'},
body: json.encode(newTodo),
);
Expand All @@ -61,6 +67,31 @@ class TodoRemoteDataSource {
}
}

Future<TodoDTO> updateDone(String id, bool isDone) async {
try {
log('Updating todo: $id, $isDone');

final response = await _client.put(
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 == 200) {
final Map<String, dynamic> responseData = json.decode(response.body);
return TodoDTO.fromJson(responseData);
} else {
throw Exception('Failed to update todo: ${response.statusCode}');
}
} catch (e) {
log('Error updating todo: $e');
throw Exception('Failed to update todo: $e');
}
}

// 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 @@ -10,6 +10,9 @@ class TodoDTO with _$TodoDTO {
const factory TodoDTO({
required String id,
required String title,
required String description,
required String imageUrl,
required bool isDone,
int? createdAtSeconds,
}) = _TodoDTO;

Expand Down
92 changes: 88 additions & 4 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.

18 changes: 14 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,25 @@ 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> updateTodo(String id, bool isDone) async {
try {
final remoteDto = await remoteDataSource.updateDone(id, isDone);
return _mapDtoToModel(remoteDto);
} catch (e) {
log('Error updating remote todo: $e');
throw Exception('Failed to update todo: $e');
}
}

// Helper method to map DTOs to domain models
TodoModel _mapDtoToModel(TodoDTO dto) {
try {
Expand All @@ -51,10 +61,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, description: dto.description, imageUrl: dto.imageUrl, isDone: dto.isDone, createdAt: createdAt);
} 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', description: 'Unknown description', imageUrl: 'Unknown imageUrl', isDone: false, createdAt: DateTime.now());
}
}
}
10 changes: 8 additions & 2 deletions lib/features/todo/domain/models/todo_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ part 'todo_model.g.dart';
class TodoModel with _$TodoModel {
const TodoModel._();

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

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

Expand Down
Loading