Final Project: Python Flask API

GitHub Repositories:

  1. Online Voting System

  2. Online Job Platform

    https://github.com/ReynierCarpio/online-job-platform.git

  3. E-commerce

    https://github.com/ReynierCarpio/e-commerce.git

  4. Travel Booking

    https://github.com/ReynierCarpio/travel-booking

  5. Restaurant Reservation

    https://github.com/ReynierCarpio/restaurant-reservation.git

  6. Online Quiz Platform

  7. Event Management System

  8. Blogging Platform

  9. Personal Finance Manager

  10. Recipe Sharing Platform

  11. Library Web App

    https://github.com/ReynierCarpio/library-web-app

Set Up the Project Environment

    •                mkdir python-flask-api-project
                     cd python-flask-api-project
      
  1. Create Subdirectories for Each Application:

     mkdir library-web-app online-voting-system online-job-platform e-commerce travel-booking restaurant-reservation online-quiz-platform event-management-system blogging-platform personal-finance-manager recipe-sharing-platform
    
  2. Set Up Virtual Environment for Each Application:

# For library-web-app directory
cd library-web-app
python -m venv venv
  • Activate virtual environment:

      venv\Scripts\activate
    
  1. Install Dependencies (Flask, Flask-SQLAlchemy, etc.):

    •           pip install Flask flask-mysqldb flask-cors flask-jwt-extended requests
      

Set Up MySQL Database (AIVEN):

  •           app.config['MYSQL_HOST'] = 'your_aiven_host'
              app.config['MYSQL_USER'] = 'your_aiven_user'
              app.config['MYSQL_PASSWORD'] = 'your_aiven_password'
              app.config['MYSQL_DB'] = 'your_aiven_db_name'
    

Create Database Models and Structure

  1. Create models.py in Each Subdirectory:

     # models.py (Library Web App)
     from flask_sqlalchemy import SQLAlchemy
     db = SQLAlchemy()
    
     class User(db.Model):
         user_id = db.Column(db.String(100), primary_key=True)
         username = db.Column(db.String(50), nullable=False)
         email = db.Column(db.String(100), nullable=False)
         password = db.Column(db.String(100), nullable=False)
         role = db.Column(db.String(50))
         created_at = db.Column(db.DateTime)
         updated_at = db.Column(db.DateTime)
    
     class Book(db.Model):
         book_id = db.Column(db.String(100), primary_key=True)
         title = db.Column(db.String(100))
         author = db.Column(db.String(100))
         genre = db.Column(db.String(50))
         publication_date = db.Column(db.Date)
         ISBN = db.Column(db.String(50))
         summary = db.Column(db.Text)
         created_at = db.Column(db.DateTime)
         updated_at = db.Column(db.DateTime)
    

    Repeat for Other Applications

Set Up Flask Routes (API Endpoints)

  1.      # routes.py (Library Web App)
         from flask import Flask, request, jsonify
         from models import db, User, Book
    
         app = Flask(__name__)
    
         @app.route('/users', methods=['GET'])
         def get_users():
             users = User.query.all()
             return jsonify([user.serialize() for user in users])
    
         @app.route('/user', methods=['POST'])
         def add_user():
             data = request.get_json()
             new_user = User(username=data['username'], email=data['email'], password=data['password'])
             db.session.add(new_user)
             db.session.commit()
             return jsonify(new_user.serialize()), 201
    
  2. Create Endpoints for CRUD Operations:

    • GET: Retrieve records (e.g., GET /users, GET /books).

    • POST: Create records (e.g., POST /user, POST /book).

    • PUT/PATCH: Update records (e.g., PUT /user/{id}, PATCH /book/{id}).

    • DELETE: Delete records (e.g., DELETE /user/{id}, DELETE /book/{id}).

  3. Define Serializers for Models:

    • Add methods to convert model instances to JSON format:
    class User(db.Model):
        # model fields...
        def serialize(self):
            return {
                'user_id': self.user_id,
                'username': self.username,
                'email': self.email,
                'role': self.role,
                'created_at': self.created_at,
                'updated_at': self.updated_at
            }

Repeat for Other Applications

Testing with Postman

  1. Create Postman Collections for Each Application:

  2. Define Subfolders for Each Collection:

  3. Test Each Endpoint:


online-job-platform:

POST:

GET:

PUT/PATCH:

DELETE:


restaurant-reservation:

POST:

GET:

PUT:


e-commerce:

POST:

GET

PUT/PATCH


library-web-app:

GET:

POST:

PUT/PATCH

DELETE:


travel-booking:

POST

GET


Set Up GitHub Repositories

Create a Separate GitHub Repository for Each Application

  • Initialize a Git repository inside each subdirectory:

      git init
      git add .
      git commit -m "Initial commit"
      git remote add origin <your-repository-url>
      git push -u origin master