From ef233c414ba9807f9a62a8ac519a06ae1f8ffb54 Mon Sep 17 00:00:00 2001 From: hannathkadher Date: Wed, 2 Oct 2024 10:11:55 +0400 Subject: [PATCH] added api for space management --- lib/common/search_bar.dart | 7 +- lib/services/space_mana_api.dart | 230 +++++++++++++++++++++++++++++ lib/utils/app_routes.dart | 4 +- lib/utils/constants/api_const.dart | 30 +++- 4 files changed, 267 insertions(+), 4 deletions(-) create mode 100644 lib/services/space_mana_api.dart diff --git a/lib/common/search_bar.dart b/lib/common/search_bar.dart index 21ac8055..728fad33 100644 --- a/lib/common/search_bar.dart +++ b/lib/common/search_bar.dart @@ -47,7 +47,12 @@ class CustomSearchBar extends StatelessWidget { fillColor: ColorsManager.textFieldGreyColor, hintText: hintText, hintStyle: TextStyle( - color: Colors.grey[400], + color: Color(0xB2999999), + fontSize: 12, + fontFamily: 'Aftika', + fontWeight: FontWeight.w400, + height: 0, + letterSpacing: -0.24, ), suffixIcon: Padding( padding: const EdgeInsets.only(right: 16), diff --git a/lib/services/space_mana_api.dart b/lib/services/space_mana_api.dart new file mode 100644 index 00000000..0a96ed2b --- /dev/null +++ b/lib/services/space_mana_api.dart @@ -0,0 +1,230 @@ +import 'package:flutter/material.dart'; +import 'package:syncrow_web/pages/spaces_management/model/community_model.dart'; +import 'package:syncrow_web/pages/spaces_management/model/space_model.dart'; +import 'package:syncrow_web/pages/spaces_management/model/space_response_model.dart'; +import 'package:syncrow_web/services/api/http_service.dart'; +import 'package:syncrow_web/utils/constants/api_const.dart'; + +class CommunitySpaceManagementApi { + // Community Management APIs + Future> fetchCommunities() async { + try { + final response = await HTTPService().get( + path: ApiEndpoints.getCommunityList, + showServerMessage: true, + expectedResponseModel: (json) { + List jsonData = json; + List communityList = jsonData.map((jsonItem) { + return CommunityModel.fromJson(jsonItem); + }).toList(); + return communityList; + }, + ); + return response; + } catch (e) { + debugPrint('Error fetching communities: $e'); + return []; + } + } + + Future getCommunityById(String communityId) async { + try { + final response = await HTTPService().get( + path: ApiEndpoints.getCommunityById + .replaceAll('{communityId}', communityId), + showServerMessage: true, + expectedResponseModel: (json) { + return CommunityModel.fromJson(json['data']); + }, + ); + return response; + } catch (e) { + debugPrint('Error fetching community by ID: $e'); + return null; + } + } + + Future createCommunity( + String name, String description, String regionId) async { + try { + final response = await HTTPService().post( + path: ApiEndpoints.createCommunity, + body: { + 'name': name, + 'description': description, + 'regionId': regionId, + }, + showServerMessage: true, + expectedResponseModel: (json) { + return CommunityModel.fromJson(json['data']); + }, + ); + return response; + } catch (e) { + debugPrint('Error creating community: $e'); + return null; + } + } + + Future updateCommunity( + String communityId, CommunityModel community) async { + try { + final response = await HTTPService().put( + path: ApiEndpoints.updateCommunity + .replaceAll('{communityId}', communityId), + body: community.toMap(), + showServerMessage: true, + expectedResponseModel: (json) { + return json['success'] ?? false; + }, + ); + return response; + } catch (e) { + debugPrint('Error updating community: $e'); + return false; + } + } + + Future deleteCommunity(String communityId) async { + try { + final response = await HTTPService().delete( + path: ApiEndpoints.deleteCommunity + .replaceAll('{communityId}', communityId), + showServerMessage: true, + expectedResponseModel: (json) { + return json['success'] ?? false; + }, + ); + return response; + } catch (e) { + debugPrint('Error deleting community: $e'); + return false; + } + } + + Future fetchSpaces(String communityId) async { + try { + final response = await HTTPService().get( + path: ApiEndpoints.listSpaces.replaceAll('{communityId}', communityId), + showServerMessage: true, + expectedResponseModel: (json) { + return SpacesResponse.fromJson(json); + }, + ); + return response; + } catch (e) { + debugPrint('Error fetching spaces: $e'); + return SpacesResponse( + data: [], + message: 'Error fetching spaces', + page: 1, + size: 10, + totalItem: 0, + totalPage: 0, + hasNext: false, + hasPrevious: false, + ); + } + } + + Future getSpace(String communityId, String spaceId) async { + try { + final response = await HTTPService().get( + path: ApiEndpoints.getSpace + .replaceAll('{communityId}', communityId) + .replaceAll('{spaceId}', spaceId), + showServerMessage: true, + expectedResponseModel: (json) { + return SpaceModel.fromJson(json); + }, + ); + return response; + } catch (e) { + debugPrint('Error fetching space: $e'); + return SpaceModel(); // Assuming an empty SpaceModel constructor + } + } + + Future createSpace(String communityId, String name, + {String? parentId, bool isPrivate = false}) async { + try { + final body = { + 'name': name, + 'isPrivate': isPrivate, + }; + if (parentId != null) { + body['parentId'] = parentId; + } + + final response = await HTTPService().post( + path: ApiEndpoints.createSpace.replaceAll('{communityId}', communityId), + body: body, + showServerMessage: true, + expectedResponseModel: (json) { + return SpaceModel.fromJson(json['data']); + }, + ); + return response; + } catch (e) { + debugPrint('Error creating space: $e'); + return null; + } + } + + Future updateSpace( + String communityId, String spaceId, SpaceModel space) async { + try { + final response = await HTTPService().put( + path: ApiEndpoints.updateSpace + .replaceAll('{communityId}', communityId) + .replaceAll('{spaceId}', spaceId), + body: space.toMap(), + showServerMessage: true, + expectedResponseModel: (json) { + return json['success'] ?? false; + }, + ); + return response; + } catch (e) { + debugPrint('Error updating space: $e'); + return false; + } + } + + Future deleteSpace(String communityId, String spaceId) async { + try { + final response = await HTTPService().delete( + path: ApiEndpoints.deleteSpace + .replaceAll('{communityId}', communityId) + .replaceAll('{spaceId}', spaceId), + showServerMessage: true, + expectedResponseModel: (json) { + return json['success'] ?? false; + }, + ); + return response; + } catch (e) { + debugPrint('Error deleting space: $e'); + return false; + } + } + + Future> getSpaceHierarchy(String communityId) async { + try { + final response = await HTTPService().get( + path: ApiEndpoints.getSpaceHierarchy + .replaceAll('{communityId}', communityId), + showServerMessage: true, + expectedResponseModel: (json) { + return (json['data'] as List) + .map((spaceJson) => SpaceModel.fromJson(spaceJson)) + .toList(); + }, + ); + return response; + } catch (e) { + debugPrint('Error fetching space hierarchy: $e'); + return []; + } + } +} diff --git a/lib/utils/app_routes.dart b/lib/utils/app_routes.dart index 3714aa69..b14da393 100644 --- a/lib/utils/app_routes.dart +++ b/lib/utils/app_routes.dart @@ -12,7 +12,7 @@ class AppRoutes { return [ GoRoute( path: RoutesConst.auth, - builder: (context, state) => SpaceManagementPage(), + builder: (context, state) => const HomePage(), ), GoRoute( path: RoutesConst.home, @@ -32,7 +32,7 @@ class AppRoutes { ), GoRoute( path: RoutesConst.spacesManagementPage, - builder: (context, state) => SpaceManagementPage()), + builder: (context, state) => SpaceManagementPage()), ]; } } diff --git a/lib/utils/constants/api_const.dart b/lib/utils/constants/api_const.dart index 2d85a7f5..487aeaa3 100644 --- a/lib/utils/constants/api_const.dart +++ b/lib/utils/constants/api_const.dart @@ -1,6 +1,7 @@ abstract class ApiEndpoints { static const String baseUrl = 'https://syncrow-dev.azurewebsites.net'; - // static const String baseUrl = 'http://100.107.182.63:4001'; //Localhost + static const String baseLocalUrl = 'http://localhost:4001'; //Localhost + //https://syncrow-staging.azurewebsites.net ////////////////////////////////////// Authentication /////////////////////////////// static const String signUp = '$baseUrl/authentication/user/signup'; @@ -38,4 +39,31 @@ abstract class ApiEndpoints { static const String getDeviceLogs = '$baseUrl/device/report-logs/{uuid}?code={code}'; + +// Space Module + static const String createSpace = + '$baseLocalUrl/communities/{communityId}/spaces'; + static const String listSpaces = + '$baseLocalUrl/communities/{communityId}/spaces'; + static const String deleteSpace = + '$baseLocalUrl/communities/{communityId}/spaces/{spaceId}'; + static const String updateSpace = + '$baseLocalUrl/communities/{communityId}/spaces/{spaceId}'; + static const String getSpace = + '$baseLocalUrl/communities/{communityId}/spaces/{spaceId}'; + static const String getSpaceHierarchy = + '$baseLocalUrl/communities/{communityId}/spaces/hierarchy'; + +// Community Module + static const String createCommunity = '$baseLocalUrl/communities'; + static const String getCommunityList = '$baseLocalUrl/communities'; + static const String getCommunityById = + '$baseLocalUrl/communities/{communityId}'; + static const String updateCommunity = + '$baseLocalUrl/communities/{communityId}'; + static const String deleteCommunity = + '$baseLocalUrl/communities/{communityId}'; + static const String getUserCommunities = + '$baseLocalUrl/communities/user/{userUuid}'; + static const String createUserCommunity = '$baseLocalUrl/communities/user'; }