first commit
This commit is contained in:
153
lib/repository/base_repository.dart
Normal file
153
lib/repository/base_repository.dart
Normal file
@@ -0,0 +1,153 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
abstract class BaseRepository {
|
||||
final Dio dio;
|
||||
BaseRepository({required this.dio});
|
||||
|
||||
Future<Map<String, dynamic>> post({
|
||||
required Map<String, dynamic> param,
|
||||
required String service,
|
||||
String? token,
|
||||
}) async {
|
||||
try {
|
||||
final response = await dio.post(
|
||||
// Constant.baseUrl + service,
|
||||
service,
|
||||
data: jsonEncode(param),
|
||||
options: Options(
|
||||
headers: token != null
|
||||
? {
|
||||
HttpHeaders.contentTypeHeader: "application/json",
|
||||
HttpHeaders.authorizationHeader: "Bearer $token",
|
||||
}
|
||||
: {
|
||||
HttpHeaders.contentTypeHeader: "application/json",
|
||||
},
|
||||
contentType: "application/json",
|
||||
),
|
||||
);
|
||||
if (response.statusCode != 200) {
|
||||
throw BaseRepositoryException(
|
||||
message: "Invalid Http Response ${response.statusCode}",
|
||||
);
|
||||
}
|
||||
Map<String, dynamic> jsonData = jsonDecode(response.data);
|
||||
if (jsonData["status"] != "OK") {
|
||||
throw BaseRepositoryException(
|
||||
message: jsonData["message"],
|
||||
);
|
||||
} else {
|
||||
return jsonData;
|
||||
}
|
||||
} on DioError catch (e) {
|
||||
throw BaseRepositoryException(message: e.message ?? '');
|
||||
} on SocketException catch (e) {
|
||||
throw BaseRepositoryException(message: e.message);
|
||||
} on BaseRepositoryException catch (e) {
|
||||
throw BaseRepositoryException(message: e.message);
|
||||
}
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> get({
|
||||
required String service,
|
||||
}) async {
|
||||
try {
|
||||
final response = await dio.get(
|
||||
// Constant.baseUrl + service,
|
||||
service,
|
||||
);
|
||||
if (response.statusCode != 200) {
|
||||
throw BaseRepositoryException(
|
||||
message: "Invalid Http Response ${response.statusCode}",
|
||||
);
|
||||
}
|
||||
// print(jsonDecode(response.data));
|
||||
Map<String, dynamic> jsonData = response.data;
|
||||
if (jsonData["status"] != "OK") {
|
||||
throw BaseRepositoryException(
|
||||
message: jsonData["message"],
|
||||
);
|
||||
} else {
|
||||
return jsonData;
|
||||
}
|
||||
} on DioError catch (e) {
|
||||
throw BaseRepositoryException(message: e.message ?? '');
|
||||
} on SocketException catch (e) {
|
||||
throw BaseRepositoryException(message: e.message);
|
||||
} on BaseRepositoryException catch (e) {
|
||||
throw BaseRepositoryException(message: e.message);
|
||||
}
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> getBooth({
|
||||
required String service,
|
||||
}) async {
|
||||
try {
|
||||
final response = await dio.get(
|
||||
// Constant.baseUrl + service,
|
||||
service,
|
||||
);
|
||||
if (response.statusCode != 200) {
|
||||
throw BaseRepositoryException(
|
||||
message: "Invalid Http Response ${response.statusCode}",
|
||||
);
|
||||
}
|
||||
// print(jsonDecode(response.data));
|
||||
Map<String, dynamic> jsonData = jsonDecode(response.data);
|
||||
if (jsonData["status"] != "OK") {
|
||||
throw BaseRepositoryException(
|
||||
message: jsonData["message"],
|
||||
);
|
||||
} else {
|
||||
return jsonData;
|
||||
}
|
||||
} on DioError catch (e) {
|
||||
throw BaseRepositoryException(message: e.message ?? '');
|
||||
} on SocketException catch (e) {
|
||||
throw BaseRepositoryException(message: e.message);
|
||||
} on BaseRepositoryException catch (e) {
|
||||
throw BaseRepositoryException(message: e.message);
|
||||
}
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> getService({
|
||||
required String service,
|
||||
}) async {
|
||||
try {
|
||||
final response = await dio.get(
|
||||
// Constant.baseUrl + service,
|
||||
service,
|
||||
);
|
||||
if (response.statusCode != 200) {
|
||||
throw BaseRepositoryException(
|
||||
message: "Invalid Http Response ${response.statusCode}",
|
||||
);
|
||||
}
|
||||
// print(jsonDecode(response.data));
|
||||
Map<String, dynamic> jsonData = jsonDecode(response.data);
|
||||
if (jsonData["status"] != "OK") {
|
||||
throw BaseRepositoryException(
|
||||
message: jsonData["message"],
|
||||
);
|
||||
} else {
|
||||
return jsonData;
|
||||
}
|
||||
} on DioError catch (e) {
|
||||
throw BaseRepositoryException(message: e.message ?? "");
|
||||
} on SocketException catch (e) {
|
||||
throw BaseRepositoryException(message: e.message);
|
||||
} on BaseRepositoryException catch (e) {
|
||||
throw BaseRepositoryException(message: e.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class BaseRepositoryException implements Exception {
|
||||
final String message;
|
||||
BaseRepositoryException({
|
||||
required this.message,
|
||||
});
|
||||
}
|
||||
36
lib/repository/booth_repository.dart
Normal file
36
lib/repository/booth_repository.dart
Normal file
@@ -0,0 +1,36 @@
|
||||
import 'package:ticket_booth/app/constant.dart';
|
||||
import 'package:ticket_booth/model/booth.dart';
|
||||
import 'package:ticket_booth/model/branch_model.dart';
|
||||
import 'package:ticket_booth/repository/base_repository.dart';
|
||||
|
||||
class BoothRepository extends BaseRepository {
|
||||
BoothRepository({required super.dio});
|
||||
Future<List<Booth>> getData({required String hostIP}) async {
|
||||
final url =
|
||||
"$hostIP/one-api/mockup/masterdata/one-md-ticket-dispenser/ticketbooth/index";
|
||||
print(url);
|
||||
// final url = "$hostIP/one-api/training/ticketbooth/index";
|
||||
final resp = await getBooth(service: url);
|
||||
final List<Booth> listBooth = List.empty(growable: true);
|
||||
resp['data']['records'].forEach((e) {
|
||||
final model = Booth.fromJson(e);
|
||||
listBooth.add(model);
|
||||
});
|
||||
return listBooth;
|
||||
}
|
||||
|
||||
Future<List<BranchModel>> getBranch({required String hostIP}) async {
|
||||
// https://devcpone.aplikasi.web.id/one-api/mockup/fo/antrian/AntrianByStationAndLocation/getbranch
|
||||
final url =
|
||||
"$hostIP/one-api/mockup/fo/antrian/AntrianByStationAndLocation/getbranch";
|
||||
print(url);
|
||||
// final url = "$hostIP/one-api/training/ticketbooth/index";
|
||||
final resp = await getBooth(service: url);
|
||||
final List<BranchModel> listBooth = List.empty(growable: true);
|
||||
resp['data']['records'].forEach((e) {
|
||||
final model = BranchModel.fromJson(e);
|
||||
listBooth.add(model);
|
||||
});
|
||||
return listBooth;
|
||||
}
|
||||
}
|
||||
24
lib/repository/queue_repository.dart
Normal file
24
lib/repository/queue_repository.dart
Normal file
@@ -0,0 +1,24 @@
|
||||
import '../app/constant.dart';
|
||||
import 'base_repository.dart';
|
||||
|
||||
class QueueRepository extends BaseRepository {
|
||||
QueueRepository({required super.dio});
|
||||
Future<Map<String, dynamic>> getData(
|
||||
{required service_id,
|
||||
required booth_id,
|
||||
required hostIp,
|
||||
required branchID}) async {
|
||||
// var url = "$hostIp/ticket/$service_id/$booth_id";
|
||||
var url = "$hostIp/one-api/antrian/ticket/getAntrian";
|
||||
// final resp = await get(
|
||||
// service: url,
|
||||
// );
|
||||
final resp = await post(param: {
|
||||
"service_id": service_id,
|
||||
"booth_id": booth_id,
|
||||
"branch_id": branchID
|
||||
}, service: url);
|
||||
// print(resp);
|
||||
return resp;
|
||||
}
|
||||
}
|
||||
24
lib/repository/service_repository.dart
Normal file
24
lib/repository/service_repository.dart
Normal file
@@ -0,0 +1,24 @@
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:ticket_booth/model/layanan.dart';
|
||||
|
||||
import '../app/constant.dart';
|
||||
import 'base_repository.dart';
|
||||
|
||||
class ServiceRepository extends BaseRepository {
|
||||
ServiceRepository({required super.dio});
|
||||
Future<List<Layanan>> getData({required String hostIp}) async {
|
||||
final url =
|
||||
"$hostIp/one-api/mockup/masterdata/one-md-fo-service/service/listService";
|
||||
print(url);
|
||||
final resp = await getService(service: url);
|
||||
|
||||
final List<Layanan> listLayanan = List.empty(growable: true);
|
||||
|
||||
resp['data']['records'].forEach((e) {
|
||||
final model = Layanan.fromJson(e);
|
||||
listLayanan.add(model);
|
||||
});
|
||||
return listLayanan;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user