71 lines
2.1 KiB
Dart
71 lines
2.1 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:initial_folder/base_service.dart';
|
|
import 'package:initial_folder/models/course_model.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'dart:developer';
|
|
|
|
class SearchService {
|
|
Future<List<CourseModel>> search(String? judul) async {
|
|
Uri url = Uri.parse('$baseUrl/homepage/filter?keyword=$judul');
|
|
http.Response response = await http.get(url, headers: baseHeader);
|
|
log(response.body);
|
|
if (response.statusCode == 200) {
|
|
var data = jsonDecode(response.body)['data'][0];
|
|
List<CourseModel> course = [];
|
|
for (var item in data) {
|
|
course.add(CourseModel.fromJson(item));
|
|
}
|
|
|
|
return course;
|
|
} else {
|
|
throw Exception('Gagal');
|
|
}
|
|
}
|
|
|
|
Future<dynamic> filter({
|
|
String price = '',
|
|
String level = '',
|
|
String language = '',
|
|
String rating = '',
|
|
String keyword = '',
|
|
}) async {
|
|
Map<String, dynamic> queryParams = {
|
|
if (keyword != '') 'keyword': keyword,
|
|
if (price != '') 'price': price,
|
|
if (level != '') 'level': level,
|
|
if (language != '') 'language': language,
|
|
if (rating != '') 'rating': rating,
|
|
};
|
|
print("Isi Query Form ---------> $queryParams");
|
|
Uri url = Uri.parse('$baseUrl/homepage/course/search').replace(
|
|
queryParameters: queryParams,
|
|
);
|
|
if (queryParams.containsKey('keyword')) {
|
|
url = Uri.parse('$baseUrl/homepage/course/search').replace(
|
|
queryParameters: queryParams,
|
|
);
|
|
} else if (price != '') {
|
|
url = Uri.parse('$baseUrl/homepage/course/search').replace(
|
|
queryParameters: queryParams,
|
|
);
|
|
}
|
|
|
|
print("Url to API ----> $url");
|
|
var response =
|
|
await http.get(Uri.parse(Uri.decodeComponent(url.toString())));
|
|
if (response.statusCode == 200) {
|
|
print("berhasil fitlter search ${response.body}");
|
|
var data = jsonDecode(response.body)['data'][0];
|
|
List<CourseModel> courses = [];
|
|
for (var item in data) {
|
|
courses.add(CourseModel.fromJson(item));
|
|
}
|
|
return courses;
|
|
} else {
|
|
print("gagal fitlter search ${response.body}");
|
|
throw Exception('Gagal');
|
|
}
|
|
}
|
|
}
|