Activity 50: Keriderya API

·

16 min read

Step 1: Fork the Repository

  1. Go to the GitHub repository: Visit the repository https://github.com/thirdygayares/keriderya-api on GitHub.

    • URL: https://github.com/thirdygayares/keriderya-api
  2. Fork the Repository:

    • On the top-right corner of the repository page, click the Fork button.

    • Choose your GitHub account to fork the repository to.


Step 2: Clone Your Repository

  1. After forking the repository, clone it to your local machine.

  2. Open a terminal or command prompt, and run the following command (replace your-username with your GitHub username):

      git clone https://github.com/JovRoncal/keriderya-api
    

  3. Navigate to the project directory:

      cd <project-name>
    


Step 3: Set Up the Project

A. Create a Virtual Environment

  1. In the terminal, create a new virtual environment. This is essential to isolate your project's dependencies from the global Python environment.

    For Windows:

      python -m venv venv
    

  2. Activate the virtual environment:

    For Windows:

      venv\Scripts\activate
    

B. Install Requirements

  1. With the virtual environment activated, install the necessary dependencies using the requirements.txt file.

      pip install -r requirements.txt
    

C. Add .env File

  1. Create a .env file in the root of the project directory.

  2. Add the following environment variables to the .env file:

      FLASK_ENV=development
      DATABASE_URL=mysql://avnadmin:{password}@{host}:{port}/{databasename1}
      SECRET_KEY=your_secret_key
    
    • Replace {password}, {host}, {port}, and {databasename1} with the appropriate values from your Aiven MySQL database.

    • SECRET_KEY should be a secret string (you can generate one randomly).

D. Copy Certs to app/certs

  1. Copy any required certificate files (if needed) to the app/certs directory.

E. Run Database Migrations

  1. In the terminal, navigate to the project directory and run:

      flask db upgrade
    

    This will apply any database migrations to set up the tables required by the application.

F. Run the Application

  1. To run the application, execute the following command:

      python run.py
    

    This will start the Flask application on http://127.0.0.1:5000/.


Step 4: Create a Database in Aiven

  1. Log into your Aiven account: Go to the Aiven dashboard at Aiven.

  2. Create a New Database:

    • Choose the MySQL service.

    • Enter a database name (e.g., databasename1).

    • Follow the Aiven setup process to complete the database creation.

  3. Get Database Connection Details:

    • After setting up the database, you’ll get a connection URL in the format:

          mysql://avnadmin:{password}@{host}:{port}/{databasename1}
      
    • Use this connection URL in your .env file.


Step 5: Code Explanation and Postman Testing

Code Overview

  1. run.py: This file runs the Flask application and serves it on the specified port (5000 by default).

  2. Database Models: The code likely uses SQLAlchemy for database interaction. The models define the structure of tables and relationships in the database (e.g., User model).

  3. API Routes: Routes are defined in app/routes.py or similar, handling HTTP requests like GET, POST, PUT, etc., for interacting with the User resource.


Postman Testing

  1. Create User in Postman:

    • Open Postman and create a new POST request.

    • Set the URL to http://127.0.0.1:5000/users.

    • In the "Body" tab, select raw and choose JSON as the format.

    • Add the user data (similar to the example above) and send the request.

    • If successful, you should get a response with the created user’s data, including their ID.

          {
            "username": "jov",
            "email": "jov@gmail.com",
            "password": "ramonjov123",
             "role": "user"
          }
      

  1. Get User by UID in Postman:

    • Create a new GET request.

    • Set the URL to http://127.0.0.1:5000/users/user_uid (or another UID you want to query).

    • Send the request and verify that you get the user details for the given UID.

  1. Get All Users (GET Request)

    • Step 1: Create a new GET request in Postman.

    • Step 2: Set the URL to http://127.0.0.1:5000/users to retrieve a list of all users.

    • Step 3: Click Send.

    • Step 4: The response will contain a list of all users:

  2. Update User (PUT Request)

  • Step 1: Create a new PUT request in Postman.

  • Step 2: Set the URL to http://127.0.0.1:5000/users/{user_uid}, replacing {user_uid} with the user’s UID you want to update. For example: http://127.0.0.1:5000/users/384cdf95-db30-42f3-a852-e9705ee850b6.

  • Step 3: In the "Body" tab, select raw and choose JSON as the format.

  • Step 4: Add the updated user data. For example, to change the user's name:

        {
          "username": "roncal",
          "email": "roncal@gmail.com",
          "password": "ramonjov123",
           "role": "user"
        }
    
  • Step 5: Click Send to send the PUT request.

  • Step 6: If successful, the response will contain the updated user data:

  1. Delete User (DELETE Request)

    • Step 1: Create a new DELETE request in Postman.

    • Step 2: Set the URL to http://127.0.0.1:5000/users/{user_uid}, replacing {user_uid} with the UID of the user you want to delete. For example: http://127.0.0.1:5000/users/ad38f820-42f2-422c-8053-6573cd48eca9.

    • Step 3: Click Send to delete the user.

