-
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.
[Scalabilty] Implent pagination and category services
- Loading branch information
[esekyi]
committed
Aug 30, 2024
1 parent
fdfc8e0
commit b0fdea2
Showing
7 changed files
with
138 additions
and
34 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,35 @@ | ||
from app import db | ||
from app.models.category import Category | ||
from sqlalchemy import func | ||
|
||
class CategoryService: | ||
@staticmethod | ||
def create_category(name: str, description: str = None) -> Category: | ||
""" | ||
Create a new category. | ||
Args: | ||
name (str): The name of the category. | ||
description (str, optional): The description of the category. | ||
Returns: | ||
Category: The newly created category. | ||
Raises: | ||
ValueError: If a category with the given name already exists (case-insensitive).. | ||
""" | ||
existing_category = Category.query.filter(func.lower(Category.name) == func.lower(name)).first() | ||
|
||
if existing_category: | ||
raise ValueError(f"A category with the name '{name}' already exists.") | ||
|
||
new_category = Category(name=name, description=description) | ||
db.session.add(new_category) | ||
db.session.commit() | ||
|
||
return new_category | ||
|
||
@staticmethod | ||
def get_all_categories(): | ||
categories = Category.query.order_by(Category.name.asc()).all() | ||
return categories |
This file was deleted.
Oops, something went wrong.
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,34 @@ | ||
from math import ceil | ||
|
||
def paginate(items, page, per_page): | ||
"""Paginate a list of items. | ||
Args: | ||
items (list): The list of items to paginate. | ||
page (int): The current page number (1-indexed). | ||
per_page (int): The number of items per page. | ||
Returns: | ||
dict: A dictionary containing paginated items and pagination info. | ||
""" | ||
|
||
if page < 1: | ||
raise ValueError("Page number must be 1 or greater.") | ||
|
||
total_items = len(items) | ||
total_pages = ceil(total_items / per_page) | ||
|
||
if page > total_pages: | ||
raise ValueError("Page number exceeds total pages") | ||
|
||
start = (page - 1) * per_page | ||
end = start + per_page | ||
paginated_items = items[start:end] | ||
|
||
return { | ||
'items': paginated_items, | ||
'total_items': total_items, | ||
'total_pages': total_pages, | ||
'current_page': page, | ||
'per_page': per_page | ||
} |
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
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