TKK_E32221348/Mobile/lib/Service/api_service.dart

157 lines
4.6 KiB
Dart

import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:ui_loker/Model/locker_model.dart';
import 'package:ui_loker/Model/penitipan_model.dart';
import 'package:intl/intl.dart';
class ApiService {
static const String baseUrl = 'https://smartlocker.punyapadias.my.id/api';
/// Get All Lockers
static Future<List<LockerModel>> getAllLockers() async {
final response = await http.get(Uri.parse('$baseUrl/lokers'));
if (response.statusCode == 200) {
final data = jsonDecode(response.body);
return (data['data'] as List)
.map((e) => LockerModel.fromJson(e))
.toList();
} else {
throw Exception('Gagal mengambil data loker');
}
}
/// Add New Locker
static Future<Map<String, dynamic>> addLocker(int number) async {
final res = await http.post(
Uri.parse('$baseUrl/lokers/store'),
headers: {'Content-Type': 'application/json'},
body: jsonEncode({
'nomor_loker': 'Loker $number',
'status': 'servis',
}),
);
if (res.statusCode == 200) {
return jsonDecode(res.body);
} else {
throw Exception('Gagal menambahkan loker');
}
}
/// Kirim Data Penitipan
static Future<PenitipanModel> kirimPenitipan({
required String user,
required int lokerId,
}) async {
final res = await http.post(
Uri.parse('$baseUrl/penitipan/store'),
headers: {'Content-Type': 'application/json'},
body: jsonEncode({
'nama': user,
'loker_id': lokerId,
}),
);
if (res.statusCode == 201) {
final jsonRes = jsonDecode(res.body);
return PenitipanModel.fromJson(jsonRes['data']);
} else {
throw Exception('Gagal menyimpan penitipan: ${res.body}');
}
}
/// Hapus Locker Berdasarkan ID
static Future<bool> deleteLocker(int id) async {
final res = await http.delete(
Uri.parse('$baseUrl/lokers/$id'),
headers: {'Content-Type': 'application/json'},
);
if (res.statusCode == 200) {
return true;
} else {
throw Exception('Gagal menghapus loker');
}
}
static Future<List<PenitipanModel>> getPenitipanByLoker(int lokerId) async {
final response = await http.get(
Uri.parse('$baseUrl/lokers/$lokerId/penitipan'),
);
if (response.statusCode == 200) {
final data = jsonDecode(response.body);
return (data['data'] as List)
.map((e) => PenitipanModel.fromJson(e))
.toList();
} else {
throw Exception('Gagal mengambil riwayat penitipan');
}
}
static Future<List<PenitipanModel>> getPenggunaLoker(int lokerId) async {
final response = await http.get(Uri.parse('$baseUrl/penitipan/loker/$lokerId'));
if (response.statusCode == 200) {
final List<dynamic> jsonList = jsonDecode(response.body);
return jsonList.map((json) => PenitipanModel.fromJson(json)).toList();
} else {
throw Exception('Gagal memuat data pengguna');
}
}
static Future<List<PenitipanModel>> etPenitipanByLokerg(int lokerId) async {
final response = await http.get(Uri.parse('$baseUrl/penitipan/loker/$lokerId'));
if (response.statusCode == 200) {
List<dynamic> jsonList = jsonDecode(response.body);
return jsonList.map((json) => PenitipanModel.fromJson(json)).toList();
} else {
throw Exception('Gagal memuat data pengguna');
}
}
static Future<bool> updateLokerStatus(int lokerId, String newStatus) async {
final url = Uri.parse('$baseUrl/lokers/$lokerId/status');
final response = await http.put(
url,
headers: {'Content-Type': 'application/json'},
body: jsonEncode({'status': newStatus}),
);
if (response.statusCode == 200) {
return true;
} else {
print('Gagal update status. Status code: ${response.statusCode}');
print('Response: ${response.body}');
throw Exception('Gagal update status loker');
}
}
Future<int> getPenghasilanHariIni() async {
final response = await http.get(Uri.parse("$baseUrl/today"));
if (response.statusCode == 200) {
final data = json.decode(response.body);
return data['total_penghasilan'] ?? 0;
} else {
throw Exception("Gagal mengambil data hari ini");
}
}
Future<int> getPenghasilanByTanggal(String tanggal) async {
final response = await http.post(
Uri.parse("$baseUrl/count"),
headers: {"Content-Type": "application/json"},
body: json.encode({"tanggal": tanggal}),
);
if (response.statusCode == 200) {
final data = json.decode(response.body);
return data['total_penghasilan'] ?? 0;
} else {
throw Exception("Gagal mengambil data berdasarkan tanggal");
}
}
}