Skip to content

Enhanced the updateSearchCount function to remove Duplicacy in trending movie list #9

@aashu700

Description

@aashu700

export const updateSearchCount = async (searchTerm, movie) => {
try {
let doc;

try {
  // Try to get the document
  doc = await database.getDocument(DATABASE_ID, COLLECTION_ID, getUniqueId(movie.id));

  // Update count if found,
  await database.updateDocument(DATABASE_ID, COLLECTION_ID, doc.$id, {
    count: doc.count + 1,
  });

} catch (error) {
  if (error.code === 404) {
    // Document not found → create a new one
    console.log("Document not found, creating new...");
    await database.createDocument(DATABASE_ID, COLLECTION_ID, getUniqueId(movie.id), {
      searchTerm,
      count: 1,
      movie_id: movie.id,
      poster_url: `https://image.tmdb.org/t/p/w500${movie.poster_path}`,
    });
  } else {
    // Some other error
    throw error;
  }
}

} catch (err) {
console.error("Unexpected error =>>", err);
}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions