def patch_movie(payload: PayloadJSON, movie_id: int) -> ResourceJSON: """Update a movie by movie_id""" movie = MovieModel.find_by_id(movie_id) if movie is None: abort(404) data = request.get_json() title = data.get("title", None) release_date = data.get("release_date", None) if title is None or release_date is None: abort(400) movie.title = title movie.release_date = release_date result = movie.save_to_db() if result["error"]: abort(500) _id = result["id"] return jsonify({ "success": True, "movie": MovieModel.find_by_id(_id).json() })
def post_movie(payload: PayloadJSON) -> ResourceJSON: """Create a new movie""" data = request.get_json() movie = MovieModel(**data) result = movie.save_to_db() if result["error"]: abort(500) _id = result["id"] return jsonify({ "success": True, "movie": MovieModel.find_by_id(_id).json() })
def get_movie(payload: PayloadJSON, movie_id: int) -> ResourceJSON: """Return a movie by movie_id""" movie = MovieModel.find_by_id(movie_id) if movie is None: abort(404) return jsonify({"success": True, "movie": movie.json()}, )
def get_movies(payload: PayloadJSON) -> ResourceJSON: """Return a list of movies""" _movies = MovieModel.find_all() if len(_movies) == 0: abort(404) return jsonify({ "success": True, "movies": [movie.json() for movie in _movies], })
def add_actor_to_movie(payload: PayloadJSON, movie_id: int, actor_id: int) -> ResourceJSON: """Add an actor by actor_id to a movie by movie_id""" movie = MovieModel.find_by_id(movie_id) actor = ActorModel.find_by_id(actor_id) if movie is None or actor is None: abort(404) movie.actors.append(actor) result = movie.save_to_db() if result["error"]: abort(500) _id = result["id"] return jsonify({ "success": True, "movie": MovieModel.find_by_id(_id).json() })
def test_json(self): with self.app_context: self.movie.save_to_db() movie = MovieModel.find_by_id(1) self.assertDictEqual( movie.json(), { "id": self.movie.id, "title": self.movie.title, "release_date": self.movie.release_date, "actors": [], }, )
def delete_movie(payload: PayloadJSON, movie_id: int) -> ResourceJSON: """Delete a movie by movie_id""" movie = MovieModel.find_by_id(movie_id) if movie is None: abort(404) result = movie.delete_from_db() if result["error"]: abort(500) return jsonify({"success": True, "deleted": movie_id}, )
class TestMovieModel(BaseTest): """Test all methods for the MovieModel""" def setUp(self): super(TestMovieModel, self).setUp() with self.app_context: self.movie = MovieModel( title="My Great Movie", release_date="2020-01-31" ) def test_init(self): with self.app_context: self.assertEqual(self.movie.title, "My Great Movie") self.assertEqual(self.movie.release_date, "2020-01-31") def test_find_all(self): with self.app_context: self.movie.save_to_db() movies = MovieModel.find_all() self.assertEqual(1, movies[0].id) def test_find_by_id(self): with self.app_context: self.movie.save_to_db() movie = MovieModel.find_by_id(1) self.assertEqual(1, movie.id) def test_find_by_title(self): with self.app_context: self.movie.save_to_db() movie = MovieModel.find_by_title("My Great Movie") self.assertEqual(1, movie.id) def test_json(self): with self.app_context: self.movie.save_to_db() movie = MovieModel.find_by_id(1) self.assertDictEqual( movie.json(), { "id": self.movie.id, "title": self.movie.title, "release_date": self.movie.release_date, "actors": [], }, )
def setUp(self): super(TestMovieModel, self).setUp() with self.app_context: self.movie = MovieModel( title="My Great Movie", release_date="2020-01-31" )
def test_find_by_title(self): with self.app_context: self.movie.save_to_db() movie = MovieModel.find_by_title("My Great Movie") self.assertEqual(1, movie.id)
def test_find_by_id(self): with self.app_context: self.movie.save_to_db() movie = MovieModel.find_by_id(1) self.assertEqual(1, movie.id)
def test_find_all(self): with self.app_context: self.movie.save_to_db() movies = MovieModel.find_all() self.assertEqual(1, movies[0].id)