mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2025-07-11 07:37:48 +00:00
88 lines
2.7 KiB
Dart
88 lines
2.7 KiB
Dart
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
|
import 'package:syncrow_app/features/app_layout/model/space_model.dart';
|
|
import 'package:syncrow_app/features/auth/model/user_model.dart';
|
|
import 'package:syncrow_app/features/devices/model/subspace_model.dart';
|
|
import 'package:syncrow_app/services/api/api_links_endpoints.dart';
|
|
import 'package:syncrow_app/services/api/http_service.dart';
|
|
|
|
class SpacesAPI {
|
|
static final HTTPService _httpService = HTTPService();
|
|
|
|
static Future<List<SpaceModel>> getSpacesByUserId() async {
|
|
try {
|
|
var uuid =
|
|
await const FlutterSecureStorage().read(key: UserModel.userUuidKey);
|
|
if (uuid == null) throw Exception("User UUID is missing");
|
|
|
|
final path = ApiEndpoints.userSpaces.replaceFirst('{userUuid}', uuid);
|
|
final response = await _httpService.get(
|
|
path: path,
|
|
showServerMessage: false,
|
|
expectedResponseModel: (json) {
|
|
return SpaceModel.fromJsonList(json['data']);
|
|
},
|
|
);
|
|
|
|
return response;
|
|
} catch (error) {
|
|
rethrow; // Rethrow the error to be caught by `fetchUnitsByUserId`
|
|
}
|
|
}
|
|
|
|
static Future<List<SubSpaceModel>> getSubSpaceBySpaceId(
|
|
String communityId, String spaceId) async {
|
|
try {
|
|
// Construct the API path
|
|
final path = ApiEndpoints.listSubspace
|
|
.replaceFirst('{communityUuid}', communityId)
|
|
.replaceFirst('{spaceUuid}', spaceId);
|
|
|
|
|
|
final response = await _httpService.get(
|
|
path: path,
|
|
queryParameters: {"page": 1, "pageSize": 10},
|
|
showServerMessage: false,
|
|
expectedResponseModel: (json) {
|
|
|
|
List<SubSpaceModel> rooms = [];
|
|
if (json['data'] != null) {
|
|
for (var subspace in json['data']) {
|
|
rooms.add(SubSpaceModel.fromJson(subspace));
|
|
}
|
|
} else {
|
|
print("Warning: 'data' key is missing or null in response JSON.");
|
|
}
|
|
return rooms;
|
|
},
|
|
);
|
|
|
|
return response;
|
|
} catch (error, stackTrace) {
|
|
return []; // Return an empty list if there's an error
|
|
}
|
|
}
|
|
|
|
static Future<String> generateInvitationCode(
|
|
String unitId,
|
|
) async {
|
|
final response = await _httpService.get(
|
|
path: ApiEndpoints.invitationCode.replaceAll('{unitUuid}', unitId),
|
|
showServerMessage: false,
|
|
expectedResponseModel: (json) => json['invitationCode'],
|
|
);
|
|
return response;
|
|
}
|
|
|
|
static Future<bool> joinUnit(
|
|
Map<String, String> body,
|
|
) async {
|
|
final response = await _httpService.post(
|
|
path: ApiEndpoints.verifyInvitationCode,
|
|
showServerMessage: false,
|
|
body: body,
|
|
expectedResponseModel: (json) => json['success'],
|
|
);
|
|
return response;
|
|
}
|
|
}
|