사용하지 않는 파일 정리 전 백업 (Phase 10 완료 후 상태)
This commit is contained in:
160
lib/services/administrator_service.dart
Normal file
160
lib/services/administrator_service.dart
Normal file
@@ -0,0 +1,160 @@
|
||||
import 'package:injectable/injectable.dart';
|
||||
import 'package:superport/data/datasources/remote/administrator_remote_datasource.dart';
|
||||
import 'package:superport/data/models/administrator_dto.dart';
|
||||
|
||||
/// 관리자 서비스 (백엔드 Administrator 테이블)
|
||||
/// HTTP API 호출을 담당하는 서비스 레이어
|
||||
@lazySingleton
|
||||
class AdministratorService {
|
||||
final AdministratorRemoteDataSource _remoteDataSource;
|
||||
|
||||
AdministratorService(this._remoteDataSource);
|
||||
|
||||
/// 관리자 목록 조회 (페이지네이션 지원)
|
||||
Future<AdministratorListResponse> getAdministrators({
|
||||
int page = 1,
|
||||
int pageSize = 20,
|
||||
String? search,
|
||||
}) async {
|
||||
try {
|
||||
return await _remoteDataSource.getAdministrators(
|
||||
page: page,
|
||||
pageSize: pageSize,
|
||||
search: search,
|
||||
);
|
||||
} catch (e) {
|
||||
throw Exception('관리자 목록 조회 실패: ${e.toString()}');
|
||||
}
|
||||
}
|
||||
|
||||
/// 특정 관리자 조회
|
||||
Future<AdministratorDto> getAdministrator(int id) async {
|
||||
try {
|
||||
return await _remoteDataSource.getAdministrator(id);
|
||||
} catch (e) {
|
||||
throw Exception('관리자 조회 실패: ${e.toString()}');
|
||||
}
|
||||
}
|
||||
|
||||
/// 관리자 계정 생성
|
||||
Future<AdministratorDto> createAdministrator({
|
||||
required String name,
|
||||
required String phone,
|
||||
required String mobile,
|
||||
required String email,
|
||||
required String password,
|
||||
}) async {
|
||||
try {
|
||||
final request = AdministratorRequestDto(
|
||||
name: name,
|
||||
phone: phone,
|
||||
mobile: mobile,
|
||||
email: email,
|
||||
passwd: password,
|
||||
);
|
||||
|
||||
return await _remoteDataSource.createAdministrator(request);
|
||||
} catch (e) {
|
||||
throw Exception('관리자 계정 생성 실패: ${e.toString()}');
|
||||
}
|
||||
}
|
||||
|
||||
/// 관리자 정보 수정
|
||||
Future<AdministratorDto> updateAdministrator(
|
||||
int id, {
|
||||
String? name,
|
||||
String? phone,
|
||||
String? mobile,
|
||||
String? email,
|
||||
String? password,
|
||||
}) async {
|
||||
try {
|
||||
final request = AdministratorUpdateRequestDto(
|
||||
name: name,
|
||||
phone: phone,
|
||||
mobile: mobile,
|
||||
email: email,
|
||||
passwd: password,
|
||||
);
|
||||
|
||||
return await _remoteDataSource.updateAdministrator(id, request);
|
||||
} catch (e) {
|
||||
throw Exception('관리자 정보 수정 실패: ${e.toString()}');
|
||||
}
|
||||
}
|
||||
|
||||
/// 관리자 계정 삭제
|
||||
Future<void> deleteAdministrator(int id) async {
|
||||
try {
|
||||
await _remoteDataSource.deleteAdministrator(id);
|
||||
} catch (e) {
|
||||
throw Exception('관리자 계정 삭제 실패: ${e.toString()}');
|
||||
}
|
||||
}
|
||||
|
||||
/// 이메일 중복 확인
|
||||
Future<bool> checkEmailAvailability(String email, {int? excludeId}) async {
|
||||
try {
|
||||
return await _remoteDataSource.checkEmailAvailability(
|
||||
email,
|
||||
excludeId: excludeId,
|
||||
);
|
||||
} catch (e) {
|
||||
// 에러 발생 시 안전하게 false 반환 (사용 불가로 처리)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// 이메일 중복 여부 확인 (편의 메서드)
|
||||
Future<bool> isDuplicateEmail(String email, {int? excludeId}) async {
|
||||
try {
|
||||
final isAvailable = await checkEmailAvailability(email, excludeId: excludeId);
|
||||
return !isAvailable; // 사용 가능하면 중복이 아님
|
||||
} catch (e) {
|
||||
// 에러 발생 시 안전하게 중복으로 처리
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// 관리자 인증 (로그인)
|
||||
Future<AdministratorDto> authenticateAdministrator(
|
||||
String email,
|
||||
String password,
|
||||
) async {
|
||||
try {
|
||||
return await _remoteDataSource.authenticateAdministrator(email, password);
|
||||
} catch (e) {
|
||||
throw Exception('관리자 인증 실패: ${e.toString()}');
|
||||
}
|
||||
}
|
||||
|
||||
/// 관리자 전체 수 조회 (통계용)
|
||||
Future<int> getAdministratorCount() async {
|
||||
try {
|
||||
final response = await getAdministrators(page: 1, pageSize: 1);
|
||||
return response.totalCount;
|
||||
} catch (e) {
|
||||
throw Exception('관리자 수 조회 실패: ${e.toString()}');
|
||||
}
|
||||
}
|
||||
|
||||
/// 이메일로 관리자 검색 (단일 결과 기대)
|
||||
Future<AdministratorDto?> findAdministratorByEmail(String email) async {
|
||||
try {
|
||||
final response = await getAdministrators(search: email, pageSize: 10);
|
||||
|
||||
// 정확히 일치하는 이메일 찾기
|
||||
final exactMatch = response.items.where((admin) =>
|
||||
admin.email.toLowerCase() == email.toLowerCase()).firstOrNull;
|
||||
|
||||
return exactMatch;
|
||||
} catch (e) {
|
||||
throw Exception('이메일로 관리자 검색 실패: ${e.toString()}');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// List 확장 메서드 (firstOrNull이 없는 Dart 버전 대응)
|
||||
extension ListExtension<T> on List<T> {
|
||||
T? get firstOrNull => isEmpty ? null : first;
|
||||
}
|
||||
Reference in New Issue
Block a user