feat: 사용자 관리 API 연동 구현
- UserRemoteDataSource: 사용자 CRUD, 상태 변경, 비밀번호 변경, 중복 확인 API 구현 - UserService: DTO-Model 변환 로직 및 역할/전화번호 매핑 처리 - UserListController: ChangeNotifier 패턴 적용, 페이지네이션, 검색, 필터링 기능 추가 - UserFormController: API 연동, username 중복 확인 기능 추가 - user_form.dart: username/password 필드 추가 및 실시간 검증 - user_list_redesign.dart: Provider 패턴 적용, 무한 스크롤 구현 - equipment_out_form_controller.dart: 구문 오류 수정 - API 통합 진행률: 85% (사용자 관리 100% 완료)
This commit is contained in:
@@ -1,14 +1,17 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
import 'package:injectable/injectable.dart';
|
||||
import 'package:superport/core/constants/api_endpoints.dart';
|
||||
import 'package:superport/core/errors/exceptions.dart';
|
||||
import 'package:superport/data/datasources/remote/api_client.dart';
|
||||
import 'package:superport/data/models/common/api_response.dart';
|
||||
import 'package:superport/data/models/common/paginated_response.dart';
|
||||
import 'package:superport/data/models/company/company_dto.dart';
|
||||
import 'package:superport/data/models/company/company_list_dto.dart';
|
||||
import 'package:superport/data/models/company/branch_dto.dart';
|
||||
|
||||
abstract class CompanyRemoteDataSource {
|
||||
Future<List<CompanyListDto>> getCompanies({
|
||||
Future<PaginatedResponse<CompanyListDto>> getCompanies({
|
||||
int page = 1,
|
||||
int perPage = 20,
|
||||
String? search,
|
||||
@@ -27,6 +30,14 @@ abstract class CompanyRemoteDataSource {
|
||||
|
||||
Future<List<CompanyNameDto>> getCompanyNames();
|
||||
|
||||
Future<List<CompanyWithBranches>> getCompaniesWithBranches();
|
||||
|
||||
Future<bool> checkDuplicateCompany(String name);
|
||||
|
||||
Future<List<CompanyListDto>> searchCompanies(String query);
|
||||
|
||||
Future<void> updateCompanyStatus(int id, bool isActive);
|
||||
|
||||
// Branch related methods
|
||||
Future<BranchResponse> createBranch(int companyId, CreateBranchRequest request);
|
||||
|
||||
@@ -39,11 +50,14 @@ abstract class CompanyRemoteDataSource {
|
||||
Future<List<BranchListDto>> getCompanyBranches(int companyId);
|
||||
}
|
||||
|
||||
@LazySingleton(as: CompanyRemoteDataSource)
|
||||
class CompanyRemoteDataSourceImpl implements CompanyRemoteDataSource {
|
||||
final ApiClient _apiClient = GetIt.instance<ApiClient>();
|
||||
final ApiClient _apiClient;
|
||||
|
||||
CompanyRemoteDataSourceImpl(this._apiClient);
|
||||
|
||||
@override
|
||||
Future<List<CompanyListDto>> getCompanies({
|
||||
Future<PaginatedResponse<CompanyListDto>> getCompanies({
|
||||
int page = 1,
|
||||
int perPage = 20,
|
||||
String? search,
|
||||
@@ -57,35 +71,55 @@ class CompanyRemoteDataSourceImpl implements CompanyRemoteDataSource {
|
||||
if (isActive != null) 'is_active': isActive,
|
||||
};
|
||||
|
||||
final response = await _apiClient.dio.get(
|
||||
final response = await _apiClient.get(
|
||||
ApiEndpoints.companies,
|
||||
queryParameters: queryParams,
|
||||
);
|
||||
|
||||
final List<dynamic> data = response.data['data'];
|
||||
return data.map((json) => CompanyListDto.fromJson(json)).toList();
|
||||
} on DioException catch (e) {
|
||||
throw ServerException(
|
||||
message: e.response?.data['message'] ?? 'Failed to fetch companies',
|
||||
code: e.response?.statusCode,
|
||||
);
|
||||
if (response.statusCode == 200) {
|
||||
final apiResponse = ApiResponse<PaginatedResponse<CompanyListDto>>.fromJson(
|
||||
response.data,
|
||||
(json) => PaginatedResponse<CompanyListDto>.fromJson(
|
||||
json,
|
||||
(item) => CompanyListDto.fromJson(item),
|
||||
),
|
||||
);
|
||||
return apiResponse.data;
|
||||
} else {
|
||||
throw ApiException(
|
||||
message: 'Failed to load companies',
|
||||
statusCode: response.statusCode,
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
if (e is ApiException) rethrow;
|
||||
throw ApiException(message: e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<CompanyResponse> createCompany(CreateCompanyRequest request) async {
|
||||
try {
|
||||
final response = await _apiClient.dio.post(
|
||||
final response = await _apiClient.post(
|
||||
ApiEndpoints.companies,
|
||||
data: request.toJson(),
|
||||
);
|
||||
|
||||
return CompanyResponse.fromJson(response.data['data']);
|
||||
} on DioException catch (e) {
|
||||
throw ServerException(
|
||||
message: e.response?.data['message'] ?? 'Failed to create company',
|
||||
code: e.response?.statusCode,
|
||||
);
|
||||
if (response.statusCode == 201) {
|
||||
final apiResponse = ApiResponse<CompanyResponse>.fromJson(
|
||||
response.data,
|
||||
(json) => CompanyResponse.fromJson(json),
|
||||
);
|
||||
return apiResponse.data;
|
||||
} else {
|
||||
throw ApiException(
|
||||
message: 'Failed to create company',
|
||||
statusCode: response.statusCode,
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
if (e is ApiException) rethrow;
|
||||
throw ApiException(message: e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -250,4 +284,103 @@ class CompanyRemoteDataSourceImpl implements CompanyRemoteDataSource {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<CompanyWithBranches>> getCompaniesWithBranches() async {
|
||||
try {
|
||||
final response = await _apiClient.get('${ApiEndpoints.companies}/branches');
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final apiResponse = ApiResponse<List<CompanyWithBranches>>.fromJson(
|
||||
response.data,
|
||||
(json) => (json as List)
|
||||
.map((item) => CompanyWithBranches.fromJson(item))
|
||||
.toList(),
|
||||
);
|
||||
return apiResponse.data;
|
||||
} else {
|
||||
throw ApiException(
|
||||
message: 'Failed to load companies with branches',
|
||||
statusCode: response.statusCode,
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
if (e is ApiException) rethrow;
|
||||
throw ApiException(message: e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> checkDuplicateCompany(String name) async {
|
||||
try {
|
||||
final response = await _apiClient.get(
|
||||
'${ApiEndpoints.companies}/check-duplicate',
|
||||
queryParameters: {'name': name},
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final apiResponse = ApiResponse<Map<String, dynamic>>.fromJson(
|
||||
response.data,
|
||||
(json) => json,
|
||||
);
|
||||
return apiResponse.data['exists'] ?? false;
|
||||
} else {
|
||||
throw ApiException(
|
||||
message: 'Failed to check duplicate',
|
||||
statusCode: response.statusCode,
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
if (e is ApiException) rethrow;
|
||||
throw ApiException(message: e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<CompanyListDto>> searchCompanies(String query) async {
|
||||
try {
|
||||
final response = await _apiClient.get(
|
||||
'${ApiEndpoints.companies}/search',
|
||||
queryParameters: {'q': query},
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final apiResponse = ApiResponse<List<CompanyListDto>>.fromJson(
|
||||
response.data,
|
||||
(json) => (json as List)
|
||||
.map((item) => CompanyListDto.fromJson(item))
|
||||
.toList(),
|
||||
);
|
||||
return apiResponse.data;
|
||||
} else {
|
||||
throw ApiException(
|
||||
message: 'Failed to search companies',
|
||||
statusCode: response.statusCode,
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
if (e is ApiException) rethrow;
|
||||
throw ApiException(message: e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> updateCompanyStatus(int id, bool isActive) async {
|
||||
try {
|
||||
final response = await _apiClient.patch(
|
||||
'${ApiEndpoints.companies}/$id/status',
|
||||
data: {'is_active': isActive},
|
||||
);
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
throw ApiException(
|
||||
message: 'Failed to update company status',
|
||||
statusCode: response.statusCode,
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
if (e is ApiException) rethrow;
|
||||
throw ApiException(message: e.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user