익명 커뮤니티 게시판(백판) 기능 추가
디시인사이드식 고정 카테고리 게시판 + 에타 스타일 익명(글 안에서만 유효한 익명 번호) 댓글. 신고는 자동 삭제 없이 운영자(교사/관리자) 검토 후 처리하도록 별도 신고 검토 화면을 대시보드에 추가. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
// 🗣️ 커뮤니티 게시판 - 카테고리별 게시글 목록 화면의 기능(서버 통신/상태) 담당 컨트롤러.
|
||||
import 'dart:convert';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import '../config.dart';
|
||||
|
||||
class BoardListController extends ChangeNotifier {
|
||||
final String category;
|
||||
final String studentId;
|
||||
|
||||
BoardListController({required this.category, required this.studentId});
|
||||
|
||||
bool _isLoading = true;
|
||||
bool _isLoadingMore = false;
|
||||
bool _hasMore = true;
|
||||
int _page = 1;
|
||||
List<dynamic> _posts = [];
|
||||
bool _disposed = false;
|
||||
|
||||
bool get isLoading => _isLoading;
|
||||
bool get isLoadingMore => _isLoadingMore;
|
||||
bool get hasMore => _hasMore;
|
||||
List<dynamic> get posts => _posts;
|
||||
|
||||
static const int _pageSize = 20;
|
||||
|
||||
void _safeNotify() {
|
||||
if (!_disposed) notifyListeners();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_disposed = true;
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> init() => refresh();
|
||||
|
||||
Future<void> refresh() async {
|
||||
_isLoading = true;
|
||||
_page = 1;
|
||||
_hasMore = true;
|
||||
_safeNotify();
|
||||
await _fetchPage(replace: true);
|
||||
_isLoading = false;
|
||||
_safeNotify();
|
||||
}
|
||||
|
||||
Future<void> loadMore() async {
|
||||
if (_isLoadingMore || !_hasMore || _isLoading) return;
|
||||
_isLoadingMore = true;
|
||||
_safeNotify();
|
||||
_page += 1;
|
||||
await _fetchPage(replace: false);
|
||||
_isLoadingMore = false;
|
||||
_safeNotify();
|
||||
}
|
||||
|
||||
Future<void> _fetchPage({required bool replace}) async {
|
||||
try {
|
||||
final response = await http.get(
|
||||
Uri.parse(
|
||||
'$baseUrl/api/board/posts?category=$category&studentId=$studentId&page=$_page',
|
||||
),
|
||||
);
|
||||
if (response.statusCode == 200) {
|
||||
final data = jsonDecode(utf8.decode(response.bodyBytes));
|
||||
final fetched = (data['posts'] as List<dynamic>?) ?? [];
|
||||
_posts = replace ? fetched : [..._posts, ...fetched];
|
||||
_hasMore = fetched.length >= _pageSize;
|
||||
}
|
||||
} catch (_) {
|
||||
// 조용히 무시 - 마지막으로 받아온 목록을 유지한다.
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
// 💬 커뮤니티 게시판 - 게시글 상세(+댓글) 화면의 기능(서버 통신/상태) 담당 컨트롤러.
|
||||
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');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
// 🚩 커뮤니티 게시판 - 신고 검토(운영자용) 화면의 기능(서버 통신/상태) 담당 컨트롤러.
|
||||
import 'dart:convert';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import '../config.dart';
|
||||
|
||||
class BoardReportController extends ChangeNotifier {
|
||||
final String reviewerId;
|
||||
|
||||
BoardReportController({required this.reviewerId});
|
||||
|
||||
bool _isLoading = true;
|
||||
bool _isBusy = false;
|
||||
List<dynamic> _reports = [];
|
||||
bool _disposed = false;
|
||||
|
||||
bool get isLoading => _isLoading;
|
||||
bool get isBusy => _isBusy;
|
||||
List<dynamic> get reports => _reports;
|
||||
|
||||
void _safeNotify() {
|
||||
if (!_disposed) notifyListeners();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_disposed = true;
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> init() => fetchPending();
|
||||
|
||||
Future<void> fetchPending() async {
|
||||
_isLoading = true;
|
||||
_safeNotify();
|
||||
try {
|
||||
final response = await http.get(
|
||||
Uri.parse('$baseUrl/api/board/reports?status=pending'),
|
||||
);
|
||||
if (response.statusCode == 200) {
|
||||
final data = jsonDecode(utf8.decode(response.bodyBytes));
|
||||
_reports = data['reports'] ?? [];
|
||||
}
|
||||
} catch (_) {
|
||||
// 조용히 무시 - 마지막으로 받아온 목록을 유지한다.
|
||||
} finally {
|
||||
_isLoading = false;
|
||||
_safeNotify();
|
||||
}
|
||||
}
|
||||
|
||||
Future<(bool success, String message)> resolve(
|
||||
int reportId,
|
||||
String action,
|
||||
) async {
|
||||
_isBusy = true;
|
||||
_safeNotify();
|
||||
try {
|
||||
final response = await http.post(
|
||||
Uri.parse('$baseUrl/api/board/reports/$reportId/resolve'),
|
||||
headers: {"Content-Type": "application/json"},
|
||||
body: jsonEncode({"reviewerId": reviewerId, "action": action}),
|
||||
);
|
||||
final result = jsonDecode(utf8.decode(response.bodyBytes));
|
||||
if (response.statusCode == 200 && result['status'] == 'success') {
|
||||
await fetchPending();
|
||||
return (true, '${result['message'] ?? '처리되었습니다.'}');
|
||||
}
|
||||
return (false, '${result['message'] ?? '처리 실패'}');
|
||||
} catch (e) {
|
||||
return (false, '네트워크 에러: $e');
|
||||
} finally {
|
||||
_isBusy = false;
|
||||
_safeNotify();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
// ✍️ 커뮤니티 게시판 - 글쓰기 화면의 기능(서버 통신/상태) 담당 컨트롤러.
|
||||
import 'dart:convert';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import '../config.dart';
|
||||
|
||||
class BoardWriteController extends ChangeNotifier {
|
||||
final String studentId;
|
||||
|
||||
BoardWriteController({required this.studentId});
|
||||
|
||||
bool _isSubmitting = false;
|
||||
bool get isSubmitting => _isSubmitting;
|
||||
|
||||
Future<(bool success, String message)> submit({
|
||||
required String category,
|
||||
required String title,
|
||||
required String content,
|
||||
}) async {
|
||||
if (title.trim().isEmpty || content.trim().isEmpty) {
|
||||
return (false, '제목과 내용을 입력해주세요.');
|
||||
}
|
||||
_isSubmitting = true;
|
||||
notifyListeners();
|
||||
try {
|
||||
final response = await http.post(
|
||||
Uri.parse('$baseUrl/api/board/posts'),
|
||||
headers: {"Content-Type": "application/json"},
|
||||
body: jsonEncode({
|
||||
"studentId": studentId,
|
||||
"category": category,
|
||||
"title": title.trim(),
|
||||
"content": content.trim(),
|
||||
}),
|
||||
);
|
||||
final result = jsonDecode(utf8.decode(response.bodyBytes));
|
||||
if (response.statusCode == 200 && result['status'] == 'success') {
|
||||
return (true, '게시글이 등록되었습니다.');
|
||||
}
|
||||
return (false, '${result['message'] ?? '게시글 등록 실패'}');
|
||||
} catch (e) {
|
||||
return (false, '네트워크 에러: $e');
|
||||
} finally {
|
||||
_isSubmitting = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user