Activity #50: Keriderya API

Step 1: Fork the repository github.com/thirdygayares/keriderya-api

This creates a copy of the repository in your GitHub account.

Step 2: Clone your repository

Navigate to your GitHub profile and open the newly forked repository.

Click on the green "Code" button and copy the HTTPS link.

Open your terminal and run the following command to clone the repository:

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

(Replace your-username with your actual GitHub username.)

Change into the directory:

cd keriderya-api

Step 3: Set up the Virtual Environment

A. Create and Activate a Virtual Environment

Create a virtual environment in your project directory by running:

python -m venv myvenv
myvenv\Scripts\activate

B. Install Dependencies

Install the required dependencies by running:

pip install -r requirements.txt

This will install all the packages listed in the requirements.txt file.

C. Set up the .env File

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

Add the following content to the .env file (replace {password}, {host}, {port}, and {databasename1} with your actual database credentials):

  FLASK_ENV=development
  DATABASE_URL=mysql://avnadmin:{password}@{host}:{port}/{databasename1}
  SECRET_KEY=your_secret_key

The SECRET_KEY can be any random string that Flask will use to sign cookies and sessions.

D. Add Certificates

If required by the application (e.g., for secure connections), copy the SSL certificates to the app/certs folder. Ensure the necessary certificates (cert.pem, key.pem, etc.) are in place.

E. Run Database Migrations

Run the following command to upgrade the database:

flask db upgrade

This will apply any pending migrations to the database.

F. Run the Application

Finally, run the application by executing:

python run.py

Step 4: Create a Database on Aiven Dashboard

Go to the Aiven dashboard.

Create a new PostgreSQL or MySQL database.

Make sure to replace the database connection details (like password, host, port, and databasename1) in your .env file with the values provided by Aiven.

Step 5: Explain the Code and Show Postman Requests

Code Explanation

  • Flask Application: The project is a Flask-based application that manages restaurant menu items and users. It uses a MySQL database for storing user and menu information.

  • Endpoints:

    • POST /users: Creates a new user in the database.

    • GET /users/<id>: Retrieves a user by their ID.

Here is an overview of the most important files:

  • run.py: This is the entry point of the Flask application. It initializes the app and starts the server.

  • app/models.py: Contains the models for the database, such as the User model for user-related data.

  • app/routes.py: Contains the Flask routes that define the application’s API endpoints (e.g., creating and fetching users).

API Request

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": "Fast Food"
}

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.

2. Get All Restaurant Categories (GET Request)

3. Get Restaurant Category by ID (GET Request)

Step 1: Create a New GET Request

4. Update Restaurant Category

5. Delete Restaurant Category

RonaDecio/keriderya-api