Result

1. Create Restaurant Category (POST Request)

Step 1: Open Postman and Create a New POST Request

Step 2: Set the Request Body

  • Go to the Body tab.

  • Select raw and choose JSON from the dropdown.

  • Add the category data in JSON format. For example:

      {
        "name": "Cafe"
      }
    

Step 3: Send the Request

  • Click Send to create the restaurant category.

Step 4: Verify the Response

  • If the request is successful, you should receive a response with the created restaurant category’s data, including an ID. For example:

      {
          "category_id": 1,
          "category_uuid": "8ec547ff-e345-455b-af7f-23b77f9fb3d3",
          "created_at": "2024-11-27T05:25:38",
          "deleted_at": null,
          "name": "Cafe",
          "updated_at": "2024-11-27T05:25:38"
      }
    


2. Get Restaurant Category by ID (GET Request)

Step 1: Create a New GET Request

Step 2: Send the Request

  • Click Send to get the details of the specified restaurant category.

Step 3: Verify the Response

  • If the category exists, the response should contain the category data, such as:


3. Get All Restaurant Categories (GET Request)

Step 1: Create a New GET Request

Step 2: Send the Request

  • Click Send to retrieve a list of all restaurant categories.

Step 3: Verify the Response

  • The response will contain a list of all restaurant categories. For example:


4. Update Restaurant Category (PUT Request)

Step 1: Create a New PUT Request

Step 2: Set the Request Body

  • Go to the Body tab.

  • Select raw and choose JSON from the dropdown.

  • Add the updated category data. For example, to update the category's name:

      {
        "name": "Pub"
      }
    

Step 3: Send the Request

  • Click Send to send the PUT request.

Step 4: Verify the Response

  • If the request is successful, the response will contain the updated category data, such as:

      {
          "category_id": 5,
          "category_uuid": "5e68e9b4-72da-4bcc-a061-25c62e3193d4",
          "created_at": "2024-11-27T05:27:46",
          "deleted_at": null,
          "name": "Pub",
          "updated_at": "2024-11-27T05:44:29"
      }
    


5. Delete Restaurant Category (DELETE Request)

Step 1: Create a New DELETE Request

Step 2: Send the Request

  • Click Send to delete the restaurant category.

Step 3: Verify the Response

RESULT

1. Create Restaurant (POST Request)

Step 1: Open Postman and Create a New POST Request

Step 2: Set the Request Body

  • Go to the Body tab.

  • Select raw and choose JSON from the dropdown.

  • Add the restaurant data in JSON format. For example:

    
      {
        "name": "Sunrise Café",
        "address": "123 Morning Ave, Downtown City",
        "phone": "+1 (555) 123-4567",
        "email": "info@sunrisecafe.com",
        "status": "true",
        "description": "Sunrise Café is a cozy and inviting spot where locals come to enjoy freshly brewed coffee, homemade pastries, and a warm atmosphere.",
        "user_id": "2",
        "category_id": "1"
      }
    
    • category_id refers to the ID of the restaurant category (e.g., Italian, Mexican) that the restaurant belongs to.

Step 3: Send the Request

  • Click Send to create the restaurant.

Step 4: Verify the Response

  • If the request is successful, you should receive a response with the created restaurant’s data, including an ID. For example:

      {
          "address": "123 Morning Ave, Downtown City",
          "category_id": 1,
          "created_at": "2024-11-27T06:11:42",
          "deleted_at": null,
          "description": "Sunrise Café is a cozy and inviting spot where locals come to enjoy freshly brewed coffee, homemade pastries, and a warm atmosphere.",
          "email": "info@sunrisecafe.com",
          "name": "Sunrise Café",
          "phone": "+1 (555) 123-4567",
          "restaurant_id": 1,
          "restaurant_uuid": "d5a263ca-a90d-4c9a-ab9c-30db5ecc0e17",
          "status": true,
          "updated_at": "2024-11-27T06:11:42",
          "user_id": 2
      }
    


