refactor: Repository 패턴 적용 및 Clean Architecture 완성
## 주요 변경사항 ### 🏗️ Architecture - Repository 패턴 전면 도입 (인터페이스/구현체 분리) - Domain Layer에 Repository 인터페이스 정의 - Data Layer에 Repository 구현체 배치 - UseCase 의존성을 Service에서 Repository로 전환 ### 📦 Dependency Injection - GetIt 기반 DI Container 재구성 (lib/injection_container.dart) - Repository 인터페이스와 구현체 등록 - Service와 Repository 공존 (마이그레이션 기간) ### 🔄 Migration Status 완료: - License 모듈 (6개 UseCase) - Warehouse Location 모듈 (5개 UseCase) 진행중: - Auth 모듈 (2/5 UseCase) - Company 모듈 (1/6 UseCase) 대기: - User 모듈 (7개 UseCase) - Equipment 모듈 (4개 UseCase) ### 🎯 Controller 통합 - 중복 Controller 제거 (with_usecase 버전) - 단일 Controller로 통합 - UseCase 패턴 직접 적용 ### 🧹 코드 정리 - 임시 파일 제거 (test_*.md, task.md) - Node.js 아티팩트 제거 (package.json) - 불필요한 테스트 파일 정리 ### ✅ 테스트 개선 - Real API 중심 테스트 구조 - Mock 제거, 실제 API 엔드포인트 사용 - 통합 테스트 프레임워크 강화 ## 기술적 영향 - 의존성 역전 원칙 적용 - 레이어 간 결합도 감소 - 테스트 용이성 향상 - 확장성 및 유지보수성 개선 ## 다음 단계 1. User/Equipment 모듈 Repository 마이그레이션 2. Service Layer 점진적 제거 3. 캐싱 전략 구현 4. 성능 최적화
This commit is contained in:
100
lib/data/datasources/interceptors/api_interceptor.dart
Normal file
100
lib/data/datasources/interceptors/api_interceptor.dart
Normal file
@@ -0,0 +1,100 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import '../../../core/storage/secure_storage.dart';
|
||||
|
||||
/// API 요청/응답 인터셉터
|
||||
class ApiInterceptor extends Interceptor {
|
||||
final SecureStorage _storage;
|
||||
|
||||
ApiInterceptor(this._storage);
|
||||
|
||||
@override
|
||||
void onRequest(
|
||||
RequestOptions options,
|
||||
RequestInterceptorHandler handler,
|
||||
) async {
|
||||
// 토큰 추가
|
||||
final token = await _storage.getAccessToken();
|
||||
if (token != null) {
|
||||
options.headers['Authorization'] = 'Bearer $token';
|
||||
}
|
||||
|
||||
// Content-Type 헤더 추가
|
||||
if (options.data != null && options.data is! FormData) {
|
||||
options.headers['Content-Type'] = 'application/json';
|
||||
}
|
||||
|
||||
// 디버그 모드에서 요청 로깅
|
||||
if (kDebugMode) {
|
||||
print('🚀 API Request: ${options.method} ${options.path}');
|
||||
if (options.queryParameters.isNotEmpty) {
|
||||
print(' Query: ${options.queryParameters}');
|
||||
}
|
||||
if (options.data != null) {
|
||||
print(' Body: ${options.data}');
|
||||
}
|
||||
}
|
||||
|
||||
handler.next(options);
|
||||
}
|
||||
|
||||
@override
|
||||
void onResponse(
|
||||
Response response,
|
||||
ResponseInterceptorHandler handler,
|
||||
) {
|
||||
// 디버그 모드에서 응답 로깅
|
||||
if (kDebugMode) {
|
||||
print('✅ API Response: ${response.statusCode} ${response.requestOptions.path}');
|
||||
if (response.data != null) {
|
||||
print(' Data: ${response.data}');
|
||||
}
|
||||
}
|
||||
|
||||
handler.next(response);
|
||||
}
|
||||
|
||||
@override
|
||||
void onError(
|
||||
DioException err,
|
||||
ErrorInterceptorHandler handler,
|
||||
) async {
|
||||
// 디버그 모드에서 에러 로깅
|
||||
if (kDebugMode) {
|
||||
print('❌ API Error: ${err.requestOptions.path}');
|
||||
print(' Error Type: ${err.type}');
|
||||
print(' Message: ${err.message}');
|
||||
if (err.response != null) {
|
||||
print(' Status: ${err.response?.statusCode}');
|
||||
print(' Data: ${err.response?.data}');
|
||||
}
|
||||
}
|
||||
|
||||
// 401 Unauthorized 처리
|
||||
if (err.response?.statusCode == 401) {
|
||||
// 토큰 갱신 시도
|
||||
final refreshToken = await _storage.getRefreshToken();
|
||||
if (refreshToken != null) {
|
||||
try {
|
||||
// 토큰 갱신 로직 (필요시 구현)
|
||||
// final newToken = await _refreshToken(refreshToken);
|
||||
// await _storage.saveTokens(newToken);
|
||||
|
||||
// 원래 요청 재시도
|
||||
// final options = err.requestOptions;
|
||||
// options.headers['Authorization'] = 'Bearer $newToken';
|
||||
// final response = await dio.fetch(options);
|
||||
// return handler.resolve(response);
|
||||
} catch (e) {
|
||||
// 토큰 갱신 실패 시 로그아웃 처리
|
||||
await _storage.clearAll();
|
||||
}
|
||||
} else {
|
||||
// 리프레시 토큰이 없으면 로그아웃 처리
|
||||
await _storage.clearAll();
|
||||
}
|
||||
}
|
||||
|
||||
handler.next(err);
|
||||
}
|
||||
}
|
||||
@@ -88,13 +88,13 @@ class LoggingInterceptor extends Interceptor {
|
||||
debugPrint('║ $line');
|
||||
});
|
||||
debugPrint('║ ... (${lines.length - 50} lines omitted) ...');
|
||||
lines.skip(lines.length - 25).forEach((line) {
|
||||
for (final line in lines.skip(lines.length - 25)) {
|
||||
debugPrint('║ $line');
|
||||
});
|
||||
}
|
||||
} else {
|
||||
lines.forEach((line) {
|
||||
for (final line in lines) {
|
||||
debugPrint('║ $line');
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint('║ ${response.data}');
|
||||
|
||||
@@ -4,11 +4,37 @@ import 'package:superport/core/errors/exceptions.dart';
|
||||
import 'package:superport/data/datasources/remote/api_client.dart';
|
||||
import 'package:superport/data/models/user/user_dto.dart';
|
||||
|
||||
@lazySingleton
|
||||
class UserRemoteDataSource {
|
||||
abstract class UserRemoteDataSource {
|
||||
Future<UserListDto> getUsers({
|
||||
int page = 1,
|
||||
int perPage = 20,
|
||||
bool? isActive,
|
||||
int? companyId,
|
||||
String? role,
|
||||
});
|
||||
|
||||
Future<UserDto> getUser(int id);
|
||||
Future<UserDto> createUser(CreateUserRequest request);
|
||||
Future<UserDto> updateUser(int id, UpdateUserRequest request);
|
||||
Future<void> deleteUser(int id);
|
||||
Future<UserDto> changeUserStatus(int id, ChangeStatusRequest request);
|
||||
Future<void> changePassword(int id, ChangePasswordRequest request);
|
||||
Future<bool> checkDuplicateUsername(String username);
|
||||
Future<UserListDto> searchUsers({
|
||||
required String query,
|
||||
int? companyId,
|
||||
String? status,
|
||||
String? permissionLevel,
|
||||
int page = 1,
|
||||
int perPage = 20,
|
||||
});
|
||||
}
|
||||
|
||||
@LazySingleton(as: UserRemoteDataSource)
|
||||
class UserRemoteDataSourceImpl implements UserRemoteDataSource {
|
||||
final ApiClient _apiClient;
|
||||
|
||||
UserRemoteDataSource() : _apiClient = ApiClient();
|
||||
UserRemoteDataSourceImpl(this._apiClient);
|
||||
|
||||
/// 사용자 목록 조회
|
||||
Future<UserListDto> getUsers({
|
||||
@@ -40,7 +66,7 @@ class UserRemoteDataSource {
|
||||
// role이 null인 경우 기본값 설정
|
||||
final users = data.map((json) {
|
||||
if (json['role'] == null) {
|
||||
json['role'] = 'staff'; // 기본값
|
||||
json['role'] = 'member'; // 기본값
|
||||
}
|
||||
return UserDto.fromJson(json);
|
||||
}).toList();
|
||||
|
||||
@@ -23,6 +23,11 @@ abstract class WarehouseLocationRemoteDataSource {
|
||||
int page = 1,
|
||||
int perPage = 20,
|
||||
});
|
||||
|
||||
// Repository에서 사용하는 추가 메서드들
|
||||
Future<void> updateWarehouseLocationStatus(int id, bool isActive);
|
||||
Future<bool> checkWarehouseHasEquipment(int id);
|
||||
Future<bool> checkDuplicateWarehouseName(String name);
|
||||
}
|
||||
|
||||
@LazySingleton(as: WarehouseLocationRemoteDataSource)
|
||||
@@ -266,6 +271,56 @@ class WarehouseLocationRemoteDataSourceImpl implements WarehouseLocationRemoteDa
|
||||
}
|
||||
}
|
||||
|
||||
// Repository에서 사용하는 추가 메서드들 구현
|
||||
@override
|
||||
Future<void> updateWarehouseLocationStatus(int id, bool isActive) async {
|
||||
try {
|
||||
await _apiClient.patch(
|
||||
'${ApiEndpoints.warehouseLocations}/$id/status',
|
||||
data: {'is_active': isActive},
|
||||
);
|
||||
} catch (e) {
|
||||
throw _handleError(e);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> checkWarehouseHasEquipment(int id) async {
|
||||
try {
|
||||
final response = await _apiClient.get(
|
||||
'${ApiEndpoints.warehouseLocations}/$id/has-equipment',
|
||||
);
|
||||
|
||||
if (response.data != null && response.data['success'] == true) {
|
||||
return response.data['data']['has_equipment'] ?? false;
|
||||
}
|
||||
return false;
|
||||
} catch (e) {
|
||||
// 오류 시 기본값 false 반환
|
||||
debugPrint('📦 창고 장비 보유 여부 확인 중 오류: $e');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> checkDuplicateWarehouseName(String name) async {
|
||||
try {
|
||||
final response = await _apiClient.get(
|
||||
'${ApiEndpoints.warehouseLocations}/check-duplicate',
|
||||
queryParameters: {'name': name},
|
||||
);
|
||||
|
||||
if (response.data != null && response.data['success'] == true) {
|
||||
return response.data['data']['is_duplicate'] ?? false;
|
||||
}
|
||||
return false;
|
||||
} catch (e) {
|
||||
// 오류 시 기본값 false 반환 (중복이 아니라고 가정)
|
||||
debugPrint('📦 중복 창고명 확인 중 오류: $e');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Exception _handleError(dynamic error) {
|
||||
if (error is ApiException) {
|
||||
return error;
|
||||
|
||||
Reference in New Issue
Block a user