Untitled

mail@pastecode.io avatar
unknown
plain_text
6 months ago
1.6 kB
1
Indexable
Never
import 'package:dio/dio.dart';
import '../helpers/api_client.dart';
import '../model/poli.dart';

class PoliService {
  Future<List<Poli>> listData() async {
    final Response response = await ApiClient().get('poli/?key=123');
    // final List data = response.data as List;
    final List data = response.data['data'];
    print(data);
    List<Poli> result = data.map((json) => Poli.fromJson(json)).toList();
    return result;
  }

  // simpan(Poli poli) async {
  Future<Poli> simpan(Poli poli) async {
    var data = poli.toJson();
    final Response response = await ApiClient().post('poli/?key=123', data);
    print(response.data);
    Poli result = Poli.fromJson(response.data['data']);
    // return result; // success
    return response.data['status']; // success
  }

  Future<Poli> ubah(Poli poli, String? id) async {
    var data = poli.toJson();
    final Response response = await ApiClient().put('poli/${id}?key=123', data);
    print(response.data);
    Poli result = Poli.fromJson(response.data['data']);
    return result;
  }

  Future<Poli> getById(String? id) async {
    final Response response = await ApiClient().get('poli/${id}?key=123');
    print(response.data);
    Poli result = Poli.fromJson(response.data['data']);
    return result;
  }

  // Future<String> hapus(String? id) async {
  Future<Poli> hapus(Poli poli) async {
    print(poli.id);
    final Response response = await ApiClient().delete('poli/${poli.id}?key=123');
    print(response.data);
    Poli result = Poli.fromJson(response.data['data']);
    return result;
  }
}
Leave a Comment