189 lines
5.1 KiB
Dart
189 lines
5.1 KiB
Dart
// lib/controllers/team_controller.dart
|
|
import 'dart:convert';
|
|
import 'package:get/get.dart';
|
|
import 'package:rogapp/model/team.dart';
|
|
import 'package:rogapp/model/category.dart';
|
|
import 'package:rogapp/model/user.dart';
|
|
import 'package:rogapp/services/api_service.dart';
|
|
|
|
class TeamController extends GetxController {
|
|
late final ApiService _apiService;
|
|
|
|
final teams = <Team>[].obs;
|
|
final categories = <NewCategory>[].obs;
|
|
final teamMembers = <User>[].obs;
|
|
|
|
|
|
final selectedCategory = Rx<NewCategory?>(null);
|
|
final selectedTeam = Rx<Team?>(null);
|
|
final currentUser = Rx<User?>(null);
|
|
|
|
final teamName = ''.obs;
|
|
final isLoading = false.obs;
|
|
final error = RxString('');
|
|
|
|
@override
|
|
void onInit() async{
|
|
super.onInit();
|
|
try {
|
|
_apiService = Get.find<ApiService>();
|
|
|
|
await fetchCategories();
|
|
await Future.wait([
|
|
fetchTeams(),
|
|
getCurrentUser(),
|
|
]);
|
|
|
|
print("selectedCategory=$selectedCategory.value");
|
|
// カテゴリが取得できたら、最初のカテゴリを選択状態にする
|
|
if (categories.isNotEmpty && selectedCategory.value == null) {
|
|
selectedCategory.value = categories.first;
|
|
}
|
|
|
|
}catch(e){
|
|
print("Team Controller error: $e");
|
|
error.value = e.toString();
|
|
}finally{
|
|
isLoading.value = false;
|
|
}
|
|
|
|
}
|
|
|
|
void setSelectedTeam(Team team) {
|
|
selectedTeam.value = team;
|
|
teamName.value = team.teamName;
|
|
if (categories.isNotEmpty) {
|
|
selectedCategory.value = categories.firstWhere(
|
|
(category) => category.id == team.category.id,
|
|
orElse: () => categories.first,
|
|
);
|
|
} else {
|
|
// カテゴリリストが空の場合、teamのカテゴリをそのまま使用
|
|
selectedCategory.value = team.category;
|
|
}
|
|
fetchTeamMembers(team.id);
|
|
}
|
|
|
|
void resetForm() {
|
|
selectedTeam.value = null;
|
|
teamName.value = '';
|
|
selectedCategory.value = categories.isNotEmpty ? categories.first : null;
|
|
teamMembers.clear();
|
|
}
|
|
|
|
Future<void> fetchTeams() async {
|
|
try {
|
|
isLoading.value = true;
|
|
final fetchedTeams = await _apiService.getTeams();
|
|
teams.assignAll(fetchedTeams);
|
|
} catch (e) {
|
|
error.value = 'チームの取得に失敗しました: $e';
|
|
print('Error fetching teams: $e');
|
|
} finally {
|
|
isLoading.value = false;
|
|
}
|
|
}
|
|
|
|
Future<void> fetchCategories() async {
|
|
try {
|
|
final fetchedCategories = await _apiService.getCategories();
|
|
categories.assignAll(fetchedCategories);
|
|
print("Fetched categories: ${categories.length}"); // デバッグ用
|
|
|
|
} catch (e) {
|
|
print('Error fetching categories: $e');
|
|
}
|
|
}
|
|
|
|
Future<void> getCurrentUser() async {
|
|
try {
|
|
final user = await _apiService.getCurrentUser();
|
|
currentUser.value = user;
|
|
} catch (e) {
|
|
print('Error getting current user: $e');
|
|
}
|
|
}
|
|
|
|
Future<Team> createTeam(String teamName, int categoryId) async {
|
|
final newTeam = await _apiService.createTeam(teamName, categoryId);
|
|
return newTeam;
|
|
}
|
|
|
|
Future<Team> updateTeam(int teamId, String teamName, int categoryId) async {
|
|
// APIサービスを使用してチームを更新
|
|
final updatedTeam = await _apiService.updateTeam(teamId, teamName, categoryId);
|
|
return updatedTeam;
|
|
}
|
|
|
|
Future<void> deleteTeam(int teamId) async {
|
|
try {
|
|
await _apiService.deleteTeam(teamId);
|
|
teams.removeWhere((team) => team.id == teamId);
|
|
} catch (e) {
|
|
print('Error deleting team: $e');
|
|
}
|
|
}
|
|
|
|
Future<void> fetchTeamMembers(int teamId) async {
|
|
try {
|
|
final members = await _apiService.getTeamMembers(teamId);
|
|
teamMembers.assignAll(members);
|
|
} catch (e) {
|
|
print('Error fetching team members: $e');
|
|
}
|
|
}
|
|
|
|
void updateTeamName(String value) {
|
|
teamName.value = value;
|
|
}
|
|
|
|
void updateCategory(NewCategory? value) {
|
|
if (value != null) {
|
|
selectedCategory.value = categories.firstWhere(
|
|
(category) => category.id == value.id,
|
|
orElse: () => value,
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<void> saveTeam() async {
|
|
try {
|
|
isLoading.value = true;
|
|
if (selectedCategory.value == null) {
|
|
throw Exception('カテゴリを選択してください');
|
|
}
|
|
|
|
if (selectedTeam.value == null) {
|
|
await createTeam(teamName.value, selectedCategory.value!.id);
|
|
} else {
|
|
await updateTeam(selectedTeam.value!.id, teamName.value, selectedCategory.value!.id);
|
|
}
|
|
|
|
// サーバーから最新のデータを再取得
|
|
await fetchTeams();
|
|
|
|
// 選択中のチームを更新
|
|
if (selectedTeam.value != null) {
|
|
selectedTeam.value = teams.firstWhere((t) => t.id == selectedTeam.value!.id);
|
|
teamName.value = selectedTeam.value!.teamName;
|
|
selectedCategory.value = selectedTeam.value!.category;
|
|
}
|
|
|
|
update(); // UIを強制的に更新
|
|
} catch (e) {
|
|
error.value = 'チームの保存に失敗しました: $e';
|
|
print("Team save error: $e");
|
|
} finally {
|
|
isLoading.value = false;
|
|
}
|
|
}
|
|
|
|
|
|
Future<void> deleteSelectedTeam() async {
|
|
if (selectedTeam.value != null) {
|
|
await deleteTeam(selectedTeam.value!.id);
|
|
selectedTeam.value = null;
|
|
}
|
|
}
|
|
|
|
} |