Skip to content

Commit

Permalink
Commit to main
Browse files Browse the repository at this point in the history
  • Loading branch information
someone624 committed Nov 22, 2024
1 parent c47501b commit d28afd5
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 0 deletions.
10 changes: 10 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from flask import Flask
from src.controllers.route_mapper import register_routes

app = Flask(__name__)

# Register all routes
register_routes(app)

if __name__ == "__main__":
app.run(debug=True)
6 changes: 6 additions & 0 deletions src/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from .user_controller import UserController
from .product_controller import ProductController
from .auth_controller import AuthController

# Export all controllers as part of the package API
__all__ = ["UserController", "ProductController", "AuthController"]
18 changes: 18 additions & 0 deletions src/controllers/auth_controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from flask import Blueprint, request, jsonify
from ..models.user_model import User
from ..utils.auth_utils import generate_token

auth_blueprint = Blueprint("auth", __name__)

@auth_blueprint.route("/login", methods=["POST"])
def login():
data = request.json
email = data.get("email")
password = data.get("password")

user = User.authenticate(email, password)
if not user:
return jsonify({"error": "Invalid credentials"}), 401

token = generate_token(user.id)
return jsonify({"message": "Login successful", "token": token})
21 changes: 21 additions & 0 deletions src/controllers/product_controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from flask import Blueprint, request, jsonify
from ..models.product_model import Product

product_blueprint = Blueprint("product", __name__)

@product_blueprint.route("/products", methods=["POST"])
def create_product():
data = request.json
name = data.get("name")
price = data.get("price")

if not name or not isinstance(price, (int, float)):
return jsonify({"error": "Invalid product data"}), 400

product = Product.create(name=name, price=price)
return jsonify({"message": "Product created successfully", "product_id": product.id}), 201

@product_blueprint.route("/products", methods=["GET"])
def list_products():
products = Product.get_all()
return jsonify([product.to_dict() for product in products])
9 changes: 9 additions & 0 deletions src/controllers/route_mapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from flask import Flask
from .user_controller import user_blueprint
from .product_controller import product_blueprint
from .auth_controller import auth_blueprint

def register_routes(app: Flask):
app.register_blueprint(user_blueprint, url_prefix="/api/users")
app.register_blueprint(product_blueprint, url_prefix="/api/products")
app.register_blueprint(auth_blueprint, url_prefix="/api/auth")
24 changes: 24 additions & 0 deletions src/controllers/user_controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from flask import Blueprint, request, jsonify
from ..models.user_model import User
from ..utils.validation import validate_email

user_blueprint = Blueprint("user", __name__)

@user_blueprint.route("/register", methods=["POST"])
def register_user():
data = request.json
email = data.get("email")
password = data.get("password")

if not validate_email(email):
return jsonify({"error": "Invalid email address"}), 400

user = User.create(email=email, password=password)
return jsonify({"message": "User registered successfully", "user_id": user.id}), 201

@user_blueprint.route("/users/<int:user_id>", methods=["GET"])
def get_user(user_id):
user = User.get_by_id(user_id)
if not user:
return jsonify({"error": "User not found"}), 404
return jsonify(user.to_dict())

0 comments on commit d28afd5

Please sign in to comment.