디시인사이드식 고정 카테고리 게시판 + 에타 스타일 익명(글 안에서만 유효한 익명 번호) 댓글. 신고는 자동 삭제 없이 운영자(교사/관리자) 검토 후 처리하도록 별도 신고 검토 화면을 대시보드에 추가. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
144 lines
4.4 KiB
Dart
144 lines
4.4 KiB
Dart
// 💬 커뮤니티 게시판 - 게시글 상세(+댓글) 화면의 기능(서버 통신/상태) 담당 컨트롤러.
|
|
import 'dart:convert';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import '../config.dart';
|
|
|
|
class BoardPostController extends ChangeNotifier {
|
|
final int postId;
|
|
final String studentId;
|
|
|
|
BoardPostController({required this.postId, required this.studentId});
|
|
|
|
bool _isLoading = true;
|
|
bool _isBusy = false;
|
|
bool _postDeleted = false;
|
|
Map<String, dynamic>? _post;
|
|
List<dynamic> _comments = [];
|
|
bool _disposed = false;
|
|
|
|
bool get isLoading => _isLoading;
|
|
bool get isBusy => _isBusy;
|
|
bool get postDeleted => _postDeleted;
|
|
Map<String, dynamic>? get post => _post;
|
|
List<dynamic> get comments => _comments;
|
|
|
|
void _safeNotify() {
|
|
if (!_disposed) notifyListeners();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_disposed = true;
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> init() => fetchDetail();
|
|
|
|
Future<void> fetchDetail() async {
|
|
_isLoading = true;
|
|
_safeNotify();
|
|
try {
|
|
final response = await http.get(
|
|
Uri.parse('$baseUrl/api/board/posts/$postId?studentId=$studentId'),
|
|
);
|
|
final result = jsonDecode(utf8.decode(response.bodyBytes));
|
|
if (response.statusCode == 200 && result['status'] == 'success') {
|
|
_post = Map<String, dynamic>.from(result['post']);
|
|
_comments = result['comments'] ?? [];
|
|
} else {
|
|
_postDeleted = true;
|
|
}
|
|
} catch (_) {
|
|
// 조용히 무시 - 마지막으로 받아온 상태를 유지한다.
|
|
} finally {
|
|
_isLoading = false;
|
|
_safeNotify();
|
|
}
|
|
}
|
|
|
|
Future<(bool success, String message)> addComment(String content) async {
|
|
if (content.trim().isEmpty) return (false, '댓글 내용을 입력해주세요.');
|
|
_isBusy = true;
|
|
_safeNotify();
|
|
try {
|
|
final response = await http.post(
|
|
Uri.parse('$baseUrl/api/board/posts/$postId/comments'),
|
|
headers: {"Content-Type": "application/json"},
|
|
body: jsonEncode({"studentId": studentId, "content": content.trim()}),
|
|
);
|
|
final result = jsonDecode(utf8.decode(response.bodyBytes));
|
|
if (response.statusCode == 200 && result['status'] == 'success') {
|
|
await fetchDetail();
|
|
return (true, '댓글이 등록되었습니다.');
|
|
}
|
|
return (false, '${result['message'] ?? '댓글 등록 실패'}');
|
|
} catch (e) {
|
|
return (false, '네트워크 에러: $e');
|
|
} finally {
|
|
_isBusy = false;
|
|
_safeNotify();
|
|
}
|
|
}
|
|
|
|
Future<(bool success, String message)> deletePost() async {
|
|
return _delete('$baseUrl/api/board/posts/$postId');
|
|
}
|
|
|
|
Future<(bool success, String message)> deleteComment(int commentId) async {
|
|
final (success, message) = await _delete(
|
|
'$baseUrl/api/board/comments/$commentId',
|
|
);
|
|
if (success) await fetchDetail();
|
|
return (success, message);
|
|
}
|
|
|
|
Future<(bool success, String message)> _delete(String url) async {
|
|
_isBusy = true;
|
|
_safeNotify();
|
|
try {
|
|
final response = await http.delete(
|
|
Uri.parse(url),
|
|
headers: {"Content-Type": "application/json"},
|
|
body: jsonEncode({"studentId": studentId}),
|
|
);
|
|
final result = jsonDecode(utf8.decode(response.bodyBytes));
|
|
if (response.statusCode == 200 && result['status'] == 'success') {
|
|
return (true, '${result['message'] ?? '삭제되었습니다.'}');
|
|
}
|
|
return (false, '${result['message'] ?? '삭제 실패'}');
|
|
} catch (e) {
|
|
return (false, '네트워크 에러: $e');
|
|
} finally {
|
|
_isBusy = false;
|
|
_safeNotify();
|
|
}
|
|
}
|
|
|
|
Future<(bool success, String message)> report({
|
|
required String targetType,
|
|
required int targetId,
|
|
String? reason,
|
|
}) async {
|
|
try {
|
|
final response = await http.post(
|
|
Uri.parse('$baseUrl/api/board/reports'),
|
|
headers: {"Content-Type": "application/json"},
|
|
body: jsonEncode({
|
|
"targetType": targetType,
|
|
"targetId": targetId,
|
|
"reporterStudentId": studentId,
|
|
"reason": reason,
|
|
}),
|
|
);
|
|
final result = jsonDecode(utf8.decode(response.bodyBytes));
|
|
if (response.statusCode == 200 && result['status'] == 'success') {
|
|
return (true, '${result['message'] ?? '신고가 접수되었습니다.'}');
|
|
}
|
|
return (false, '${result['message'] ?? '신고 접수 실패'}');
|
|
} catch (e) {
|
|
return (false, '네트워크 에러: $e');
|
|
}
|
|
}
|
|
}
|