forked from zhoho/APP_ConveUntact_Yookhaehan
generated from osamhack2021/flutter-devcontainer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
reservation_controller.dart
63 lines (54 loc) · 2.17 KB
/
reservation_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
58
59
60
61
62
63
import 'package:get/get.dart';
import 'package:myapp/domain/reservation/reservation.dart';
import 'package:myapp/domain/reservation/reservation_repository.dart';
class ReservationController extends GetxController {
final ReservationRepository _ReservationRepository = ReservationRepository();
final reservations = <Reservation>[].obs;
final reservation = Reservation().obs;
final unitcode;
// 객체 생성(create) onInit 함수실행(initialize)
// ReservationController n = Get.put(ReservationController());
ReservationController({this.unitcode});
@override
void onInit() {
super.onInit();
findByUnitCode(unitcode);
}
Future<List<Reservation>> findByUnitCode(String unitcode) async {
List<Reservation> reservations = await _ReservationRepository.findByUnitCode(unitcode);
this.reservations.value = reservations;
return reservations;
}
Future<List<Reservation>> findByUid(String uid) async {
List<Reservation> reservations = await _ReservationRepository.findByUid(uid);
this.reservations.value = reservations;
return reservations;
}
Future<void> add(Reservation newReservation) async {
Reservation reservation = await _ReservationRepository.add(newReservation);
if (reservation.id != null) {
this.reservations.add(reservation);
}
}
Future<void> findById(String id) async {
Reservation reservation = await _ReservationRepository.findById(id);
this.reservation.value = reservation;
}
Future<void> updateById(Reservation newReservation, String id) async {
int result = await _ReservationRepository.updateById(newReservation, id);
if (result == 1) {
Reservation reservation = await _ReservationRepository.findById(id);
this.reservation.value = reservation;
this.reservations.value = this.reservations.map((e) => e.id == id ? reservation : e).toList();
}
}
Future<int> deleteById(String id) async {
int result = await _ReservationRepository.deleteById(id);
if (result == 1) {
print("서버 쪽 삭제 성공");
List<Reservation> result = reservations.where((reservation) => reservation.id != id).toList();
reservations.value = result;
}
return result;
}
}