결재 템플릿 CRUD 화면과 컨트롤러 정식화
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
import '../../../../core/common/models/paginated_result.dart';
|
||||
import '../../../../core/network/api_client.dart';
|
||||
import '../../domain/entities/approval_template.dart';
|
||||
import '../../domain/repositories/approval_template_repository.dart';
|
||||
@@ -14,18 +15,23 @@ class ApprovalTemplateRepositoryRemote implements ApprovalTemplateRepository {
|
||||
static const _basePath = '/approval-templates';
|
||||
|
||||
@override
|
||||
Future<List<ApprovalTemplate>> list({bool activeOnly = true}) async {
|
||||
Future<PaginatedResult<ApprovalTemplate>> list({
|
||||
int page = 1,
|
||||
int pageSize = 20,
|
||||
String? query,
|
||||
bool? isActive,
|
||||
}) async {
|
||||
final response = await _api.get<Map<String, dynamic>>(
|
||||
_basePath,
|
||||
query: {'page': 1, 'page_size': 100, if (activeOnly) 'active': true},
|
||||
query: {
|
||||
'page': page,
|
||||
'page_size': pageSize,
|
||||
if (query != null && query.isNotEmpty) 'q': query,
|
||||
if (isActive != null) 'active': isActive,
|
||||
},
|
||||
options: Options(responseType: ResponseType.json),
|
||||
);
|
||||
final items = (response.data?['items'] as List<dynamic>? ?? [])
|
||||
.whereType<Map<String, dynamic>>()
|
||||
.map(ApprovalTemplateDto.fromJson)
|
||||
.map((dto) => dto.toEntity(includeSteps: false))
|
||||
.toList();
|
||||
return items;
|
||||
return ApprovalTemplateDto.parsePaginated(response.data);
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -43,4 +49,85 @@ class ApprovalTemplateRepositoryRemote implements ApprovalTemplateRepository {
|
||||
data,
|
||||
).toEntity(includeSteps: includeSteps);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<ApprovalTemplate> create(
|
||||
ApprovalTemplateInput input, {
|
||||
List<ApprovalTemplateStepInput> steps = const [],
|
||||
}) async {
|
||||
final response = await _api.post<Map<String, dynamic>>(
|
||||
_basePath,
|
||||
data: input.toCreatePayload(),
|
||||
options: Options(responseType: ResponseType.json),
|
||||
);
|
||||
final data = (response.data?['data'] as Map<String, dynamic>?) ?? {};
|
||||
final created = ApprovalTemplateDto.fromJson(
|
||||
data,
|
||||
).toEntity(includeSteps: false);
|
||||
if (steps.isNotEmpty) {
|
||||
await _postSteps(created.id, steps);
|
||||
}
|
||||
return fetchDetail(created.id, includeSteps: true);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<ApprovalTemplate> update(
|
||||
int id,
|
||||
ApprovalTemplateInput input, {
|
||||
List<ApprovalTemplateStepInput>? steps,
|
||||
}) async {
|
||||
await _api.patch<Map<String, dynamic>>(
|
||||
'$_basePath/$id',
|
||||
data: input.toUpdatePayload(id),
|
||||
options: Options(responseType: ResponseType.json),
|
||||
);
|
||||
if (steps != null) {
|
||||
await _patchSteps(id, steps);
|
||||
}
|
||||
return fetchDetail(id, includeSteps: true);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> delete(int id) async {
|
||||
await _api.delete<void>('$_basePath/$id');
|
||||
}
|
||||
|
||||
@override
|
||||
Future<ApprovalTemplate> restore(int id) async {
|
||||
final response = await _api.post<Map<String, dynamic>>(
|
||||
'$_basePath/$id/restore',
|
||||
options: Options(responseType: ResponseType.json),
|
||||
);
|
||||
final data = (response.data?['data'] as Map<String, dynamic>?) ?? {};
|
||||
return ApprovalTemplateDto.fromJson(data).toEntity(includeSteps: false);
|
||||
}
|
||||
|
||||
Future<void> _postSteps(
|
||||
int templateId,
|
||||
List<ApprovalTemplateStepInput> steps,
|
||||
) async {
|
||||
if (steps.isEmpty) return;
|
||||
await _api.post<Map<String, dynamic>>(
|
||||
'$_basePath/$templateId/steps',
|
||||
data: {
|
||||
'id': templateId,
|
||||
'steps': steps.map((step) => step.toJson(includeId: false)).toList(),
|
||||
},
|
||||
options: Options(responseType: ResponseType.json),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _patchSteps(
|
||||
int templateId,
|
||||
List<ApprovalTemplateStepInput> steps,
|
||||
) async {
|
||||
await _api.patch<Map<String, dynamic>>(
|
||||
'$_basePath/$templateId/steps',
|
||||
data: {
|
||||
'id': templateId,
|
||||
'steps': steps.map((step) => step.toJson()).toList(),
|
||||
},
|
||||
options: Options(responseType: ResponseType.json),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user