Fakultas Ilmu Komputer UI

kegiatan_repository.dart 1.93 KB
Newer Older
1
import 'package:bisaGo/flavor/flavor.dart';
2
3
4
5
6
import 'package:bisaGo/model/kegiatan.dart';
import 'package:bisaGo/network/network_interface.dart';

abstract class BaseKegiatanRepository {
  Future<KegiatanList> fetchKegiatan(String placeId);
7
  Future<List<String>> fetchImages(String placeId, int id);
8
9
  Future<dynamic> createKegiatan(
      Map<String, dynamic> newKegiatanData, String placeId);
10
11
12
13
14
15
16
17
18
19
}

class KegiatanRepository implements BaseKegiatanRepository {
  final NetworkInterface _network = NetworkInterface();

  @override
  Future<KegiatanList> fetchKegiatan(String placeId) async {
    final url = '/informasi-fasilitas/lokasi/list-kegiatan/$placeId';
    final response = await _network.get(url: url, isLogin: false);
    final data = response.values.toList().reversed.toList();
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
    var allKegiatan = [];

    for (var _index = 0; _index < data.length; _index++) {
      var kegiatan = KegiatanModel.fromJson(data[_index]);
      kegiatan.image = await fetchImages(kegiatan.placeId, kegiatan.id);
      allKegiatan.add(kegiatan);
    }
    allKegiatan = allKegiatan.cast<KegiatanModel>();
    return KegiatanList(allKegiatan);
  }

  @override
  Future<List<String>> fetchImages(String placeId, int id) async {
    final url = '/informasi-fasilitas/lokasi/list-foto-kegiatan/$placeId/$id';
    final response = await _network.get(url: url, isLogin: false);
    var allImages = [];

37
38
39
    for (var _index = 0; _index < response.length; _index++) {
      var fotoUrl =
          ApiFlavor.getBaseUrl() + response.values.toList()[_index]['foto'];
40
41
42
43
      allImages.add(fotoUrl);
    }
    allImages = allImages.cast<String>();
    return allImages;
44
  }
45
46
47
48
49
50
51
52
53
54
55
56
57
58

  @override
  Future<dynamic> createKegiatan(
    Map<String, dynamic> newKegiatanData,
    String placeId,
  ) async {
    print(newKegiatanData);
    final response = await _network.post(
      url: '/informasi-fasilitas/lokasi/add-kegiatan/$placeId/',
      bodyParams: newKegiatanData,
    );
    return response;
  }
}