-
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.
Feat(#65): Adiciona request, response e service.
- Loading branch information
1 parent
f598509
commit 0518413
Showing
3 changed files
with
79 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,22 @@ | ||
import 'dart:convert'; | ||
|
||
class TrailsRequest {} | ||
class TrailsRequest { | ||
final String title; | ||
final String description; | ||
final List<String> contents; | ||
|
||
TrailsRequest({ | ||
required this.title, | ||
required this.description, | ||
required this.contents, | ||
}); | ||
|
||
// Método para converter o objeto em um Map (JSON) | ||
Map<String, dynamic> toJson() { | ||
return { | ||
'title': title, | ||
'description': description, | ||
'contents': contents, | ||
}; | ||
} | ||
} |
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,28 @@ | ||
class TrailResponse { | ||
final String id; | ||
final String title; | ||
final String description; | ||
final List<String> contents; | ||
final DateTime createdAt; | ||
final DateTime updatedAt; | ||
|
||
TrailResponse({ | ||
required this.id, | ||
required this.title, | ||
required this.description, | ||
required this.contents, | ||
required this.createdAt, | ||
required this.updatedAt, | ||
}); | ||
|
||
factory TrailResponse.fromJson(Map<String, dynamic> json) { | ||
return TrailResponse( | ||
id: json['_id'], | ||
title: json['title'], | ||
description: json['description'], | ||
contents: List<String>.from(json['contents']), | ||
createdAt: DateTime.parse(json['createdAt']), | ||
updatedAt: DateTime.parse(json['updatedAt']), | ||
); | ||
} | ||
} |
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,9 +1,35 @@ | ||
import 'package:aranduapp/core/network/auth_api.dart'; | ||
import 'package:aranduapp/ui/trails/model/trails_request.dart'; | ||
import 'package:aranduapp/core/log/log.dart'; | ||
import 'package:aranduapp/core/network/auth_api.dart'; // Certifique-se de que esta importação está correta | ||
import 'package:aranduapp/core/network/studio_maker_api.dart'; | ||
import 'package:aranduapp/ui/trails/model/trails_request.dart'; // Importação do modelo de request | ||
import 'package:aranduapp/ui/trails/model/trails_response.dart'; | ||
import 'package:dio/dio.dart'; // Importação do Dio para usar a classe Response | ||
|
||
class TrailsService { | ||
Future<void> edit(TrailsRequest editProfileRequest) async { | ||
//await AuthApi.getInstance(auth: true) | ||
//.patch(path: '/users', data: editProfileRequest.toJson()); Não queremos isso! | ||
// Método para editar uma trilha | ||
Future<TrailResponse?> edit( | ||
String trailId, TrailsRequest trailRequest) async { | ||
try { | ||
String path = '/trails/$trailId'; // Endpoint da API com o ID da trilha | ||
|
||
// Faz a requisição PUT (ou PATCH) para editar a trilha | ||
Response response = await StudioMakerApi.getInstance().put( | ||
path: path, | ||
data: | ||
trailRequest.toJson(), // Converte o objeto TrailsRequest para JSON | ||
); | ||
|
||
// Verifica se a requisição foi bem-sucedida | ||
if (response.statusCode == 200 && response.data != null) { | ||
return TrailResponse.fromJson( | ||
response.data); // Mapeia a resposta para TrailResponse | ||
} else { | ||
Log.e('Erro ao editar trilha: ${response.statusCode}'); | ||
return null; | ||
} | ||
} catch (e) { | ||
Log.e('Erro na requisição: $e'); | ||
return null; | ||
} | ||
} | ||
} |