-
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
1 parent
c47501b
commit d28afd5
Showing
6 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
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,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) |
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,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"] |
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,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}) |
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,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]) |
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,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") |
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,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()) |