Skip to content

Commit 24c09b5

Browse files
committed
Refactor Planventure API to integrate SQLAlchemy for database management, add user and trip models, and implement database initialization script.
1 parent ad90069 commit 24c09b5

8 files changed

Lines changed: 97 additions & 10 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.vscode
22
# Bruno adds a dir to your vscode workspace
3-
Planventure
3+
Planventure
4+
venv/

planventure-api/app.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,37 @@
11
from flask import Flask, jsonify
22
from flask_cors import CORS
3+
from flask_sqlalchemy import SQLAlchemy
4+
from os import environ
5+
from dotenv import load_dotenv
36

4-
app = Flask(__name__)
5-
CORS(app)
7+
# Load environment variables
8+
load_dotenv()
69

7-
@app.route('/')
8-
def home():
9-
return jsonify({"message": "Welcome to PlanVenture API"})
10+
# Initialize SQLAlchemy
11+
db = SQLAlchemy()
1012

11-
@app.route('/health')
12-
def health_check():
13-
return jsonify({"status": "healthy"})
13+
def create_app():
14+
app = Flask(__name__)
15+
CORS(app)
16+
17+
# Database configuration
18+
app.config['SQLALCHEMY_DATABASE_URI'] = environ.get('DATABASE_URL', 'sqlite:///planventure.db')
19+
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
20+
21+
# Initialize extensions
22+
db.init_app(app)
23+
24+
# Register routes
25+
@app.route('/')
26+
def home():
27+
return jsonify({"message": "Welcome to PlanVenture API"})
28+
29+
@app.route('/health')
30+
def health_check():
31+
return jsonify({"status": "healthy"})
32+
33+
return app
1434

1535
if __name__ == '__main__':
36+
app = create_app()
1637
app.run(debug=True)

planventure-api/init_db.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from app import create_app, db
2+
from models import User
3+
4+
def init_db():
5+
app = create_app()
6+
with app.app_context():
7+
# Create all database tables
8+
db.create_all()
9+
print("Database tables created successfully!")
10+
11+
if __name__ == '__main__':
12+
init_db()
16 KB
Binary file not shown.

planventure-api/models/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from .user import User
2+
from .trip import Trip
3+
4+
__all__ = ['User', 'Trip']

planventure-api/models/trip.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from datetime import datetime, timezone
2+
from app import db
3+
4+
class Trip(db.Model):
5+
__tablename__ = 'trips'
6+
7+
id = db.Column(db.Integer, primary_key=True)
8+
user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
9+
destination = db.Column(db.String(200), nullable=False)
10+
start_date = db.Column(db.DateTime, nullable=False)
11+
end_date = db.Column(db.DateTime, nullable=False)
12+
latitude = db.Column(db.Float)
13+
longitude = db.Column(db.Float)
14+
itinerary = db.Column(db.JSON)
15+
created_at = db.Column(db.DateTime, default=lambda: datetime.now(timezone.utc))
16+
updated_at = db.Column(db.DateTime, default=lambda: datetime.now(timezone.utc), onupdate=lambda: datetime.now(timezone.utc))
17+
18+
# Relationship
19+
user = db.relationship('User', back_populates='trips')
20+
21+
def __repr__(self):
22+
return f'<Trip {self.destination} ({self.start_date} - {self.end_date})>'

planventure-api/models/user.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from datetime import datetime, timezone
2+
from app import db
3+
4+
class User(db.Model):
5+
__tablename__ = 'users'
6+
7+
id = db.Column(db.Integer, primary_key=True)
8+
email = db.Column(db.String(120), unique=True, nullable=False)
9+
password_hash = db.Column(db.String(128), nullable=False)
10+
created_at = db.Column(db.DateTime, default=lambda: datetime.now(timezone.utc))
11+
updated_at = db.Column(db.DateTime, default=lambda: datetime.now(timezone.utc), onupdate=lambda: datetime.now(timezone.utc))
12+
13+
# Add relationship
14+
trips = db.relationship('Trip', back_populates='user', cascade='all, delete-orphan')
15+
16+
def __repr__(self):
17+
return f'<User {self.email}>'

planventure-api/requirements.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,14 @@
22
flask==2.3.3
33
flask-sqlalchemy==3.1.1
44
flask-cors==4.0.0
5-
python-dotenv==1.0.0
5+
python-dotenv==1.0.0
6+
7+
# Authentication
8+
flask-jwt-extended==4.5.3
9+
werkzeug==2.3.7
10+
11+
# Password hashing
12+
bcrypt==4.0.1
13+
14+
# Database migrations
15+
Flask-Migrate==4.0.5

0 commit comments

Comments
 (0)