2. Get Restaurant by ID (GET Request)

Step 1: Create a New GET Request

Step 2: Send the Request

  • Click Send to get the details of the specified restaurant.

Step 3: Verify the Response

  • If the restaurant exists, the response should contain the restaurant data, such as:

      {
          "address": "789 Pizza Lane, Westside District",
          "category_id": 4,
          "created_at": "2024-11-27T06:17:45",
          "deleted_at": null,
          "description": "Bella Italia Pizzeria offers authentic Italian pizza made with fresh, high-quality ingredients.",
          "email": "info@bellaitaliapizza.com",
          "name": "Bella Italia Pizzeria",
          "phone": "+1 (555) 321-4321",
          "restaurant_id": 3,
          "restaurant_uuid": "08468d69-35b5-403b-8f49-45c9c772f4f4",
          "status": true,
          "updated_at": "2024-11-27T06:17:45",
          "user_id": 2
      }
    


3. Get All Restaurants (GET Request)

Step 1: Create a New GET Request

Step 2: Send the Request

  • Click Send to retrieve a list of all restaurants.

Step 3: Verify the Response

  • The response will contain a list of all restaurants. For example:

      [
          {
              "address": "123 Morning Ave, Downtown City",
              "category_id": 1,
              "created_at": "2024-11-27T06:11:42",
              "deleted_at": null,
              "description": "Sunrise Café is a cozy and inviting spot where locals come to enjoy freshly brewed coffee, homemade pastries, and a warm atmosphere.",
              "email": "info@sunrisecafe.com",
              "name": "Sunrise Café",
              "phone": "+1 (555) 123-4567",
              "restaurant_id": 1,
              "restaurant_uuid": "d5a263ca-a90d-4c9a-ab9c-30db5ecc0e17",
              "status": true,
              "updated_at": "2024-11-27T06:11:42",
              "user_id": 2
          },
          {
              "address": "456 Retro Blvd, Midtown City",
              "category_id": 2,
              "created_at": "2024-11-27T06:17:08",
              "deleted_at": null,
              "description": "Classic Diner serves all-American comfort food in a nostalgic, retro atmosphere. ",
              "email": "contact@classicdiner.com",
              "name": "Classic Diner",
              "phone": "+1 (555) 987-6543",
              "restaurant_id": 2,
              "restaurant_uuid": "3655fc22-eaff-40e1-9ba9-644484cda317",
              "status": true,
              "updated_at": "2024-11-27T06:17:08",
              "user_id": 2
          },
          {
              "address": "789 Pizza Lane, Westside District",
              "category_id": 4,
              "created_at": "2024-11-27T06:17:45",
              "deleted_at": null,
              "description": "Bella Italia Pizzeria offers authentic Italian pizza made with fresh, high-quality ingredients.",
              "email": "info@bellaitaliapizza.com",
              "name": "Bella Italia Pizzeria",
              "phone": "+1 (555) 321-4321",
              "restaurant_id": 3,
              "restaurant_uuid": "08468d69-35b5-403b-8f49-45c9c772f4f4",
              "status": true,
              "updated_at": "2024-11-27T06:17:45",
              "user_id": 2
          }
      ]
    


4. Update Restaurant (PUT Request)

Step 1: Create a New PUT Request

Step 2: Set the Request Body

  • Go to the Body tab.

  • Select raw and choose JSON from the dropdown.

  • Add the updated restaurant data. For example, to change the restaurant's name and rating:

      {
        "name": "Cyra's Pizza",
        "address": "789 Pizza Lane, Westside District",
        "phone": "+1 (555) 321-4321",
        "email": "info@bellaitaliapizza.com",
        "status": "true",
        "description": "Cyra's pizza offers authentic Italian pizza made with fresh, high-quality ingredients.",
        "user_id": "2",
        "category_id": "4"
      }
    

Step 3: Send the Request

  • Click Send to send the PUT request.

Step 4: Verify the Response

  • If the request is successful, the response will contain the updated restaurant data, such as:

      {
          "address": "789 Pizza Lane, Westside District",
          "category_id": 4,
          "created_at": "2024-11-27T06:17:45",
          "deleted_at": null,
          "description": "Cyra's pizza offers authentic Italian pizza made with fresh, high-quality ingredients.",
          "email": "info@bellaitaliapizza.com",
          "name": "Cyra's Pizza",
          "phone": "+1 (555) 321-4321",
          "restaurant_id": 3,
          "restaurant_uuid": "08468d69-35b5-403b-8f49-45c9c772f4f4",
          "status": true,
          "updated_at": "2024-11-27T06:22:10",
          "user_id": 2
      }
    


