/back-cineco

Data base for frontend cineco project

Primary LanguageJavaScript

Cinema API - Backend

This API manages the information for a frontend application for a user to select a movie, a cinena, and finally booking a seat to see a movie.

USER INSTRUCTIONS Template from an old project

How-to-use video:

This video shows how to use basic workflow for CRUD operations with the API.

You can see the prodution FAVS-API deployed here

Instructions

This API aims to provide a better way to organize your favorite things: music, clothes, courses, etc., all in one place. You can create a user, then login into your account to create a list of favorite things you like, and finally add items to your list. Later on, this API can be consumed with a frontend application to ultimately allow a nicer user interface (UI).

Pre-requisites

First you need an empty MySQL data base to connect to. Make sure you have a user, a password and the port number to connect to your data base. The data base connection is setup with a connection string that needs those parameters.

Finally, you also need a REST Client to test the different request methods (CREATE, GET, UPDATE, DELETE) with the API endpoints. You can use the one you prefer, for example, Postman, Thunder Client, or Insomnia.

Using the API:

1. Download the files

Download the files from the repository to your local computer. Fork or Clone options are available in the Github repository.

2. Create a dotenv (.env)

In the root folder (from the downloaded repository) create a new .env file. Open the . env file and setup your environment variables writing these lines:

DATABASE_URL = "copy your connection string to your mysql data base here";
PORT = "write the port number you want to use here";
SECRET =
  "copy a string you want to use a secret to generate authorization tokens";
  • Here is an example of how it should look like:
DATABASE_URL = "mysql://myuser:mypassword@localhost:3306/my_database";
PORT = 4000;
SECRET = "this_is_a_nice_secure_string:_eluieIqUpRM0u6SZs716SwOiD29c0E";

3. Install package.json dependencies

Before running the API, you need to install the dependencies. Open a terminal in your project folder and run de command: npm install.

4. Synchronize the schema and the database

In a terminal window run the comand npx prisma migrate reset. If your data base is connected this command will errase your mysql-model (and data also), create a new one, an because the repository has a migration folde included, I'will run this migration to start anew.

5. Run the API

After dependencies are installed, open a terminal in your project folder and run de command: npm start.

Now you should see a message in your terminal window displaying: Server Initialized on PORT.

6. TEST endpoints

Use your REST client to make the request to the available endpoints. You can find a list of endpoints in the table below.

Also, there is a folder in the repository with a backup file that has a collection of requests that can be imported into your REST Client (see folder src/restclient_backup_collection).

Route HTTP Verb Description
/user/signup POST Creates a new user
/api/favs GET Get all list of favorites
/api/favs POST Creates a new list of favorites
/api/favs/:id GET Get a single list of favorites
/api/favs/:id DELETE Deletes a list of favorites
/item/:id POST Creates an item to include in the list
/user/auth/local/login POST Login user by email/password

Workflow example

  • Create a new user:

By using the route /user/signup, and the post method. Write the email and password in the body request and make sure you pass the data in a JSON format. The creation has some verification so make sure you enter a valid email and a strong password.

Example:

{
  "email": "testme@example.com",
  "password": "Test123!",
  "repassword": "Test123!"
}
  • Login:

Login with the user you created. The route is: /user/auth/local/login and the method is post. Use the email and password in the body request and write it in a JSON format.

Example:

{
  "email": " testme @example.com",
  "password": "Test123!"
}

IMPORTANT: You will get a response with the token as a string. This token has to be copied and pass for the next endpoints to work properly.

Pass the token in the request headers: After login in, for each of the endpoints below. The token got to be paste in the header in order to get a valid response. First open the request, go to the headers tab, fill in a new header with the title Authorization, then right next to it write this as the value: first the word Beared follow by a blank space and finally the token you copied.

  • Create a new list:

Create a new list of favorites: route is /api/favs and method is post. Write the name for the list and the user for that list.

Example:

{
  "name": "next week",
  "user_iduser": 1
}
  • Create a new item:

Create a new item for the list: route is /item/:id, and method is post. Note that :id should be change to match the id of the list to include the item to. Write the title, description, link, and list id in the body request using a JSON format.

Example:

{
  "title": "playing guitar",
  "description": "Every day I practice 15 minutes",
  "link": "https://youtu.be/w4a2ge9N31E",
  "lists_idlist": 1
}
  • Get all the lists :

Get all the lists of favorites. route: /api/favs, and method get. Write down into the body request the id for the user to display.

Example:

{
  "userId": 1
}
  • Get only one list :

Get only one list of favorites: route /api/favs/:id, and method get. Note that :id should be change to match the id of the list to be displayed.

  • Delete a list :

Delete a list: route /api/favs/:id, and method delete. Note that :id should be change to match the id of the list to be deleted.

CAUTION: beware that by deleting a list, the items of the list will be deleted as well.

7. Verify changes

Finally, after testing the endpoints you will see that your database has now been changed according to the endpoint that you tested.