-
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.
[]Updates📢] Added api route for js frontend and admin page
- Loading branch information
[esekyi]
committed
Aug 30, 2024
1 parent
6648918
commit fdfc8e0
Showing
6 changed files
with
128 additions
and
13 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,31 @@ | ||
from flask import Blueprint, jsonify, request | ||
from app.services.recipe_service import get_all_recipes | ||
|
||
api = Blueprint('api', __name__) | ||
|
||
def serialize_recipe(recipe): | ||
"""Convert a Recipe object to a dictionary.""" | ||
return { | ||
'id': recipe.id, | ||
'title': recipe.title, | ||
'description': recipe.description, | ||
'image_url': recipe.image_url, | ||
'created_at': recipe.created_at.isoformat(), # Convert datetime to string if needed | ||
'view_count': recipe.view_count | ||
} | ||
|
||
@api.route('/recipes', methods=['GET'], strict_slashes=False) | ||
def recipes(): | ||
page = int(request.args.get('page', 1)) | ||
per_page = int(request.args.get('per_page', 3)) | ||
try: | ||
paginated_recipes = get_all_recipes(page, per_page) | ||
return jsonify({ | ||
'recipes': paginated_recipes['items'], | ||
'total_items': paginated_recipes['total_items'], | ||
'total_pages': paginated_recipes['total_pages'], | ||
'current_page': paginated_recipes['current_page'] | ||
}) | ||
except ValueError as e: | ||
return jsonify({'error': str(e)}), 400 | ||
|
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
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,27 @@ | ||
from flask import Blueprint, request, url_for, render_template, jsonify, flash, redirect | ||
from app.services.category_service import CategoryService | ||
|
||
cat_bp = Blueprint('category_routes', __name__) | ||
|
||
|
||
@cat_bp.route('/categories', methods=['POST'], strict_slashes=False) | ||
def create_category(): | ||
data = request.json | ||
name = data.get('name') | ||
description = data.get('description') | ||
|
||
if not name: | ||
return jsonify({'error': 'Category name is required'}), 400 | ||
|
||
try: | ||
new_category = CategoryService.create_category(name, description) | ||
return jsonify({ | ||
'message': 'Category created successfully', | ||
'category': { | ||
'id': new_category.id, | ||
'name': new_category.name, | ||
'description': new_category.description | ||
} | ||
}), 201 | ||
except ValueError as e: | ||
return jsonify({'error': str(e)}), 400 |
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,7 @@ | ||
from flask import Blueprint, render_template, flash | ||
|
||
main = Blueprint('main', __name__) | ||
|
||
@main.route('/', methods=['GET']) | ||
def index(): | ||
return render_template('index.html') |
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
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