- CompanyService 및 RemoteDataSource 구현 - Company, Branch DTO 모델 생성 (Freezed) - 의존성 주입 컨테이너 업데이트 - 회사 등록/수정 폼에 API 연동 로직 적용 - API 통합 계획 문서 업데이트
253 lines
7.5 KiB
Dart
253 lines
7.5 KiB
Dart
import 'package:dio/dio.dart';
|
|
import 'package:get_it/get_it.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/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({
|
|
int page = 1,
|
|
int perPage = 20,
|
|
String? search,
|
|
bool? isActive,
|
|
});
|
|
|
|
Future<CompanyResponse> createCompany(CreateCompanyRequest request);
|
|
|
|
Future<CompanyResponse> getCompanyDetail(int id);
|
|
|
|
Future<CompanyWithBranches> getCompanyWithBranches(int id);
|
|
|
|
Future<CompanyResponse> updateCompany(int id, UpdateCompanyRequest request);
|
|
|
|
Future<void> deleteCompany(int id);
|
|
|
|
Future<List<CompanyNameDto>> getCompanyNames();
|
|
|
|
// Branch related methods
|
|
Future<BranchResponse> createBranch(int companyId, CreateBranchRequest request);
|
|
|
|
Future<BranchResponse> getBranchDetail(int companyId, int branchId);
|
|
|
|
Future<BranchResponse> updateBranch(int companyId, int branchId, UpdateBranchRequest request);
|
|
|
|
Future<void> deleteBranch(int companyId, int branchId);
|
|
|
|
Future<List<BranchListDto>> getCompanyBranches(int companyId);
|
|
}
|
|
|
|
class CompanyRemoteDataSourceImpl implements CompanyRemoteDataSource {
|
|
final ApiClient _apiClient = GetIt.instance<ApiClient>();
|
|
|
|
@override
|
|
Future<List<CompanyListDto>> getCompanies({
|
|
int page = 1,
|
|
int perPage = 20,
|
|
String? search,
|
|
bool? isActive,
|
|
}) async {
|
|
try {
|
|
final queryParams = {
|
|
'page': page,
|
|
'per_page': perPage,
|
|
if (search != null) 'search': search,
|
|
if (isActive != null) 'is_active': isActive,
|
|
};
|
|
|
|
final response = await _apiClient.dio.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,
|
|
);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<CompanyResponse> createCompany(CreateCompanyRequest request) async {
|
|
try {
|
|
final response = await _apiClient.dio.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,
|
|
);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<CompanyResponse> getCompanyDetail(int id) async {
|
|
try {
|
|
final response = await _apiClient.dio.get(
|
|
'${ApiEndpoints.companies}/$id',
|
|
);
|
|
|
|
return CompanyResponse.fromJson(response.data['data']);
|
|
} on DioException catch (e) {
|
|
throw ServerException(
|
|
message: e.response?.data['message'] ?? 'Failed to fetch company detail',
|
|
code: e.response?.statusCode,
|
|
);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<CompanyWithBranches> getCompanyWithBranches(int id) async {
|
|
try {
|
|
final response = await _apiClient.dio.get(
|
|
'${ApiEndpoints.companies}/$id/with-branches',
|
|
);
|
|
|
|
return CompanyWithBranches.fromJson(response.data['data']);
|
|
} on DioException catch (e) {
|
|
throw ServerException(
|
|
message: e.response?.data['message'] ?? 'Failed to fetch company with branches',
|
|
code: e.response?.statusCode,
|
|
);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<CompanyResponse> updateCompany(int id, UpdateCompanyRequest request) async {
|
|
try {
|
|
final response = await _apiClient.dio.put(
|
|
'${ApiEndpoints.companies}/$id',
|
|
data: request.toJson(),
|
|
);
|
|
|
|
return CompanyResponse.fromJson(response.data['data']);
|
|
} on DioException catch (e) {
|
|
throw ServerException(
|
|
message: e.response?.data['message'] ?? 'Failed to update company',
|
|
code: e.response?.statusCode,
|
|
);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<void> deleteCompany(int id) async {
|
|
try {
|
|
await _apiClient.dio.delete(
|
|
'${ApiEndpoints.companies}/$id',
|
|
);
|
|
} on DioException catch (e) {
|
|
throw ServerException(
|
|
message: e.response?.data['message'] ?? 'Failed to delete company',
|
|
code: e.response?.statusCode,
|
|
);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<List<CompanyNameDto>> getCompanyNames() async {
|
|
try {
|
|
final response = await _apiClient.dio.get(
|
|
'${ApiEndpoints.companies}/names',
|
|
);
|
|
|
|
final List<dynamic> data = response.data['data'];
|
|
return data.map((json) => CompanyNameDto.fromJson(json)).toList();
|
|
} on DioException catch (e) {
|
|
throw ServerException(
|
|
message: e.response?.data['message'] ?? 'Failed to fetch company names',
|
|
code: e.response?.statusCode,
|
|
);
|
|
}
|
|
}
|
|
|
|
// Branch methods
|
|
@override
|
|
Future<BranchResponse> createBranch(int companyId, CreateBranchRequest request) async {
|
|
try {
|
|
final response = await _apiClient.dio.post(
|
|
'${ApiEndpoints.companies}/$companyId/branches',
|
|
data: request.toJson(),
|
|
);
|
|
|
|
return BranchResponse.fromJson(response.data['data']);
|
|
} on DioException catch (e) {
|
|
throw ServerException(
|
|
message: e.response?.data['message'] ?? 'Failed to create branch',
|
|
code: e.response?.statusCode,
|
|
);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<BranchResponse> getBranchDetail(int companyId, int branchId) async {
|
|
try {
|
|
final response = await _apiClient.dio.get(
|
|
'${ApiEndpoints.companies}/$companyId/branches/$branchId',
|
|
);
|
|
|
|
return BranchResponse.fromJson(response.data['data']);
|
|
} on DioException catch (e) {
|
|
throw ServerException(
|
|
message: e.response?.data['message'] ?? 'Failed to fetch branch detail',
|
|
code: e.response?.statusCode,
|
|
);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<BranchResponse> updateBranch(int companyId, int branchId, UpdateBranchRequest request) async {
|
|
try {
|
|
final response = await _apiClient.dio.put(
|
|
'${ApiEndpoints.companies}/$companyId/branches/$branchId',
|
|
data: request.toJson(),
|
|
);
|
|
|
|
return BranchResponse.fromJson(response.data['data']);
|
|
} on DioException catch (e) {
|
|
throw ServerException(
|
|
message: e.response?.data['message'] ?? 'Failed to update branch',
|
|
code: e.response?.statusCode,
|
|
);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<void> deleteBranch(int companyId, int branchId) async {
|
|
try {
|
|
await _apiClient.dio.delete(
|
|
'${ApiEndpoints.companies}/$companyId/branches/$branchId',
|
|
);
|
|
} on DioException catch (e) {
|
|
throw ServerException(
|
|
message: e.response?.data['message'] ?? 'Failed to delete branch',
|
|
code: e.response?.statusCode,
|
|
);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<List<BranchListDto>> getCompanyBranches(int companyId) async {
|
|
try {
|
|
final response = await _apiClient.dio.get(
|
|
'${ApiEndpoints.companies}/$companyId/branches',
|
|
);
|
|
|
|
final List<dynamic> data = response.data['data'];
|
|
return data.map((json) => BranchListDto.fromJson(json)).toList();
|
|
} on DioException catch (e) {
|
|
throw ServerException(
|
|
message: e.response?.data['message'] ?? 'Failed to fetch company branches',
|
|
code: e.response?.statusCode,
|
|
);
|
|
}
|
|
}
|
|
} |