darkcohiba / Phase-4-Python-Mock-CC-Movies-Actors

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Flask Mock Challenge - Movies and Actors

It is the year 2030 and IMDB crashed, it is up to you to create a database for people to see which actor was in which movie. You are movie lovers only hope, may the force be with you.

In this repo:

  • There is a Flask application with some features built out.

You can check your API by:

  • Using Postman, Thunder Client or a different request engine to make requests

Setup

To download the dependencies for the frontend and backend, run:

pipenv install && pipenv shell

You can run your Flask API on localhost:5555 by running:

python server/app.py

Your job is to build out the Flask API to add the functionality described in the deliverables below.


Models

It is your job to build out Movie, Actor, and Credit models so that Actors can be in Movies. In a given credit, one Actor will be in one Movie. Over their careers, Actors will be in many Movies and Movies will be have many Actors.

You will implement an API for the following data model:

Schema

The file server/models.py defines the model classes without relationships or fields.

Create the fields based off the above schema.

Now you can implement the relationships as shown in the ER Diagram:

  • A Actor has many Movie through Credits
  • An Movie has many Actors through Credits
  • A Credit belongs to a Movie and belongs to a Actor

Update server/models.py to establish the model relationships. Since a Credit belongs to a Actor and a Movie, configure the model to cascade deletes.

After creating the relationships, we intialize our DB, create our migration, upgrade our database with our migration:

cd server/
export FLASK_APP=app.py
flask db init
flask db migrate
flask db upgrade

Set serialization rules to limit the recursion depth.

Seed the database:

python seed.py

If you aren't able to get the provided seed file working, you are welcome to generate your own seed data to test the application.



Validations

Add validations to the Actor model:

  • must have a name, and an age that is great than 10

Add validations to the Credit model:

  • must have a role, a actor_id and a movie_id
  • role must be either: ["Performer", "Director", "Producor", "Playwright", "Lighting Design", "Sound Design", "Set Design"].

Add validations to the Movie model:

  • must have a rating between 1 and 10.
  • genre must be one of the following: [ "Action", "Comedy", "Drama", "Horror", "Romance", "Thriller", "Science Fiction", "Fantasy", "Mystery", "Adventure", "Crime", "Family", "Animation", "Documentary", "War" ]

Routes

Set up the following routes. Make sure to return JSON data in the format specified along with the appropriate HTTP verb.

Recall you can specify fields to include or exclude when serializing a model instance to a dictionary using to_dict() (don't forget the comma if specifying a single field).

NOTE: If you choose to implement a Flask-RESTful app, you need to add code to instantiate the Api class in server/app.py.

GET /actors

Return JSON data, without any additional nested data related to each actor.

GET /actors/int:id

If the Actor exists, return JSON data. Make sure to include a list of Movies for the actor.

If the Actor does not exist, return the following JSON data, along with the appropriate HTTP status code:

{
  "error": "Actor not found"
}

POST /actors

This route should create a new Actor. It should accept an object with the following properties in the body of the request:

{
  "name": "Shawn Horizon",
  "age": 52
}

If the Actor is created successfully, send back a response with the new Actor.

If the Actor is not created successfully due to validation errors, return the following JSON data, along with the appropriate HTTP status code:

{
  "errors": ["validation errors"]
}

PATCH /actors/:id

This route should update an existing Actor. It should accept an object with one or more of the following properties in the body of the request:

{
  "name": "Bevan Horizon",
  "age": 12
}

If the Actor is updated successfully, send back a response with the updated Actor and a 202 accepted status code.

If the Actor is not updated successfully, return the following JSON data, along with the appropriate HTTP status code:

{
  "errors": ["validation errors"]
}

OR, given an invalid ID, the appropriate HTTP status code, and the following JSON:

{
  "error": "Actor not found"
}

DELETE /actors/int:id

If the Actor exists, it should be removed from the database, along with any Credits that are associated with it. If you did not set up your models to cascade deletes, you need to delete associated Credits before the Actor can be deleted.

After deleting the Actor, return an empty response body, along with the appropriate HTTP status code.

If the Actor does not exist, return the following JSON data, along with the appropriate HTTP status code:

{
  "error": "Actor not found"
}

GET /movies

Return JSON data without any additional nested data related to each movie.

POST /movies

This route should create a new Movie. It should accept an object with the following properties in the body of the request:

{
  "image": "https://flxt.tmsimg.com/assets/p1036_p_v12_an.jpg",
  "title": "Blazing Saddles",
  "genre": "Action",
  "rating": 7,
  "description": "In this satirical take on Westerns, crafty railroad worker Bart (Cleavon Little) becomes the first black sheriff of Rock Ridge, a frontier town about to be destroyed in order to make way for a new railroad. Initially, the people of Rock Ridge harbor a racial bias toward their new leader. However, they warm to him after realizing that Bart and his perpetually drunk gunfighter friend (Gene Wilder) are the only defense against a wave of thugs sent to rid the town of its population."
}

If the Movie is created successfully, send back a response about the new movie.

If the Movie is not created successfully, return the following JSON data, along with the appropriate HTTP status code:

{
  "errors": ["validation errors"]
}

GET /credits

Return JSON data without any additional nested data related to each credit.

GET /credits/int:id

If the Credit exists, Make sure to include the actor and movie associated.

If the Credit does not exist, return the following JSON data, along with the appropriate HTTP status code:

{
  "error": "Credit not found"
}

POST /credits

This route should create a new Credit. It should accept an object with the following properties in the body of the request:

{
  "actor_id": 2,
  "movie_id": 1,
  "role": "Performer"
}

If the Credit is created successfully, send back a response with the new Credit.

If the Credit is not created successfully due to validation errors, return the following JSON data, along with the appropriate HTTP status code:

{
  "errors": ["validation errors"]
}

PATCH /credit/:id

This route should update an existing Credit. It should accept an object with one or more of the following properties in the body of the request:

{
  "role": "Director",
}

If the Actor is updated successfully, send back a response with the updated Actor and a 202 accepted status code.

If the Credit is not updated successfully, return the following JSON data, along with the appropriate HTTP status code:

{
  "errors": ["validation errors"]
}

OR, given an invalid ID, the appropriate HTTP status code, and the following JSON:

{
  "error": "Credit not found"
}

DELETE /credits/int:id

If the Credit exists, it should be removed from the database.

After deleting the Credit, return an empty response body, along with the appropriate HTTP status code.

If the Credit does not exist, return the following JSON data, along with the appropriate HTTP status code:

{
  "error": "Credit not found"
}

About


Languages

Language:Python 100.0%