From d28afd554bcc242af851a5145130e3aee6f627fb Mon Sep 17 00:00:00 2001 From: someone624 Date: Fri, 22 Nov 2024 14:37:33 +0100 Subject: [PATCH] Commit to main --- app.py | 10 ++++++++++ src/controllers/__init__.py | 6 ++++++ src/controllers/auth_controller.py | 18 ++++++++++++++++++ src/controllers/product_controller.py | 21 +++++++++++++++++++++ src/controllers/route_mapper.py | 9 +++++++++ src/controllers/user_controller.py | 24 ++++++++++++++++++++++++ 6 files changed, 88 insertions(+) create mode 100644 app.py create mode 100644 src/controllers/__init__.py create mode 100644 src/controllers/auth_controller.py create mode 100644 src/controllers/product_controller.py create mode 100644 src/controllers/route_mapper.py create mode 100644 src/controllers/user_controller.py diff --git a/app.py b/app.py new file mode 100644 index 0000000..1226781 --- /dev/null +++ b/app.py @@ -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) \ No newline at end of file diff --git a/src/controllers/__init__.py b/src/controllers/__init__.py new file mode 100644 index 0000000..eae2b33 --- /dev/null +++ b/src/controllers/__init__.py @@ -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"] \ No newline at end of file diff --git a/src/controllers/auth_controller.py b/src/controllers/auth_controller.py new file mode 100644 index 0000000..3384f06 --- /dev/null +++ b/src/controllers/auth_controller.py @@ -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}) \ No newline at end of file diff --git a/src/controllers/product_controller.py b/src/controllers/product_controller.py new file mode 100644 index 0000000..6c84b3b --- /dev/null +++ b/src/controllers/product_controller.py @@ -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]) \ No newline at end of file diff --git a/src/controllers/route_mapper.py b/src/controllers/route_mapper.py new file mode 100644 index 0000000..7aa8dbc --- /dev/null +++ b/src/controllers/route_mapper.py @@ -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") \ No newline at end of file diff --git a/src/controllers/user_controller.py b/src/controllers/user_controller.py new file mode 100644 index 0000000..6c4607e --- /dev/null +++ b/src/controllers/user_controller.py @@ -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/", 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()) \ No newline at end of file