Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEAT(#63)/tela de conteúdo #106

Merged
merged 4 commits into from
Feb 1, 2025
Merged
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
3 changes: 3 additions & 0 deletions lib/ui/content/di/di_content.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import 'package:aranduapp/core/di/locator.dart';
import 'package:aranduapp/ui/content/service/content_service.dart';
import 'package:aranduapp/ui/content/viewmodel/content_viewmodel.dart';
import 'package:get_it/get_it.dart';

void setupContentDI() {
locator.registerLazySingleton(() => ContentService());
GetIt.I.registerFactory(() => ContentViewModel());
}
52 changes: 39 additions & 13 deletions lib/ui/content/model/content_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,50 @@ class ContentRequest {
final String content;
final String trailID;

ContentRequest(
{required this.title, required this.content, required this.trailID});
ContentRequest({
required this.title,
required this.content,
required this.trailID,
});

Map<String, dynamic> toJson() {
return <String, dynamic>{
'title': title,
'content': content,
'trailID': trailID,
};
// Método seguro para conversão de JSON
factory ContentRequest.fromJson(String jsonString) {
try {
final Map<String, dynamic> jsonMap = jsonDecode(jsonString);
return ContentRequest._fromMap(jsonMap);
} on FormatException catch (e) {
throw FormatException('Invalid JSON format: ${e.message}');
}
}

factory ContentRequest.fromJsonString(String jsonString) {
final json = jsonDecode(jsonString);
// Método factory seguro para conversão de Map
factory ContentRequest.fromMap(Map<String, dynamic> map) {
try {
return ContentRequest._fromMap(map);
} catch (e) {
throw ArgumentError('Invalid content data: $e');
}
}

// Implementação centralizada da lógica de parsing
factory ContentRequest._fromMap(Map<String, dynamic> map) {
return ContentRequest(
title: json['title']! as String,
content: json['content'] as String,
trailID: json['trailID'] as String,
title: _parseString(map['title']),
content: _parseString(map['content']),
trailID: _parseString(map['trail']),
);
}

// Método auxiliar para conversão segura de valores
static String _parseString(dynamic value) {
if (value == null) throw ArgumentError('Required field is missing');
return value.toString();
}

// Conversão para JSON
Map<String, dynamic> toJson() => {
'title': title,
'content': content,
'trailID': trailID,
};
}
38 changes: 24 additions & 14 deletions lib/ui/content/service/content_service.dart
Original file line number Diff line number Diff line change
@@ -1,36 +1,46 @@
import 'package:aranduapp/core/log/log.dart';
import 'package:aranduapp/core/network/studio_maker_api.dart';
import 'package:aranduapp/ui/content/model/content_request.dart';
import 'package:aranduapp/ui/content/model/content_response.dart';
import 'package:async/async.dart';
// ignore: unused_import
import 'package:dio/dio.dart';

class ContentService {
Future<List<Map<String, dynamic>>?> getContentsByTrail(String trailId) async {
// Método para buscar um conteúdo pelo ID, retornando um Result<ContentRequest>
Future<Result<ContentRequest>> getContentsById(String contentId) async {
try {
String path = '/contents/trail/$trailId';
Response response = await StudioMakerApi.getInstance().get(path: path);
final path = '/contents/$contentId';
final response = await StudioMakerApi.getInstance().get(path: path);

if (response.statusCode == 200 && response.data != null) {
return List<Map<String, dynamic>>.from(response.data);
final content = ContentRequest.fromMap(response.data);
return Result.value(content); // Retorna um Result.value
} else {
Log.e('Erro ao buscar conteúdos da trilha:${response.statusCode}');
return null;
return Result.error(
Exception("Erro ao buscar conteúdo: ${response.statusCode}"));
}
} catch (e) {
Log.e('Erro na requisição $e');
return null;
return Result.error(e); // Retorna um Result.error
}
}

Future<Map<String, dynamic>?> getContentsById(String contentId) async {
// Método para buscar conteúdos por trilha, retornando uma lista de ContentResponse
Future<List<ContentResponse>?> getContentsByTrail(String trailID) async {
try {
String path = '/contents/$contentId';
Response response = await StudioMakerApi.getInstance().get(path: path);
final path = '/contents/trail/$trailID';
final response = await StudioMakerApi.getInstance().get(path: path);

if (response.statusCode == 200 && response.data != null) {
return Map<String, dynamic>.from(response.data);
return (response.data as List)
.map((item) => ContentResponse.fromJson(item))
.toList();
} else {
Log.e('Erro ao buscar conteúdos da trilha:${response.statusCode}');
Log.e('Erro ao buscar conteúdos: ${response.statusCode}');
return null;
}
} catch (e) {
Log.e('Erro na requisição $e');
Log.e('Erro na requisição: $e');
return null;
}
}
Expand Down
Loading