createMovieList method

Future<String?> createMovieList(
  1. String listName, {
  2. List<Movie>? movies,
  3. String? description,
})

Creates a new MovieList with the given name and movies.

Implementation

Future<String?> createMovieList(String listName,
    {List<Movie>? movies, String? description}) async {
  try {
    final loggedIn = await isLoggedIn();
    if (!loggedIn) {
      debugPrint('❌ User not logged in, cannot create movie list');
      return null;
    }

    // Generate unique ID for the movie list.

    final movieListId = TurtleSerializer.generateId();

    // All MovieLists follow the ontology naming convention: MovieList-{ID}.ttl.

    final fileName = 'user_lists/MovieList-$movieListId.ttl';

    // Create the MovieList TTL content.

    final movieListTtl = TurtleSerializer.createMovieList(
      movieListId,
      listName,
      movies: movies,
      description: description,
    );

    // Write to POD.

    if (!_context.mounted) return null;
    final result = await writePod(
      fileName,
      movieListTtl,
      _context,
      _child,
      encrypted: false,
    );

    if (result == SolidFunctionCallStatus.success) {
      // Update cache.

      _movieListCache[movieListId] = {
        'id': movieListId,
        'name': listName,
        'movies': movies ?? [],
        'filePath': 'moviestar/data/$fileName',
      };

      // Add to user profile.

      final profileUpdated =
          await _userProfileService.addMovieListToProfile(movieListId);
      if (!profileUpdated) {
        debugPrint('❌ Failed to add movie list to user profile');
      }

      return movieListId;
    }

    debugPrint('❌ Failed to write movie list to POD');
    return null;
  } catch (e) {
    debugPrint('❌ Exception in create movie list: $e');
    return null;
  }
}