|  | 
|  | 1 | +from flask import Flask, jsonify, request | 
|  | 2 | +from flask_sqlalchemy import SQLAlchemy | 
|  | 3 | +from dotenv import load_dotenv | 
|  | 4 | +import os | 
|  | 5 | + | 
|  | 6 | +# Load environment variables from .env file | 
|  | 7 | +load_dotenv() | 
|  | 8 | + | 
|  | 9 | +app = Flask(__name__) | 
|  | 10 | +app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv('DATABASE_URL') | 
|  | 11 | +app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False | 
|  | 12 | + | 
|  | 13 | +db = SQLAlchemy(app) | 
|  | 14 | + | 
|  | 15 | +class User(db.Model): | 
|  | 16 | +    __tablename__ = 'users'  | 
|  | 17 | +    id = db.Column(db.Integer, primary_key=True) | 
|  | 18 | +    name = db.Column(db.String(80), nullable=False) | 
|  | 19 | + | 
|  | 20 | +    def __init__(self, name): | 
|  | 21 | +        self.name = name | 
|  | 22 | + | 
|  | 23 | +# Create the database tables | 
|  | 24 | +@app.before_request | 
|  | 25 | +def create_tables(): | 
|  | 26 | +    db.create_all() | 
|  | 27 | + | 
|  | 28 | +# Home route | 
|  | 29 | +@app.route('/', methods=['GET']) | 
|  | 30 | +def home(): | 
|  | 31 | +    return jsonify({"message": "Welcome to the User Management API!"}), 200 | 
|  | 32 | + | 
|  | 33 | +# GET all users | 
|  | 34 | +@app.route('/users', methods=['GET']) | 
|  | 35 | +def get_users(): | 
|  | 36 | +    users = User.query.all() | 
|  | 37 | +    return jsonify([{'id': user.id, 'name': user.name} for user in users]) | 
|  | 38 | + | 
|  | 39 | +# POST a new user | 
|  | 40 | +@app.route('/users', methods=['POST']) | 
|  | 41 | +def add_user(): | 
|  | 42 | +    name = request.json.get('name') | 
|  | 43 | +    if not name: | 
|  | 44 | +        return jsonify({"error": "Name is required."}), 400 | 
|  | 45 | +    user = User(name=name) | 
|  | 46 | +    db.session.add(user) | 
|  | 47 | +    db.session.commit() | 
|  | 48 | +    return jsonify({"message": f"User {name} added.", "id": user.id}), 201 | 
|  | 49 | + | 
|  | 50 | +# PUT to update a user | 
|  | 51 | +@app.route('/users/<int:id>', methods=['PUT']) | 
|  | 52 | +def update_user(id): | 
|  | 53 | +    user = User.query.get(id) | 
|  | 54 | +    if user is None: | 
|  | 55 | +        return jsonify({"error": "User not found."}), 404 | 
|  | 56 | +     | 
|  | 57 | +    name = request.json.get('name') | 
|  | 58 | +    if name: | 
|  | 59 | +        user.name = name | 
|  | 60 | +        db.session.commit() | 
|  | 61 | +        return jsonify({"message": f"User {id} updated."}) | 
|  | 62 | +     | 
|  | 63 | +    return jsonify({"error": "Name is required."}), 400 | 
|  | 64 | + | 
|  | 65 | +# DELETE a user | 
|  | 66 | +@app.route('/users/<int:id>', methods=['DELETE']) | 
|  | 67 | +def delete_user(id): | 
|  | 68 | +    user = User.query.get(id) | 
|  | 69 | +    if user is None: | 
|  | 70 | +        return jsonify({"error": "User not found."}), 404 | 
|  | 71 | +     | 
|  | 72 | +    db.session.delete(user) | 
|  | 73 | +    db.session.commit() | 
|  | 74 | +    return jsonify({"message": f"User {id} deleted."}) | 
|  | 75 | + | 
|  | 76 | +if __name__ == "__main__": | 
|  | 77 | +    app.run(host="0.0.0.0", port=5000) | 
0 commit comments