Files
bowlingManager/mobile/lib/services/event_service.dart
T
2025-08-01 16:45:26 +09:00

186 lines
4.7 KiB
Dart

import 'package:flutter/foundation.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../config/api_config.dart';
import '../models/event_model.dart';
import './api_service.dart';
class EventService with ChangeNotifier {
List<Event> _events = [];
Event? _currentEvent;
bool _isLoading = false;
String? _token;
final ApiService _apiService = ApiService();
String? _clubId;
List<Event> get events => [..._events];
bool get isLoading => _isLoading;
// 초기화 함수
Future<void> initialize(String token) async {
_token = token;
_apiService.setToken(token);
}
// 클럽 ID 설정
void setClubId(String clubId) {
_clubId = clubId;
notifyListeners();
}
// 클럽의 모든 이벤트 가져오기
Future<void> fetchClubEvents() async {
print('fetchClubEvents 호출됨: token=$_token');
_isLoading = true;
notifyListeners();
try {
// 클럽 ID 가져오기
final prefs = await SharedPreferences.getInstance();
final clubId = prefs.getString(ApiConfig.clubIdKey);
if (clubId == null) {
throw Exception('선택된 클럽이 없습니다');
}
final data = await _apiService.post(
'${ApiConfig.clubs}/events',
data: {'clubId': clubId},
);
// 응답 데이터가 리스트 형태로 직접 왔는지 확인
if (data is List) {
final List<dynamic> eventsData = data;
_events = eventsData
.map((eventData) => Event.fromJson(eventData))
.toList();
} else {
// 응답 데이터가 객체 형태로 왔을 경우 events 키를 통해 접근
final List<dynamic> eventsData = data['events'] ?? [];
_events = eventsData
.map((eventData) => Event.fromJson(eventData))
.toList();
}
_isLoading = false;
notifyListeners();
} catch (e) {
_isLoading = false;
notifyListeners();
throw Exception('이벤트 목록을 불러오는데 실패했습니다: $e');
}
}
// ID로 이벤트 정보 가져오기
Future<Event> fetchEventById(String eventId) async {
if (_token == null || _clubId == null) {
throw Exception('인증 또는 클럽 정보가 필요합니다');
}
try {
final data = await _apiService.post(
'${ApiConfig.clubs}/events/detail',
data: {'eventId': eventId},
);
return Event.fromJson(data['event']);
} catch (e) {
throw Exception('이벤트 정보를 불러오는데 실패했습니다: $e');
}
}
// 이벤트 생성
Future<Event> createEvent(Map<String, dynamic> eventData) async {
if (_token == null || _clubId == null) {
throw Exception('인증 또는 클럽 정보가 필요합니다');
}
_isLoading = true;
notifyListeners();
try {
final data = await _apiService.post(
'${ApiConfig.clubs}/events',
data: eventData,
);
final newEvent = Event.fromJson(data['event']);
_events.add(newEvent);
_isLoading = false;
notifyListeners();
return newEvent;
} catch (e) {
_isLoading = false;
notifyListeners();
throw Exception('이벤트 생성에 실패했습니다: $e');
}
}
// 이벤트 정보 업데이트
Future<Event> updateEvent(
String eventId,
Map<String, dynamic> updates,
) async {
if (_token == null || _clubId == null) {
throw Exception('인증 또는 클럽 정보가 필요합니다');
}
_isLoading = true;
notifyListeners();
try {
final data = await _apiService.put(
'${ApiConfig.clubs}/events/$eventId',
data: updates,
);
final updatedEvent = Event.fromJson(data['event']);
// 이벤트 목록 업데이트
final index = _events.indexWhere((event) => event.id == eventId);
if (index >= 0) {
_events[index] = updatedEvent;
}
_isLoading = false;
notifyListeners();
return updatedEvent;
} catch (e) {
_isLoading = false;
notifyListeners();
throw Exception('이벤트 정보 업데이트에 실패했습니다: $e');
}
}
// 이벤트 삭제
Future<bool> deleteEvent(String eventId) async {
if (_token == null || _clubId == null) {
throw Exception('인증 또는 클럽 정보가 필요합니다');
}
_isLoading = true;
notifyListeners();
try {
await _apiService.delete('${ApiConfig.clubs}/events/$eventId');
// 이벤트 목록에서 삭제
_events.removeWhere((event) => event.id == eventId);
_isLoading = false;
notifyListeners();
return true;
} catch (e) {
_isLoading = false;
notifyListeners();
throw Exception('이벤트 삭제에 실패했습니다: $e');
}
}
}