72 lines
1.9 KiB
Dart
72 lines
1.9 KiB
Dart
import 'dart:convert';
|
|
import 'package:http/http.dart' as http;
|
|
|
|
class ApiService {
|
|
final String baseUrl = "http://127.0.0.1:8000/api"; // Ganti dengan URL API kamu
|
|
|
|
Future<Map<String, dynamic>> register(String name, String email, String password) async {
|
|
final response = await http.post(
|
|
Uri.parse("$baseUrl/register"),
|
|
headers: {"Content-Type": "application/json"},
|
|
body: jsonEncode({
|
|
"name": name,
|
|
"email": email,
|
|
"password": password,
|
|
}),
|
|
);
|
|
|
|
if (response.statusCode == 201) {
|
|
return jsonDecode(response.body);
|
|
} else {
|
|
throw Exception("Gagal melakukan registrasi");
|
|
}
|
|
}
|
|
|
|
Future<Map<String, dynamic>> login(String email, String password) async {
|
|
final response = await http.post(
|
|
Uri.parse("$baseUrl/login"),
|
|
headers: {"Content-Type": "application/json"},
|
|
body: jsonEncode({
|
|
"email": email,
|
|
"password": password,
|
|
}),
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
return jsonDecode(response.body);
|
|
} else {
|
|
throw Exception("Email atau password salah");
|
|
}
|
|
}
|
|
|
|
// Fetch categories
|
|
Future<List<dynamic>> getCategories() async {
|
|
final response = await http.get(
|
|
Uri.parse("$baseUrl/categories"),
|
|
headers: {"Content-Type": "application/json"},
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
final data = jsonDecode(response.body);
|
|
return data['categories']; // Mengembalikan daftar kategori
|
|
} else {
|
|
throw Exception("Gagal mengambil data kategori");
|
|
}
|
|
}
|
|
|
|
// Fetch services
|
|
Future<List<dynamic>> getServices() async {
|
|
final response = await http.get(
|
|
Uri.parse("$baseUrl/services"),
|
|
headers: {"Content-Type": "application/json"},
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
final data = jsonDecode(response.body);
|
|
return data['services']; // Mengembalikan daftar layanan
|
|
} else {
|
|
throw Exception("Gagal mengambil data layanan");
|
|
}
|
|
}
|
|
}
|