forked from zhoho/APP_ConveUntact_Yookhaehan
generated from osamhack2021/flutter-devcontainer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
declaration_controller.dart
57 lines (49 loc) · 1.96 KB
/
declaration_controller.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import 'package:get/get.dart';
import 'package:myapp/domain/declaration/declaration.dart';
import 'package:myapp/domain/declaration/declaration_repository.dart';
class DeclarationController extends GetxController {
final DeclarationRepository _DeclarationRepository = DeclarationRepository();
final declarations = <Declaration>[].obs;
final declaration = Declaration().obs;
final unitcode;
// 객체 생성(create) onInit 함수실행(initialize)
// DeclarationController n = Get.put(DeclarationController());
DeclarationController({this.unitcode});
@override
void onInit() {
super.onInit();
findByUnitCode(unitcode);
}
Future<List<Declaration>> findByUnitCode(String unitcode) async {
List<Declaration> declarations = await _DeclarationRepository.findByUnitCode(unitcode);
this.declarations.value = declarations;
return declarations;
}
Future<void> add(Declaration newDeclaration) async {
Declaration declaration = await _DeclarationRepository.add(newDeclaration);
if (declaration.id != null) {
this.declarations.add(declaration);
}
}
Future<void> findById(String id) async {
Declaration declaration = await _DeclarationRepository.findById(id);
this.declaration.value = declaration;
}
Future<void> updateById(Declaration newDeclaration, String id) async {
int result = await _DeclarationRepository.updateById(newDeclaration, id);
if (result == 1) {
Declaration declaration = await _DeclarationRepository.findById(id);
this.declaration.value = declaration;
this.declarations.value = this.declarations.map((e) => e.id == id ? declaration : e).toList();
}
}
Future<int> deleteById(String id) async {
int result = await _DeclarationRepository.deleteById(id);
if (result == 1) {
print("서버 쪽 삭제 성공");
List<Declaration> result = declarations.where((Declaration) => Declaration.id != id).toList();
declarations.value = result;
}
return result;
}
}