5. Delete Restaurant (DELETE Request)

Step 1: Create a New DELETE Request

Step 2: Send the Request

  • Click Send to delete the restaurant.

Step 3: Verify the Response

RESULT

1. Create Menu Category (POST Request)

Step 1: Open Postman and Create a New POST Request

Step 2: Set the Request Body

  • Go to the Body tab.

  • Select raw and choose JSON from the dropdown.

  • Add the menu category data in JSON format. For example:

      {
          "name": "Desserts"
      }
    
    • name: The name of the menu category (e.g., Appetizers, Desserts, etc.).

Step 3: Send the Request

  • Click Send to create the menu category.

Step 4: Verify the Response

  • If the request is successful, you should receive a response with the created menu category’s data, including an ID. For example:

      {
          "created_at": "2024-11-27T06:40:43",
          "deleted_at": null,
          "menu_id": 6,
          "menu_uuid": "2952b80d-c188-4fbd-8f17-f0a79460f3f6",
          "name": "Desserts",
          "updated_at": "2024-11-27T06:40:43"
      }
    


2. Get Menu Category by ID (GET Request)

Step 1: Create a New GET Request

Step 2: Send the Request

  • Click Send to get the details of the specified menu category.

Step 3: Verify the Response

  • If the category exists, the response should contain the category data, such as:


3. Get All Menu Categories (GET Request)

Step 1: Create a New GET Request

Step 2: Send the Request

  • Click Send to retrieve a list of all menu categories.

Step 3: Verify the Response

  • The response will contain a list of all menu categories. For example:

      [
          {
              "created_at": "2024-11-27T06:39:32",
              "deleted_at": null,
              "menu_id": 2,
              "menu_uuid": "4880f6e7-443f-4eb6-9992-33812a6e4310",
              "name": "Breakfast",
              "updated_at": "2024-11-27T06:39:32"
          },
          {
              "created_at": "2024-11-27T06:39:50",
              "deleted_at": null,
              "menu_id": 3,
              "menu_uuid": "3bf0153e-2fcd-480b-8d2c-485f3d5a32e4",
              "name": "Salads & Sides",
              "updated_at": "2024-11-27T06:39:50"
          },
          {
              "created_at": "2024-11-27T06:40:00",
              "deleted_at": null,
              "menu_id": 4,
              "menu_uuid": "c4613cf3-1694-46a7-84ca-71c3ea3702e0",
              "name": "Pasta Dishes",
              "updated_at": "2024-11-27T06:40:00"
          },
          {
              "created_at": "2024-11-27T06:40:24",
              "deleted_at": null,
              "menu_id": 5,
              "menu_uuid": "73747e54-e530-476f-b537-713780354939",
              "name": "Wood-Fired Pizzas",
              "updated_at": "2024-11-27T06:40:24"
          },
          {
              "created_at": "2024-11-27T06:40:43",
              "deleted_at": null,
              "menu_id": 6,
              "menu_uuid": "2952b80d-c188-4fbd-8f17-f0a79460f3f6",
              "name": "Desserts",
              "updated_at": "2024-11-27T06:40:43"
          }
      ]
    


4. Update Menu Category (PUT Request)

Step 1: Create a New PUT Request

Step 2: Set the Request Body

  • Go to the Body tab.

  • Select raw and choose JSON from the dropdown.

  • Add the updated menu category data. For example, to change the category's name and description:

      {
          "name": "Salad"
      }
    

Step 3: Send the Request

  • Click Send to send the PUT request.

Step 4: Verify the Response

  • If the request is successful, the response will contain the updated menu category data, such as:

      {
          "created_at": "2024-11-27T06:39:50",
          "deleted_at": null,
          "menu_id": 3,
          "menu_uuid": "3bf0153e-2fcd-480b-8d2c-485f3d5a32e4",
          "name": "Salad",
          "updated_at": "2024-11-27T06:52:34"
      }
    


5. Delete Menu Category (DELETE Request)

Step 1: Create a New DELETE Request

Step 2: Send the Request

  • Click Send to delete the menu category.

Step 3: Verify the Response

RESULT:

1. Create Menu (POST Request)

Step 1: Open Postman and Create a New POST Request

