sendMediaToLocation static method

Future<Map<String, String>> sendMediaToLocation(
  1. GalleryItem galleryItem
)

Implementation

static Future<Map<String, String>> sendMediaToLocation(GalleryItem galleryItem) async {
  bool isTokenValid = await AuthService.validateToken();

  if (isTokenValid) {
    final galleryItemJson = galleryItem.toJson();

    galleryItemJson.remove('id');
    galleryItemJson.remove('created_at');
    galleryItemJson.remove('updated_at');

    final body = json.encode(galleryItemJson);

    String to = '';
    int id = 0;

    if (galleryItem.locationId.isNotEmpty) {
      to = 'locations';
      id = int.parse(galleryItem.locationId);
      galleryItemJson.remove('practice_id');
    } else if (galleryItem.practiceId.isNotEmpty) {
      to = 'practices';
      id = int.parse(galleryItem.practiceId);
      galleryItemJson.remove('location_id');
    }

    debugPrint('[DEBUG]: sendMediaToLocation body: $body');

    final res = await httpClient.post(Config.getURI('/$to/$id/medias.json'), body: body);

    debugPrint('[DEBUG]: statusCode ${res.statusCode}');
    debugPrint('[DEBUG]: Body ${res.body}');

    dynamic message = json.decode(res.body);

    String error = message['error'].toString().replaceAll('{', '').replaceAll('}', '');

    if (res.statusCode >= 400) return {'status': 'failed', 'message': error};

    return {'status': 'success', 'message': 'Media added'};
  }
  return {'status': 'failed', 'message': 'An error occured. Please login again.'};
}