-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
61 additions
and
116 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from flask import Flask | ||
from flask_sqlalchemy import SQLAlchemy | ||
from flask_cors import CORS | ||
from flask_migrate import Migrate | ||
import os | ||
|
||
|
||
|
||
|
||
# init | ||
app = Flask(__name__) | ||
CORS(app) | ||
|
||
|
||
# Database configuration | ||
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('POSTGRES_URL') | ||
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False | ||
|
||
|
||
#instance of db | ||
db = SQLAlchemy(app) | ||
migrate = Migrate(app, db) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,47 @@ | ||
from flask import request, jsonify | ||
from . import app, db | ||
from .models import User | ||
from flask import request, jsonify | ||
from config import app, db | ||
from models import User | ||
import traceback | ||
|
||
|
||
|
||
@app.route("/api/welcome") | ||
def hello_world(): | ||
return jsonify({"message": "Welcome to the API"}), 200 | ||
|
||
@app.route('/api/users', methods=['GET']) | ||
def get_users(): | ||
try: | ||
users = User.query.all() | ||
return jsonify([user.to_json() for user in users]), 200 | ||
except Exception as e: | ||
app.logger.error(f"Error fetching users: {str(e)}") | ||
return jsonify({"error": "An error occurred while fetching users"}), 500 | ||
users = User.query.all() | ||
return jsonify([user.to_json() for user in users]), 200 | ||
|
||
@app.route('/api/users/<int:user_id>', methods=['GET']) | ||
def get_user(user_id): | ||
try: | ||
user = User.query.get_or_404(user_id) | ||
return jsonify(user.to_json()), 200 | ||
except Exception as e: | ||
app.logger.error(f"Error fetching user {user_id}: {str(e)}") | ||
return jsonify({"error": f"An error occurred while fetching user {user_id}"}), 500 | ||
user = User.query.get_or_404(user_id) | ||
return jsonify(user.to_json()), 200 | ||
|
||
@app.route('/api/users/<int:user_id>', methods=['PATCH']) | ||
def update_user(user_id): | ||
try: | ||
data = request.json | ||
user = User.query.get_or_404(user_id) | ||
user.first_name = data.get('first_name', user.first_name) | ||
user.last_name = data.get('last_name', user.last_name) | ||
user.email = data.get('email', user.email) | ||
user.is_active = data.get('is_active', user.is_active) | ||
user.is_admin = data.get('is_admin', user.is_admin) | ||
db.session.commit() | ||
return jsonify(user.to_json()), 200 | ||
except Exception as e: | ||
db.session.rollback() | ||
app.logger.error(f"Error updating user {user_id}: {str(e)}") | ||
return jsonify({"error": f"An error occurred while updating user {user_id}"}), 500 | ||
|
||
data = request.json | ||
user = User.query.get_or_404(user_id) | ||
user.first_name = data['first_name'] | ||
user.last_name = data['last_name'] | ||
user.email = data['email'] | ||
user.is_active = data['is_active'] | ||
user.is_admin = data['is_admin'] | ||
db.session.commit() | ||
return jsonify(user.to_json()), 200 | ||
@app.route('/api/users/<int:user_id>', methods=['DELETE']) | ||
def delete_user(user_id): | ||
try: | ||
user = User.query.get_or_404(user_id) | ||
db.session.delete(user) | ||
db.session.commit() | ||
return jsonify({"message": "User deleted"}), 200 | ||
except Exception as e: | ||
db.session.rollback() | ||
app.logger.error(f"Error deleting user {user_id}: {str(e)}") | ||
return jsonify({"error": f"An error occurred while deleting user {user_id}"}), 500 | ||
|
||
# Error handling for 404 and 500 errors | ||
@app.errorhandler(404) | ||
def not_found_error(error): | ||
return jsonify({"error": "Not found"}), 404 | ||
user = User.query.get_or_404(user_id) | ||
db.session.delete(user) | ||
db.session.commit() | ||
return jsonify({"message": "User deleted"}), 200 | ||
|
||
@app.errorhandler(Exception) | ||
def handle_exception(e): | ||
# Log the full traceback | ||
app.logger.error(f"Unhandled Exception: {str(e)}\n{traceback.format_exc()}") | ||
return jsonify(error=str(e)), 500 | ||
|
||
@app.errorhandler(500) | ||
def internal_error(error): | ||
db.session.rollback() | ||
return jsonify({"error": "Internal server error"}), 500 | ||
if __name__ == "__main__": | ||
app.run(host="0.0.0.0", port=8000) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,11 @@ | ||
{ | ||
"builds": [ | ||
{ | ||
"src": "api/__init__.py", | ||
"use": "@vercel/python", | ||
"config": { | ||
"maxLambdaSize": "15mb", | ||
"runtime": "python3.9" | ||
} | ||
}, | ||
{ | ||
"src": "package.json", | ||
"use": "@vercel/next" | ||
} | ||
], | ||
"builds": [ | ||
{ "src": "api/main.py", "use": "@vercel/python", "config": { "runtime": "python3.12" } }, | ||
{ "src": "package.json", "use": "@vercel/next" } | ||
], | ||
"routes": [ | ||
{ "src": "/api/(.*)", "dest": "api/__init__.py" }, | ||
{ "src": "/api/(.*)", "dest": "api/main.py" }, | ||
{ "src": "/(.*)", "dest": "/$1" } | ||
], | ||
"env": { | ||
"FLASK_ENV": "production" | ||
} | ||
} | ||
] | ||
} | ||
|