From 2acdd307e12ed94673b86d65157b326978d96882 Mon Sep 17 00:00:00 2001 From: yaso Date: Tue, 15 Oct 2024 10:32:07 +0200 Subject: [PATCH] Add API calls file for commonly used api calles. Get profile added --- .../lib/mih_apis/mih_api_calls.dart | 115 ++++++++++++++++++ .../mih_home/mih_profile_getter.dart | 101 +-------------- 2 files changed, 118 insertions(+), 98 deletions(-) create mode 100644 Frontend/patient_manager/lib/mih_apis/mih_api_calls.dart diff --git a/Frontend/patient_manager/lib/mih_apis/mih_api_calls.dart b/Frontend/patient_manager/lib/mih_apis/mih_api_calls.dart new file mode 100644 index 00000000..7fea51e4 --- /dev/null +++ b/Frontend/patient_manager/lib/mih_apis/mih_api_calls.dart @@ -0,0 +1,115 @@ +import 'dart:convert'; + +import 'package:patient_manager/mih_env/env.dart'; +import 'package:patient_manager/mih_objects/app_user.dart'; +import 'package:patient_manager/mih_objects/arguments.dart'; +import 'package:patient_manager/mih_objects/business.dart'; +import 'package:patient_manager/mih_objects/business_user.dart'; +import 'package:patient_manager/mih_objects/notification.dart'; +import 'package:supertokens_flutter/supertokens.dart'; +import 'package:supertokens_flutter/http.dart' as http; + +class MIHApiCalls { + final baseAPI = AppEnviroment.baseApiUrl; + + /// This function is used to get profile details of signed in user. + /// + /// Patameters: int notificationAmount which is used to get number of notifications to show. + /// + /// Returns HomeArguments which contains:- + /// - Signed In user data. + /// - Business user belongs to. + /// - Business User details. + /// - notifications. + /// - user profile picture. + Future getProfile(int notificationAmount) async { + AppUser userData; + Business? busData; + BusinessUser? bUserData; + List notifi; + String userPic; + + // Get Userdata + var uid = await SuperTokens.getUserId(); + var responseUser = await http.get(Uri.parse("$baseAPI/user/$uid")); + if (responseUser.statusCode == 200) { + // print("here"); + String body = responseUser.body; + var decodedData = jsonDecode(body); + AppUser u = AppUser.fromJson(decodedData); + userData = u; + } else { + throw Exception( + "Error: GetUserData status code ${responseUser.statusCode}"); + } + + // Get BusinessUserdata + var responseBUser = await http.get( + Uri.parse("$baseAPI/business-user/$uid"), + ); + if (responseBUser.statusCode == 200) { + String body = responseBUser.body; + var decodedData = jsonDecode(body); + BusinessUser business_User = BusinessUser.fromJson(decodedData); + bUserData = business_User; + } else { + bUserData = null; + } + + // Get Businessdata + var responseBusiness = + await http.get(Uri.parse("$baseAPI/business/app_id/$uid")); + if (responseBusiness.statusCode == 200) { + String body = responseBusiness.body; + var decodedData = jsonDecode(body); + Business business = Business.fromJson(decodedData); + busData = business; + } else { + busData = null; + } + + //get profile picture + if (userData.pro_pic_path == "") { + userPic = ""; + } + // else if (AppEnviroment.getEnv() == "Dev") { + // userPic = "${AppEnviroment.baseFileUrl}/mih/${userData.pro_pic_path}"; + // } + else { + var url = + "${AppEnviroment.baseApiUrl}/minio/pull/file/${AppEnviroment.getEnv()}/${userData.pro_pic_path}"; + var response = await http.get(Uri.parse(url)); + + if (response.statusCode == 200) { + String body = response.body; + var decodedData = jsonDecode(body); + + userPic = decodedData['minioURL']; + } else { + userPic = ""; + // throw Exception( + // "Error: GetUserData status code ${response.statusCode}"); + } + } + + //Get Notifications + var responseNotification = await http.get( + Uri.parse("$baseAPI/notifications/$uid?amount=$notificationAmount")); + if (responseNotification.statusCode == 200) { + String body = responseNotification.body; + // var decodedData = jsonDecode(body); + // MIHNotification notifications = MIHNotification.fromJson(decodedData); + + Iterable l = jsonDecode(body); + //print("Here2"); + List notifications = List.from( + l.map((model) => MIHNotification.fromJson(model))); + notifi = notifications; + } else { + notifi = []; + } + + //print(userPic); + return HomeArguments(userData, bUserData, busData, notifi, userPic); + } +} diff --git a/Frontend/patient_manager/lib/mih_packages/mih_home/mih_profile_getter.dart b/Frontend/patient_manager/lib/mih_packages/mih_home/mih_profile_getter.dart index 23234ef7..3a347f7d 100644 --- a/Frontend/patient_manager/lib/mih_packages/mih_home/mih_profile_getter.dart +++ b/Frontend/patient_manager/lib/mih_packages/mih_home/mih_profile_getter.dart @@ -1,6 +1,5 @@ -import 'dart:convert'; - import 'package:flutter/material.dart'; +import 'package:patient_manager/mih_apis/mih_api_calls.dart'; import 'package:patient_manager/mih_components/mih_layout/mih_action.dart'; import 'package:patient_manager/mih_components/mih_layout/mih_body.dart'; import 'package:patient_manager/mih_components/mih_layout/mih_header.dart'; @@ -9,12 +8,8 @@ import 'package:patient_manager/mih_components/mih_pop_up_messages/mih_loading_c import 'package:patient_manager/mih_env/env.dart'; import 'package:patient_manager/mih_objects/app_user.dart'; import 'package:patient_manager/mih_objects/arguments.dart'; -import 'package:patient_manager/mih_objects/business.dart'; import 'package:patient_manager/mih_objects/business_user.dart'; -import 'package:patient_manager/mih_objects/notification.dart'; import 'package:patient_manager/mih_packages/mih_home/mih_home.dart'; -import 'package:supertokens_flutter/supertokens.dart'; -import 'package:supertokens_flutter/http.dart' as http; class MIHProfileGetter extends StatefulWidget { const MIHProfileGetter({ @@ -34,97 +29,6 @@ class _MIHProfileGetterState extends State { String proPicUrl = "empty"; ImageProvider? propicFile; - Future getProfile() async { - AppUser userData; - Business? busData; - BusinessUser? bUserData; - List notifi; - String userPic; - - // Get Userdata - var uid = await SuperTokens.getUserId(); - var responseUser = await http.get(Uri.parse("$baseAPI/user/$uid")); - if (responseUser.statusCode == 200) { - // print("here"); - String body = responseUser.body; - var decodedData = jsonDecode(body); - AppUser u = AppUser.fromJson(decodedData); - userData = u; - } else { - throw Exception( - "Error: GetUserData status code ${responseUser.statusCode}"); - } - - // Get BusinessUserdata - var responseBUser = await http.get( - Uri.parse("$baseAPI/business-user/$uid"), - ); - if (responseBUser.statusCode == 200) { - String body = responseBUser.body; - var decodedData = jsonDecode(body); - BusinessUser business_User = BusinessUser.fromJson(decodedData); - bUserData = business_User; - } else { - bUserData = null; - } - - // Get Businessdata - var responseBusiness = - await http.get(Uri.parse("$baseAPI/business/app_id/$uid")); - if (responseBusiness.statusCode == 200) { - String body = responseBusiness.body; - var decodedData = jsonDecode(body); - Business business = Business.fromJson(decodedData); - busData = business; - } else { - busData = null; - } - - //get profile picture - if (userData.pro_pic_path == "") { - userPic = ""; - } - // else if (AppEnviroment.getEnv() == "Dev") { - // userPic = "${AppEnviroment.baseFileUrl}/mih/${userData.pro_pic_path}"; - // } - else { - var url = - "${AppEnviroment.baseApiUrl}/minio/pull/file/${AppEnviroment.getEnv()}/${userData.pro_pic_path}"; - var response = await http.get(Uri.parse(url)); - - if (response.statusCode == 200) { - String body = response.body; - var decodedData = jsonDecode(body); - - userPic = decodedData['minioURL']; - } else { - userPic = ""; - // throw Exception( - // "Error: GetUserData status code ${response.statusCode}"); - } - } - - //Get Notifications - var responseNotification = - await http.get(Uri.parse("$baseAPI/notifications/$uid?amount=$amount")); - if (responseNotification.statusCode == 200) { - String body = responseNotification.body; - // var decodedData = jsonDecode(body); - // MIHNotification notifications = MIHNotification.fromJson(decodedData); - - Iterable l = jsonDecode(body); - //print("Here2"); - List notifications = List.from( - l.map((model) => MIHNotification.fromJson(model))); - notifi = notifications; - } else { - notifi = []; - } - - //print(userPic); - return HomeArguments(userData, bUserData, busData, notifi, userPic); - } - ImageProvider? isPictureAvailable(String url) { if (url == "") { return const AssetImage('images/i-dont-know-2.png'); @@ -176,7 +80,8 @@ class _MIHProfileGetterState extends State { @override void initState() { - profile = getProfile(); + //profile = getProfile(); + profile = MIHApiCalls().getProfile(amount); super.initState(); }