Initial commit: Penyerahan final Source code Tugas Akhir
This commit is contained in:
70
lib/services/search_service.dart
Normal file
70
lib/services/search_service.dart
Normal file
@ -0,0 +1,70 @@
|
||||
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');
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user