103 lines
3.3 KiB
Dart
103 lines
3.3 KiB
Dart
// 🔐 (마스터 계정용) 학생 계정 및 기기 관리 화면의 기능(서버 통신/상태) 담당 컨트롤러.
|
|||
|
|
import 'dart:convert';
|
||
|
|
import 'package:flutter/foundation.dart';
|
||
|
|
import 'package:http/http.dart' as http;
|
||
|
|
import '../config.dart';
|
||
|
|
|
||
|
|
class StudentManagementController extends ChangeNotifier {
|
||
|
|
List<dynamic> _students = [];
|
||
|
|
bool _isLoading = true;
|
||
|
|
|
||
|
|
List<dynamic> get students => _students;
|
||
|
|
bool get isLoading => _isLoading;
|
||
|
|
|
||
|
|
Future<void> init() => fetchStudents();
|
||
|
|
|
||
|
|
/// 🌐 서버에서 전체 학생 목록 불러오기. 실패 시 에러 메시지를 반환한다(성공 시 null).
|
||
|
|
Future<String?> fetchStudents() async {
|
||
|
|
_isLoading = true;
|
||
|
|
notifyListeners();
|
||
|
|
try {
|
||
|
|
final response = await http.get(Uri.parse('$baseUrl/api/users'));
|
||
|
|
if (response.statusCode == 200) {
|
||
|
|
final data = jsonDecode(utf8.decode(response.bodyBytes));
|
||
|
|
_students = data['users'] ?? [];
|
||
|
|
_isLoading = false;
|
||
|
|
notifyListeners();
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
_isLoading = false;
|
||
|
|
notifyListeners();
|
||
|
|
return '데이터 불러오기 실패 (${response.statusCode})';
|
||
|
|
} catch (e) {
|
||
|
|
_isLoading = false;
|
||
|
|
notifyListeners();
|
||
|
|
return '데이터 불러오기 실패: $e';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// 🌐 서버로 기기 초기화(리셋) 명령 보내기
|
||
|
|
Future<(bool success, String message)> resetDevice(
|
||
|
|
String studentId,
|
||
|
|
String studentName,
|
||
|
|
) async {
|
||
|
|
try {
|
||
|
|
final response = await http.post(
|
||
|
|
Uri.parse('$baseUrl/api/users/reset'),
|
||
|
|
headers: {"Content-Type": "application/json"},
|
||
|
|
body: jsonEncode({"studentId": studentId}),
|
||
|
|
);
|
||
|
|
if (response.statusCode == 200) {
|
||
|
|
await fetchStudents();
|
||
|
|
return (true, '✅ $studentName 학생 기기 초기화 완료!');
|
||
|
|
}
|
||
|
|
return (false, '❌ 초기화 통신 실패');
|
||
|
|
} catch (e) {
|
||
|
|
return (false, '❌ 초기화 통신 실패');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// 🌐 서버로 계정 완전 삭제 명령 보내기
|
||
|
|
Future<(bool success, String message)> deleteUser(
|
||
|
|
String studentId,
|
||
|
|
String studentName,
|
||
|
|
) async {
|
||
|
|
try {
|
||
|
|
final response = await http.delete(
|
||
|
|
Uri.parse('$baseUrl/api/users/delete'),
|
||
|
|
headers: {"Content-Type": "application/json"},
|
||
|
|
body: jsonEncode({"studentId": studentId}),
|
||
|
|
);
|
||
|
|
if (response.statusCode == 200) {
|
||
|
|
await fetchStudents();
|
||
|
|
return (true, '💥 $studentName 학생의 계정이 영구 삭제되었습니다.');
|
||
|
|
}
|
||
|
|
return (false, '❌ 계정 삭제 통신 실패');
|
||
|
|
} catch (e) {
|
||
|
|
return (false, '❌ 계정 삭제 통신 실패');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// 🌐 신규 학생 계정 생성
|
||
|
|
Future<(bool success, String message)> createUser(
|
||
|
|
String studentId,
|
||
|
|
String name,
|
||
|
|
) async {
|
||
|
|
try {
|
||
|
|
final response = await http.post(
|
||
|
|
Uri.parse('$baseUrl/api/users/create'),
|
||
|
|
headers: {"Content-Type": "application/json"},
|
||
|
|
body: jsonEncode({"studentId": studentId, "name": name}),
|
||
|
|
);
|
||
|
|
final res = jsonDecode(response.body);
|
||
|
|
if (res['status'] == 'success') {
|
||
|
|
await fetchStudents();
|
||
|
|
return (true, '✅ ${res['message']}');
|
||
|
|
}
|
||
|
|
return (false, '❌ ${res['message']}');
|
||
|
|
} catch (e) {
|
||
|
|
return (false, '❌ 학생 등록 실패 (통신 에러)');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|