mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-09 22:57:21 +00:00
added api for space management
This commit is contained in:
230
lib/services/space_mana_api.dart
Normal file
230
lib/services/space_mana_api.dart
Normal file
@ -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<List<CommunityModel>> fetchCommunities() async {
|
||||
try {
|
||||
final response = await HTTPService().get(
|
||||
path: ApiEndpoints.getCommunityList,
|
||||
showServerMessage: true,
|
||||
expectedResponseModel: (json) {
|
||||
List<dynamic> jsonData = json;
|
||||
List<CommunityModel> communityList = jsonData.map((jsonItem) {
|
||||
return CommunityModel.fromJson(jsonItem);
|
||||
}).toList();
|
||||
return communityList;
|
||||
},
|
||||
);
|
||||
return response;
|
||||
} catch (e) {
|
||||
debugPrint('Error fetching communities: $e');
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
Future<CommunityModel?> 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<CommunityModel?> 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<bool> 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<bool> 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<SpacesResponse> 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<SpaceModel> 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<SpaceModel?> 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<bool> 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<bool> 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<List<SpaceModel>> 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 [];
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user