Initial commit: Penyerahan final Source code Tugas Akhir
This commit is contained in:
63
lib/services/cart_service.dart
Normal file
63
lib/services/cart_service.dart
Normal file
@ -0,0 +1,63 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:initial_folder/base_service.dart';
|
||||
import 'package:initial_folder/helper/user_info.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:initial_folder/models/carts_model.dart';
|
||||
|
||||
class CartService {
|
||||
Future<bool> addCart(idCourse) async {
|
||||
var token = await UsersInfo().getToken();
|
||||
var idUser = await UsersInfo().getIdUser();
|
||||
|
||||
Uri url = Uri.parse('$baseUrl/users/add-to-cart');
|
||||
|
||||
var body = jsonEncode({
|
||||
// 'id_user': idUser,
|
||||
'cart_item': idCourse,
|
||||
});
|
||||
http.Response response =
|
||||
await http.post(url, headers: headerWithToken(token!), body: body);
|
||||
if (response.statusCode == 201) {
|
||||
return true;
|
||||
} else {
|
||||
throw Exception('Gagal Menambahkan ke keranjang');
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> deleteCart(idCart) async {
|
||||
var token = await UsersInfo().getToken();
|
||||
|
||||
Uri url = Uri.parse('$baseUrl/users/cart/delete/$idCart');
|
||||
|
||||
http.Response response = await http.delete(
|
||||
url,
|
||||
headers: headerWithToken(token!),
|
||||
);
|
||||
if (response.statusCode == 200) {
|
||||
return true;
|
||||
} else {
|
||||
throw Exception('Gagal Menghapus keranjang');
|
||||
}
|
||||
}
|
||||
|
||||
Future<CartsModel> getCarts() async {
|
||||
var token = await UsersInfo().getToken();
|
||||
// var idUser = await UsersInfo().getIdUser();
|
||||
|
||||
// Uri url = Uri.parse('$baseUrl/users/carts/$idUser');
|
||||
Uri url = Uri.parse('$baseUrl/users/carts');
|
||||
http.Response response = await http.get(
|
||||
url,
|
||||
headers: headerWithToken(token!),
|
||||
);
|
||||
log(response.body);
|
||||
if (response.statusCode == 200) {
|
||||
print("Ini isi cart berhasil ${response.body}");
|
||||
return CartsModel.fromJson(jsonDecode(response.body));
|
||||
} else {
|
||||
throw Exception('Gagal Mengambil data keranjang');
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user