49 lines
1.5 KiB
Dart
49 lines
1.5 KiB
Dart
import 'package:dio/dio.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:quiz_app/core/endpoint/api_endpoint.dart';
|
|
import 'package:quiz_app/core/utils/logger.dart';
|
|
import 'package:quiz_app/data/models/base/base_model.dart';
|
|
import 'package:quiz_app/data/models/history/participant_history_result.dart';
|
|
import 'package:quiz_app/data/providers/dio_client.dart';
|
|
|
|
class AnswerService extends GetxService {
|
|
late final Dio _dio;
|
|
|
|
@override
|
|
void onInit() {
|
|
_dio = Get.find<ApiClient>().dio;
|
|
super.onInit();
|
|
}
|
|
|
|
Future<BaseResponseModel?> submitQuizAnswers(Map<String, dynamic> payload) async {
|
|
try {
|
|
await _dio.post(
|
|
APIEndpoint.quizAnswer,
|
|
data: payload,
|
|
);
|
|
return BaseResponseModel(message: "success");
|
|
} on DioException catch (e) {
|
|
logC.e('Gagal mengirim jawaban: ${e.response?.data['message'] ?? e.message}');
|
|
return null;
|
|
}
|
|
}
|
|
|
|
Future<BaseResponseModel<ParticipantResult>?> getAnswerSession(String sessionId, String userId) async {
|
|
try {
|
|
final response = await _dio.post(APIEndpoint.quizAnswerSession, data: {
|
|
"session_id": sessionId,
|
|
"user_id": userId,
|
|
});
|
|
final parsedResponse = BaseResponseModel<ParticipantResult>.fromJson(
|
|
response.data,
|
|
(data) => ParticipantResult.fromJson(data),
|
|
);
|
|
|
|
return parsedResponse;
|
|
} on DioException catch (e) {
|
|
logC.e('Gagal mengirim jawaban: ${e.response?.data['message'] ?? e.message}');
|
|
return null;
|
|
}
|
|
}
|
|
}
|