Step 2: Set the Request Body

  • Go to the Body tab.

  • Select raw and choose JSON from the dropdown.

  • Add the menu data in JSON format. For example:

      {
           "name": "Classic Breakfast Plate",
           "description": "Two eggs any style, crispy bacon, hash browns, and toast.",
           "price": "250",
           "restaurant_id": "2",
           "category_id": "2",
           "availability": "true"
      }
    

Step 3: Send the Request

  • Click Send to create the menu item.

Step 4: Verify the Response

  • If the request is successful, you should receive a response with the created menu item’s data, including an ID. For example:

      {
          "availability": true,
          "category_id": 2,
          "created_at": "2024-11-27T07:04:35",
          "deleted_at": null,
          "description": "Two eggs any style, crispy bacon, hash browns, and toast.",
          "menu_id": 2,
          "menu_uuid": "66052195-a98a-4215-a0c4-1c3f0f04fd2f",
          "name": "Classic Breakfast Plate",
          "price": 250,
          "restaurant_id": 2,
          "updated_at": "2024-11-27T07:04:35"
      }
    


2. Get Menu by UID (GET Request)

Step 1: Create a New GET Request

Step 2: Send the Request

  • Click Send to get the details of the specified menu item.

Step 3: Verify the Response

  • If the menu item exists, the response should contain the menu item data, such as:


3. Get All Menus (GET Request)

Step 1: Create a New GET Request

Step 2: Send the Request

  • Click Send to retrieve a list of all menu items.

Step 3: Verify the Response

  • The response will contain a list of all menu items. For example:

      [
          {
              "availability": true,
              "category_id": 2,
              "created_at": "2024-11-27T07:04:35",
              "deleted_at": null,
              "description": "Two eggs any style, crispy bacon, hash browns, and toast.",
              "menu_id": 2,
              "menu_uuid": "66052195-a98a-4215-a0c4-1c3f0f04fd2f",
              "name": "Classic Breakfast Plate",
              "price": 250,
              "restaurant_id": 2,
              "updated_at": "2024-11-27T07:04:35"
          },
          {
              "availability": true,
              "category_id": 5,
              "created_at": "2024-11-27T07:19:38",
              "deleted_at": null,
              "description": "Classic pizza with fresh mozzarella, basil, and a tomato sauce base.",
              "menu_id": 3,
              "menu_uuid": "7d4094c7-6600-4766-a54b-76a4da319d7b",
              "name": "Margherita Pizza",
              "price": 600,
              "restaurant_id": 3,
              "updated_at": "2024-11-27T07:19:38"
          },
          {
              "availability": true,
              "category_id": 5,
              "created_at": "2024-11-27T07:19:51",
              "deleted_at": null,
              "description": "Topped with pepperoni slices and mozzarella cheese, baked to perfection.",
              "menu_id": 4,
              "menu_uuid": "c1fd78b2-77df-4760-8fe0-8d9e2cde7445",
              "name": "Pepperoni Pizza",
              "price": 600,
              "restaurant_id": 3,
              "updated_at": "2024-11-27T07:19:51"
          },
          {
              "availability": true,
              "category_id": 5,
              "created_at": "2024-11-27T07:20:00",
              "deleted_at": null,
              "description": "Classic Italian dessert made with layers of coffee-soaked sponge cake and mascarpone cream.",
              "menu_id": 5,
              "menu_uuid": "ae58a331-3c42-4782-be74-a8a4ea83d7f3",
              "name": "Tiramisu",
              "price": 459,
              "restaurant_id": 3,
              "updated_at": "2024-11-27T07:20:00"
          }
      ]
    


4. Update Menu (PUT Request)

Step 1: Create a New PUT Request

Step 2: Set the Request Body

  • Go to the Body tab.

  • Select raw and choose JSON from the dropdown.

  • Add the updated menu item data. For example, to change the menu item’s price and description:

      {
              "name": "update",
              "description": "update.",
              "price": "500",
              "restaurant_id": "3",
              "category_id": "4",
              "availability": "false"
      }
    

Step 3: Send the Request

  • Click Send to send the PUT request.

Step 4: Verify the Response

  • If the request is successful, the response will contain the updated menu item data, such as:


5. Delete Menu (DELETE Request)

Step 1: Create a New DELETE Request

Step 2: Send the Request

  • Click Send to delete the menu item.

Step 3: Verify the Response

RESULT:

https://github.com/JovRoncal/keriderya-api