QueueMgr/lib/backend/DataManager.dart
2023-08-09 20:36:11 +05:30

76 lines
1.9 KiB
Dart

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';
// 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);
}
}
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);
}
}
Map<String,dynamic> GetServiceById(String id){
for (var service in services) {
Map<String,dynamic> s = jsonDecode(service.toString());
if(s['id']==id){
return s;
}
}
return {};
}
}
extension DurationExtensions on Duration{
String ToHoursAndMinutes(){
return "${this.inHours}:${this.inMinutes}";
}
}