fromis_9/app/lib/services/schedules_service.dart

65 lines
1.9 KiB
Dart
Raw Normal View History

/// 일정 API 서비스
library;
import 'package:intl/intl.dart';
import '../models/schedule.dart';
import 'api_client.dart';
/// 오늘 날짜 (KST) 반환
String getTodayKST() {
final now = DateTime.now();
return DateFormat('yyyy-MM-dd').format(now);
}
/// 일정 목록 조회 (월별)
Future<List<Schedule>> getSchedules(int year, int month) async {
final response = await dio.get('/schedules', queryParameters: {
'year': year.toString(),
'month': month.toString(),
});
final List<dynamic> data = response.data;
return data.map((json) => Schedule.fromJson(json)).toList();
}
/// 다가오는 일정 N개 조회 (오늘 이후) - 웹과 동일
Future<List<Schedule>> getUpcomingSchedules(int limit) async {
final todayStr = getTodayKST();
final response = await dio.get('/schedules', queryParameters: {
'startDate': todayStr,
'limit': limit.toString(),
});
final List<dynamic> data = response.data;
return data.map((json) => Schedule.fromJson(json)).toList();
}
/// 일정 검색 결과
class SearchResult {
final List<Schedule> schedules;
final int offset;
final bool hasMore;
const SearchResult({
required this.schedules,
required this.offset,
required this.hasMore,
});
}
/// 일정 검색 (Meilisearch)
Future<SearchResult> searchSchedules(String query, {int offset = 0, int limit = 20}) async {
final response = await dio.get('/schedules', queryParameters: {
'search': query,
'offset': offset.toString(),
'limit': limit.toString(),
});
// 응답: { schedules: [...], hasMore: bool, offset: int }
final Map<String, dynamic> data = response.data;
final List<dynamic> schedulesJson = data['schedules'] ?? [];
final schedules = schedulesJson.map((json) => Schedule.fromJson(json)).toList();
return SearchResult(
schedules: schedules,
offset: offset,
hasMore: data['hasMore'] ?? schedules.length >= limit,
);
}