|
| 1 | +from typing import List, Optional, Union |
| 2 | + |
| 3 | +from sqlalchemy.orm import Session |
| 4 | + |
| 5 | +from breadbox.models.cms import CmsMenu, CmsMenuPost, CmsPost |
| 6 | +from breadbox.schemas.cms import Menu, PostIn, PostOut, PostSummaryOut |
| 7 | +from breadbox.schemas.custom_http_exception import ResourceNotFoundError |
| 8 | + |
| 9 | + |
| 10 | +def _build_menu_out(menu: CmsMenu) -> Menu: |
| 11 | + posts = [link.post.slug for link in menu.post_links] |
| 12 | + child_menus = [_build_menu_out(child) for child in menu.children] |
| 13 | + return Menu(slug=menu.slug, title=menu.title, child_menus=child_menus, posts=posts) |
| 14 | + |
| 15 | + |
| 16 | +def get_menu(db: Session) -> List[Menu]: |
| 17 | + roots = ( |
| 18 | + db.query(CmsMenu) |
| 19 | + .filter(CmsMenu.parent_id == None) |
| 20 | + .order_by(CmsMenu.order_index) |
| 21 | + .all() |
| 22 | + ) |
| 23 | + return [_build_menu_out(root) for root in roots] |
| 24 | + |
| 25 | + |
| 26 | +def _insert_menu_nodes( |
| 27 | + db: Session, items: List[Menu], parent_id: Optional[str], post_slug_to_id: dict, |
| 28 | +) -> None: |
| 29 | + for order_index, item in enumerate(items): |
| 30 | + node = CmsMenu( |
| 31 | + slug=item.slug, |
| 32 | + title=item.title, |
| 33 | + parent_id=parent_id, |
| 34 | + order_index=order_index, |
| 35 | + ) |
| 36 | + db.add(node) |
| 37 | + db.flush() # get node.id |
| 38 | + |
| 39 | + for post_order, slug in enumerate(item.posts): |
| 40 | + post_id = post_slug_to_id.get(slug) |
| 41 | + if post_id is not None: |
| 42 | + link = CmsMenuPost( |
| 43 | + menu_id=node.id, post_id=post_id, order_index=post_order |
| 44 | + ) |
| 45 | + db.add(link) |
| 46 | + |
| 47 | + _insert_menu_nodes(db, item.child_menus, node.id, post_slug_to_id) |
| 48 | + |
| 49 | + |
| 50 | +def set_menu(db: Session, menu_data: List[Menu]) -> List[Menu]: |
| 51 | + # Delete all existing menu rows (cascade handles children and post_links) |
| 52 | + db.query(CmsMenu).filter(CmsMenu.parent_id == None).delete( |
| 53 | + synchronize_session=False |
| 54 | + ) |
| 55 | + |
| 56 | + # Build slug→id map for posts |
| 57 | + posts = db.query(CmsPost).all() |
| 58 | + post_slug_to_id = {p.slug: p.id for p in posts} |
| 59 | + |
| 60 | + _insert_menu_nodes(db, menu_data, None, post_slug_to_id) |
| 61 | + db.flush() |
| 62 | + |
| 63 | + return get_menu(db) |
| 64 | + |
| 65 | + |
| 66 | +def get_posts( |
| 67 | + db: Session, include_content: bool |
| 68 | +) -> List[Union[PostOut, PostSummaryOut]]: |
| 69 | + posts = db.query(CmsPost).all() |
| 70 | + if include_content: |
| 71 | + return [PostOut.model_validate(p) for p in posts] |
| 72 | + else: |
| 73 | + return [PostSummaryOut.model_validate(p) for p in posts] |
| 74 | + |
| 75 | + |
| 76 | +def get_post(db: Session, post_id: str) -> PostOut: |
| 77 | + post = db.query(CmsPost).filter(CmsPost.id == post_id).first() |
| 78 | + if post is None: |
| 79 | + raise ResourceNotFoundError(f"Post '{post_id}' not found") |
| 80 | + return PostOut.model_validate(post) |
| 81 | + |
| 82 | + |
| 83 | +def upsert_post(db: Session, post_id: str, data: PostIn) -> PostOut: |
| 84 | + post = db.query(CmsPost).filter(CmsPost.id == post_id).first() |
| 85 | + if post is None: |
| 86 | + post = CmsPost( |
| 87 | + id=post_id, |
| 88 | + slug=data.slug, |
| 89 | + title=data.title, |
| 90 | + content=data.content, |
| 91 | + content_hash=data.content_hash, |
| 92 | + ) |
| 93 | + db.add(post) |
| 94 | + else: |
| 95 | + post.slug = data.slug |
| 96 | + post.title = data.title |
| 97 | + post.content = data.content |
| 98 | + post.content_hash = data.content_hash |
| 99 | + |
| 100 | + db.flush() |
| 101 | + db.refresh(post) |
| 102 | + return PostOut.model_validate(post) |
| 103 | + |
| 104 | + |
| 105 | +def delete_post(db: Session, post_id: str) -> None: |
| 106 | + post = db.query(CmsPost).filter(CmsPost.id == post_id).first() |
| 107 | + if post is None: |
| 108 | + raise ResourceNotFoundError(f"Post '{post_id}' not found") |
| 109 | + db.delete(post) |
| 110 | + db.flush() |
0 commit comments