Files
PVHBackend/api/categories.py

33 lines
1.1 KiB
Python

from flask import jsonify
from db.handlers.catigory_handler import get_all_categories_with_attributes, get_category_by_id
from flask import Blueprint
# Создаем blueprint для API
api_category = Blueprint('api_category', __name__, url_prefix='/categories')
@api_category.route('/', methods=['GET'], strict_slashes=False)
def get_categories():
"""
Получение всех категорий с атрибутами
GET /api/categories
"""
try:
categories = get_all_categories_with_attributes()
return jsonify(categories), 200
except Exception as e:
return jsonify({"error": str(e)}), 500
@api_category.route('/<int:category_id>', methods=['GET'], strict_slashes=False)
def get_category(category_id):
"""
Получение категории по ID
GET /api/categories/{id}
"""
try:
category = get_category_by_id(category_id)
if category:
return jsonify(category), 200
return jsonify({"error": "Category not found"}), 404
except Exception as e:
return jsonify({"error": str(e)}), 500