QueueMgr/lib/backend/DataManager.dart
2023-08-21 08:24:26 +05:30

179 lines
4.7 KiB
Dart

import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:intl/intl.dart';
import 'package:queue_mgr/backend/DebugHelper.dart';
import 'package:queue_mgr/backend/Dialogs.dart';
// DateFormat dateFormat;
final String API_ENDPOINT= "https://vps.playpoolstudios.com/qms/api/";
DateFormat dateFormat = DateFormat("yyyy/MM/dd");
DateFormat timeFormat = DateFormat("hh:mm a");
DateFormat dateTimeFormat = DateFormat("yyyy/MM/dd hh:mm a");
class DataManager{
static _DataManager? m_instance = null;
static _DataManager instance(){
m_instance ??= _DataManager();
return m_instance!;
}
}
class _DataManager{
_DataManager();
List<dynamic> services = [];
Future<void> GetData() async{
try{
var response = (await http.post(
Uri.parse('${API_ENDPOINT}get_services.php'),
body: <String, String>{}));
Debug.LogResponse(response.body.toString());
services = jsonDecode(response.body.toString());
Debug.LogResponse(services);
}catch(e){
Debug.LogError(e);
}
await ProcessServices();
}
Future<void> ProcessServices() async{
List<dynamic> _services= [];
for (var s in services) {
Map<String,dynamic> se = jsonDecode(s);
if(DateTime.now().isAfter(DateTime.parse(se['end_time']))){
continue;
}
try{
var response = (await http.post(
Uri.parse('${API_ENDPOINT}get_appointments.php'),
body: <String, String>{
'serviceId':se['id']
}));
se.putIfAbsent("appointments", () => jsonDecode(response.body.toString()));
}catch(e){
Debug.LogError(e);
}
_services.add(se);
}
services = _services;
}
Future<void> AddService(String name, DateTime sTime, DateTime eTime,Duration duration) async {
try{
var response = (await http.post(
Uri.parse('${API_ENDPOINT}add_service.php'),
body: <String, String>{
'name':name,
'stime':sTime.toString(),
'etime':eTime.toString(),
'duration':"${duration.ToHoursAndMinutes()}"
}));
Debug.LogResponse(response.body.toString());
services = jsonDecode(response.body.toString());
Debug.LogResponse(services);
}catch(e){
Debug.LogError(e);
}
await ProcessServices();
}
Future<void> DeleteService(String id) async {
try{
var response = (await http.post(
Uri.parse('${API_ENDPOINT}remove_service.php'),
body: <String, String>{
'id':id,
}));
Debug.LogResponse(response.body.toString());
services = jsonDecode(response.body.toString());
Debug.LogResponse(services);
}catch(e){
Debug.LogError(e);
}
await ProcessServices();
}
Future<void> EditService(String id,String name, DateTime sTime, DateTime eTime,Duration duration) async {
try{
var response = (await http.post(
Uri.parse('${API_ENDPOINT}edit_service.php'),
body: <String, String>{
'id':id,
'name':name,
'stime':sTime.toString(),
'etime':eTime.toString(),
'duration':"${duration.ToHoursAndMinutes()}"
}));
Debug.LogResponse(response.body.toString());
services = jsonDecode(response.body.toString());
Debug.LogResponse(services);
}catch(e){
Debug.LogError(e);
}
await ProcessServices();
}
Future<String> CompleteService(dynamic serviceId, dynamic tokenId) async {
String res = "";
Debug.Log("Completing $tokenId from Service $serviceId");
try{
var response = (await http.post(
Uri.parse('${API_ENDPOINT}complete_token.php'),
body: <String, String>{
'service_id':serviceId.toString(),
'user_id':tokenId.toString(),
}));
res = response.body.toString();
Debug.LogResponse(response.body.toString());
services = jsonDecode(response.body.toString());
await ProcessServices();
Debug.LogResponse(services);
return "0";
}catch(e){
Debug.LogError(e);
return res;
}
}
Map<String,dynamic> GetServiceById(String id){
for (var service in services) {
Map<String,dynamic> s = (service);
if(s['id']==id){
return s;
}
}
return {};
}
}
class Helpers{
static Duration ParseDuration(String input){
if(input.contains(":")){
return Duration(hours: int.parse(input.split(":")[0]), minutes: int.parse(input.split(":")[1]));
}
return Duration(minutes: 0);
}
}
extension DurationExtensions on Duration{
String ToHoursAndMinutes(){
int mins = this.inMinutes %60;
return "${this.inHours}:${mins}";
}
}