40 lines
1.2 KiB
Dart
40 lines
1.2 KiB
Dart
import 'dart:convert';
|
|
import 'package:http/http.dart' as http;
|
|
|
|
import '../config/api_config.dart';
|
|
import '../models/lesson.dart';
|
|
|
|
/// Service per le lezioni della scuola YogaSoul.
|
|
/// Chiama `public/api/my_lessons.php` (phpApiBase), stesso pattern di SettingsApi.
|
|
class LessonsApi {
|
|
static Map<String, String> _headers(String token) => {
|
|
'Accept': 'application/json',
|
|
'Authorization': 'Bearer $token',
|
|
};
|
|
|
|
/// Carica le lezioni del mese indicato.
|
|
/// [month] deve essere nel formato "YYYY-MM". Se null, il server usa il mese corrente.
|
|
static Future<LessonsResponse> fetchMyLessons({
|
|
required String token,
|
|
String? month,
|
|
}) async {
|
|
final query = (month != null && month.isNotEmpty) ? '?month=$month' : '';
|
|
final uri = Uri.parse('${ApiConfig.phpApiBase}/my_lessons.php$query');
|
|
|
|
final res = await http.get(uri, headers: _headers(token));
|
|
|
|
Map<String, dynamic> data;
|
|
try {
|
|
data = jsonDecode(res.body) as Map<String, dynamic>;
|
|
} catch (_) {
|
|
throw Exception('Risposta non valida dal server (${res.statusCode})');
|
|
}
|
|
|
|
if (res.statusCode != 200 || data['success'] != true) {
|
|
throw Exception(data['error'] ?? data['message'] ?? 'Errore lezioni');
|
|
}
|
|
|
|
return LessonsResponse.fromJson(data);
|
|
}
|
|
}
|