You are given a full-stack Movie DB application. Users can log in, create, update, and delete watch lists, and add or remove movies from a watch list.
Several unit tests are failing around watch-list movie operations. Debug and fix the backend logic so the following scenarios work correctly:
-
Add a movie to an existing watch list.
-
Add a movie that is already present in the watch list.
-
Remove a movie from an existing watch list.
-
Add many movies to a watch list, then remove them one by one.
-
Try to add a movie to a watch list that does not exist.
-
Try to remove a movie from a watch list that does not exist.
Your fix should ensure that:
-
The code checks whether the watch list exists before modifying it.
-
The code checks whether the movie exists when adding a movie.
-
Duplicate movies are not added to the same watch list.
-
Removing a movie updates the persisted watch list correctly.
-
All database changes are saved before returning a response.
-
Each failure case returns the correct HTTP status code and error response.
Assume a conventional REST contract such as:
-
POST /watchlists/{watchListId}/movies
adds a movie.
-
DELETE /watchlists/{watchListId}/movies/{movieId}
removes a movie.
-
404 Not Found
is returned when the watch list or movie does not exist.
-
409 Conflict
is returned when attempting to add a duplicate movie.
-
201 Created
or
200 OK
is returned for a successful add.
-
200 OK
or
204 No Content
is returned for a successful delete, depending on the existing API convention.