- 학생 계정 관리 화면에 학번으로 학년을 지정/변경하는 카드+다이얼로그 추가 - 계정 생성 성공 판정을 HTTP 상태코드가 아닌 응답 status 필드로 정확히 확인하도록 수정 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
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 TeacherStudentManagementController extends ChangeNotifier {
|
|
bool _isWorking = false;
|
|
bool get isWorking => _isWorking;
|
|
|
|
/// ➕ 학생 계정 추가. (성공여부, 메시지)를 반환한다 — UI가 성공했을 때만 입력칸을 비운다.
|
|
Future<(bool success, String message)> addStudentAccount({
|
|
required String studentId,
|
|
required String name,
|
|
required String password,
|
|
int? grade,
|
|
}) async {
|
|
_isWorking = true;
|
|
notifyListeners();
|
|
try {
|
|
final url = Uri.parse('$baseUrl/api/users/create');
|
|
final response = await http.post(
|
|
url,
|
|
headers: {"Content-Type": "application/json"},
|
|
body: jsonEncode({
|
|
"studentId": studentId,
|
|
"name": name,
|
|
"password": password,
|
|
"grade": grade,
|
|
}),
|
|
);
|
|
final result = jsonDecode(utf8.decode(response.bodyBytes));
|
|
|
|
if (response.statusCode == 200 && result['status'] == 'success') {
|
|
return (true, '✅ ${result['message'] ?? '계정이 생성되었습니다.'}');
|
|
} else {
|
|
return (false, '❌ 생성 실패: ${result['message'] ?? '오류 발생'}');
|
|
}
|
|
} catch (e) {
|
|
return (false, '🚨 네트워크 에러: $e');
|
|
} finally {
|
|
_isWorking = false;
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
/// 🏫 이미 만들어진 학생 계정의 학년을 지정/변경한다. grade가 null이면 "미배정"으로 되돌린다.
|
|
Future<(bool success, String message)> updateStudentGrade({
|
|
required String studentId,
|
|
int? grade,
|
|
}) async {
|
|
_isWorking = true;
|
|
notifyListeners();
|
|
try {
|
|
final url = Uri.parse('$baseUrl/api/users/update-grade');
|
|
final response = await http.post(
|
|
url,
|
|
headers: {"Content-Type": "application/json"},
|
|
body: jsonEncode({"studentId": studentId, "grade": grade}),
|
|
);
|
|
final result = jsonDecode(utf8.decode(response.bodyBytes));
|
|
|
|
if (response.statusCode == 200 && result['status'] == 'success') {
|
|
return (true, '✅ ${result['message'] ?? '학년이 지정되었습니다.'}');
|
|
} else {
|
|
return (false, '❌ 지정 실패: ${result['message'] ?? '오류 발생'}');
|
|
}
|
|
} catch (e) {
|
|
return (false, '🚨 네트워크 에러: $e');
|
|
} finally {
|
|
_isWorking = false;
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
/// ❌ 학생 계정 삭제.
|
|
Future<(bool success, String message)> deleteStudentAccount(
|
|
String studentId,
|
|
) async {
|
|
_isWorking = true;
|
|
notifyListeners();
|
|
final url = Uri.parse('$baseUrl/api/users/delete');
|
|
try {
|
|
final response = await http.delete(
|
|
url,
|
|
headers: {"Content-Type": "application/json"},
|
|
body: jsonEncode({"studentId": studentId}),
|
|
);
|
|
if (response.statusCode == 200) {
|
|
final resData = jsonDecode(utf8.decode(response.bodyBytes));
|
|
return (true, '✅ ${resData['message']}');
|
|
} else {
|
|
return (false, '❌ 삭제 실패하였습니다.');
|
|
}
|
|
} catch (e) {
|
|
return (false, '❌ 서버 에러 발생');
|
|
} finally {
|
|
_isWorking = false;
|
|
notifyListeners();
|
|
}
|
|
}
|
|
}
|