Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,26 @@ public ResponseEntity<SuccessResponse<List<Movie>>> getAllMovies(

return ResponseEntity.ok(response);
}


@Operation(
summary = "Get all distinct genres",
description = "Retrieve a list of all unique genre values from the movies collection. " +
"Demonstrates the distinct() operation. Returns genres sorted alphabetically."
)
@GetMapping("/genres")
public ResponseEntity<SuccessResponse<List<String>>> getDistinctGenres() {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add tests for this to the test target? Looks like we have both Controller and Service tests there where we may want to consider adding this.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

List<String> genres = movieService.getDistinctGenres();

SuccessResponse<List<String>> response = SuccessResponse.<List<String>>builder()
.success(true)
.message("Found " + genres.size() + " distinct genres")
.data(genres)
.timestamp(Instant.now().toString())
.build();

return ResponseEntity.ok(response);
}

@Operation(
summary = "Get a single movie by ID",
description = "Retrieve a single movie by its MongoDB ObjectId."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ public interface MovieService {

List<Movie> getAllMovies(MovieSearchQuery query);

/**
* Gets all distinct genre values from the movies collection.
* Demonstrates the distinct() operation.
*
* @return List of unique genre strings, sorted alphabetically
*/
List<String> getDistinctGenres();

Movie getMovieById(String id);

Movie createMovie(CreateMovieRequest request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,25 @@ public List<Movie> getAllMovies(MovieSearchQuery query) {

return mongoTemplate.find(mongoQuery, Movie.class);
}


@Override
public List<String> getDistinctGenres() {
// Use MongoTemplate's findDistinct to get all unique values from the genres array field
// MongoDB automatically flattens array fields when using distinct()
List<String> genres = mongoTemplate.findDistinct(
new Query(),
Movie.Fields.GENRES,
Movie.class,
String.class
);

// Filter out null/empty values and sort alphabetically
return genres.stream()
.filter(genre -> genre != null && !genre.isEmpty())
.sorted(String::compareTo)
.collect(Collectors.toList());
}

@Override
public Movie getMovieById(String id) {
if (!ObjectId.isValid(id)) {
Expand Down
39 changes: 39 additions & 0 deletions mflix/server/python-fastapi/src/routers/movies.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
Search movies using MongoDB Vector Search to enable semantic search capabilities over
the plot field.

- GET /api/movies/genres :
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same Q here - since this is a new route, should we add a test for it in the tests dir?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

Retrieve all distinct genre values from the movies collection.
Demonstrates the distinct() operation.

- GET /api/movies/{id} :
Retrieve a single movie by its ID.

Expand Down Expand Up @@ -427,6 +431,41 @@ async def vector_search_movies(
detail=f"Error performing vector search: {str(e)}"
)

"""
GET /api/movies/genres

Retrieve all distinct genre values from the movies collection.
Demonstrates the distinct() operation.

Returns:
SuccessResponse[List[str]]: A response object containing the list of unique genres, sorted alphabetically.
"""

@router.get("/genres",
response_model=SuccessResponse[List[str]],
status_code=200,
summary="Retrieve all distinct genres from the movies collection.")
async def get_distinct_genres():
movies_collection = get_collection("movies")

try:
# Use distinct() to get all unique values from the genres array field
# MongoDB automatically flattens array fields when using distinct()
genres = await movies_collection.distinct("genres")
except Exception as e:
raise HTTPException(
status_code=500,
detail=f"Database error occurred: {str(e)}"
)

# Filter out null/empty values and sort alphabetically
valid_genres = sorted([
genre for genre in genres
if isinstance(genre, str) and len(genre) > 0
])

return create_success_response(valid_genres, f"Found {len(valid_genres)} distinct genres")

"""
GET /api/movies/{id}
Retrieve a single movie by its ID.
Expand Down