8000 Initial commit · mr-st0rm/CafeMenuApp_RESTAPI@6cd7822 · GitHub
[go: up one dir, main page]

Skip to content

33 files changed

+911
-0
lines changed

.gitignore

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
share/python-wheels/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
MANIFEST
28+
29+
# PyInstaller
30+
# Usually these files are written by a python script from a template
31+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
32+
*.manifest
33+
*.spec
34+
35+
# Installer logs
36+
pip-log.txt
37+
pip-delete-this-directory.txt
38+
39+
# Unit test / coverage reports
40+
htmlcov/
41+
.tox/
42+
.nox/
43+
.coverage
44+
.coverage.*
45+
.cache
46+
nosetests.xml
47+
coverage.xml
48+
*.cover
49+
*.py,cover
50+
.hypothesis/
51+
.pytest_cache/
52+
cover/
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
*.log
60+
local_settings.py
61+
db.sqlite3
62+
db.sqlite3-journal
63+
64+
# Flask stuff:
65+
instance/
66+
.webassets-cache
67+
68+
# Scrapy stuff:
69+
.scrapy
70+
71+
# Sphinx documentation
72+
docs/_build/
73+
74+
# PyBuilder
75+
.pybuilder/
76+
target/
77+
78+
# Jupyter Notebook
79+
.ipynb_checkpoints
80+
81+
# IPython
82+
profile_default/
83+
ipython_config.py
84+
85+
# pyenv
86+
# For a library or package, you might want to ignore these files since the code is
87+
# intended to run in multiple environments; otherwise, check them in:
88+
# .python-version
89+
90+
# pipenv
91+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
93+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
94+
# install all needed dependencies.
95+
#Pipfile.lock
96+
97+
# poetry
98+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99+
# This is especially recommended for binary packages to ensure reproducibility, and is more
100+
# commonly ignored for libraries.
101+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102+
#poetry.lock
103+
104+
# pdm
105+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
106+
#pdm.lock
107+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
108+
# in version control.
109+
# https://pdm.fming.dev/#use-with-ide
110+
.pdm.toml
111+
112+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
113+
__pypackages__/
114+
115+
# Celery stuff
116+
celerybeat-schedule
117+
celerybeat.pid
118+
119+
# SageMath parsed files
120+
*.sage.py
121+
122+
# Environments
123+
.env
124+
.venv
125+
env/
126+
venv/
127+
ENV/
128+
env.bak/
129+
venv.bak/
130+
131+
# Spyder project settings
132+
.spyderproject
133+
.spyproject
134+
135+
# Rope project settings
136+
.ropeproject
137+
138+
# mkdocs documentation
139+
/site
140+
141+
# mypy
142+
.mypy_cache/
143+
.dmypy.json
144+
dmypy.json
145+
146+
# Pyre type checker
147+
.pyre/
148+
149+
# pytype static type analyzer
150+
.pytype/
151+
152+
# Cython debug symbols
153+
cython_debug/
154+
155+
# PyCharm
156+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
157+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
158+
# and can be added to the global gitignore or merged into this file. For a more nuclear
159+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
160+
.idea/

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Empty so far...

app/__init__.py

Whitespace-only changes.

app/api/__init__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from fastapi import FastAPI
2+
3+
from .events import event_handler
4+
from .v1.routes.menu_routes import menu_router
5+
from .v1.routes.submenu_router import submenu_router
6+
from .v1.routes.dish_routes import dish_router
7+
8+
9+
def register_api_routes(app: FastAPI):
10+
"""
11+
Register all routes
12+
13+
:param app: FastAPI obj
14+
:return: None
15+
"""
16+
app.include_router(event_handler)
17+
app.include_router(menu_router, prefix="/api/v1")
18+
app.include_router(submenu_router, prefix="/api/v1")
19+
app.include_router(dish_router, prefix="/api/v1")

app/api/events.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import logging
2+
3+
from fastapi import APIRouter
4+
5+
event_handler = APIRouter()
6+
7+
8+
@event_handler.on_event("startup")
9+
async def application_started():
10+
logging.info("App started.")
11+
12+
13+
@event_handler.on_event("shutdown")
14+
async def application_exit():
15+
logging.info("App shutdown.")

app/api/v1/__init__.py

Whitespace-only changes.

app/api/v1/routes/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

app/api/v1/routes/dish_routes.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import typing
2+
3+
from fastapi import APIRouter, Depends, HTTPException
4+
from fastapi.responses import JSONResponse
5+
6+
from app.database.repos.repo import Repo
7+
from app.database.session_schema import repo_stub
8+
from app.api.v1.schemas import request as req_model
9+
from app.api.v1.schemas import response as res_model
10+
11+
dish_router = APIRouter(prefix="/menus/{menu_id}/submenus/{submenu_id}/dishes")
12+
13+
14+
@dish_router.get("/", response_model=typing.List[res_model.Dish])
15+
async def get_dishes(submenu_id: int, repo: Repo = Depends(repo_stub)):
16+
submenu = await repo.submenu.submenu_info(submenu_id)
17+
18+
if submenu:
19+
return submenu.dishes
20+
21+
raise HTTPException(status_code=404, detail="submenu not found")
22+
23+
24+
@dish_router.get("/{dish_id}", response_model=res_model.Dish)
25+
async def get_dish_information(dish_id: int, repo: Repo = Depends(repo_stub)):
26+
dish = await repo.dish.dish_info(dish_id)
27+
28+
if dish:
29+
return dish
30+
31+
raise HTTPException(status_code=404, detail="dish not found")
32+
33+
34+
@dish_router.post("/", response_model=res_model.Dish, status_code=201)
35+
async def create_dish(submenu_id: int, dish: req_model.Dish, repo: Repo = Depends(repo_stub)):
36+
dish = await repo.dish.create_dish(submenu_id, dish.title, dish.description, dish.price)
37+
38+
if dish:
39+
return dish
40+
41+
raise HTTPException(status_code=404, detail="submenu not found")
42+
43+
44+
@dish_router.patch("/{dish_id}", response_model=res_model.Dish)
45+
async def update_dish(dish_id: int, dish: req_model.Dish, repo: Repo = Depends(repo_stub)):
46+
dish = await repo.dish.update_dish(dish_id, dish.title, dish.description, dish.price)
47+
48+
if dish:
49+
return dish
50+
51+
raise HTTPException(status_code=404, detail="dish not found")
52+
53+
54+
@dish_router.delete("/{dish_id}")
55+
async def delete_dish(dish_id: int, repo: Repo = Depends(repo_stub)):
56+
dish = await repo.dish.delete_dish(dish_id)
57+
58+
if dish:
59+
return JSONResponse(content={"status": dish, "message": "The dish has been deleted"})
60+
61+
raise HTTPException(status_code=404, detail="dish not found")

app/api/v1/routes/menu_routes.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import typing
2+
3+
from fastapi import APIRouter, Depends, HTTPException
4+
from fastapi.responses import JSONResponse
5+
6+
from app.database.repos.repo import Repo
7+
from app.database.session_schema import repo_stub
8+
from app.api.v1.schemas import request as req_model
9+
from app.api.v1.schemas import response as res_model
10+
from app.api.v1.utils import menu_utils
11+
12+
menu_router = APIRouter(prefix="/menus")
13+
14+
15+
@menu_router.get("/", response_model=typing.List[res_model.Menu])
16+
async def get_menus(repo: Repo = Depends(repo_stub)):
17+
all_menus = await repo.menu.get_all_menus()
18+
all_menus = menu_utils.calculate_menu_submenus_and_dishes_list(all_menus)
19+
20+
return all_menus
21+
22+
23+
@menu_router.post("/", response_model=res_model.Menu, status_code=201)
24+
async def create_menu(menu: req_model.Menu, repo: Repo = Depends(repo_stub)):
25+
menu = await repo.menu.create_menu(menu.title, menu.description)
26+
menu = menu_utils.calculate_menu_submenus_and_dishes(menu)
27+
28+
return menu
29+
30+
31+
@menu_router.get("/{menu_id}", response_model=res_model.Menu)
32+
async def get_menu_information(menu_id: int, repo: Repo = Depends(repo_stub)):
33+
menu = await repo.menu.menu_info(menu_id)
34+
35+
if menu:
36+
menu = menu_utils.calculate_menu_submenus_and_dishes(menu)
37+
return menu
38+
39+
raise HTTPException(status_code=404, detail="menu not found")
40+
41+
42+
@menu_router.patch("/{menu_id}", response_model=res_model.Menu)
43+
async def update_menu_information(menu_id: int, menu: req_model.Menu, repo: Repo = Depends(repo_stub)):
44+
menu = await repo.menu.update_menu(menu_id, menu.title, menu.description)
45+
46+
if menu:
47+
menu = menu_utils.calculate_menu_submenus_and_dishes(menu)
48+
return menu
49+
50+
raise HTTPException(status_code=404, detail="menu not found")
51+
52+
53+
@menu_router.delete("/{menu_id}")
54+
async def delete_menu(menu_id: int, repo: Repo = Depends(repo_stub)):
55+
menu = await repo.menu.delete_menu(menu_id)
56+
57+
if menu:
58+
return JSONResponse(content={"status": menu, "message": "The menu has been deleted"})
59+
60+
raise HTTPException(status_code=404, detail="menu not found")

app/api/v1/routes/submenu_router.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import typing
2+
3+
from fastapi import APIRouter, Depends, HTTPException
4+
from fastapi.responses import JSONResponse
5+
6+
from app.database.repos.repo import Repo
7+
from app.database.session_schema import repo_stub
8+
from app.api.v1.schemas import request as req_model
9+
from app.api.v1.schemas import response as res_model
10+
from app.api.v1.utils import submenu_utils
11+
12+
submenu_router = APIRouter(prefix="/menus/{menu_id}/submenus")
13+
14+
15+
@submenu_router.get("/", response_model=typing.List[res_model.SubMenu])
16+
async def get_all_submenus(menu_id: int, repo: Repo = Depends(repo_stub)):
17+
menu = await repo.menu.menu_info(menu_id)
18+
19+
if menu:
20+
submenus = submenu_utils.calculate_submenu_dishes_list(menu.sub_menus)
21+
return submenus
22+
23+
raise HTTPException(status_code=404, detail="menu not found")
24+
25+
26+
@submenu_router.get("/{submenu_id}", response_model=res_model.SubMenu)
27+
async def get_submenu_information(submenu_id: int, repo: Repo = Depends(repo_stub)):
28+
submenu = await repo.submenu.submenu_info(submenu_id)
29+
30+
if submenu:
31+
submenu = submenu_utils.calculate_submenu_dishes(submenu)
32+
return submenu
33+
34+
raise HTTPException(status_code=404, detail="submenu not found")
35+
36+
37+
@submenu_router.post("/", response_model=res_model.SubMenu, status_code=201)
38+
async def create_submenu(menu_id: int, submenu: req_model.Menu, repo: Repo = Depends(repo_stub)):
39+
submenu = await repo.submenu.create_submenu(menu_id, submenu.title, submenu.description)
40+
submenu = submenu_utils.calculate_submenu_dishes(submenu)
41+
42+
return submenu
43+
44+
45+
@submenu_router.patch("/{submenu_id}", response_model=res_model.SubMenu)
46+
async def update_submenu_information(submenu_id: int, submenu: req_model.Menu, repo: Repo = Depends(repo_stub)):
47+
submenu = await repo.submenu.update_submenu(submenu_id, submenu.title, submenu.description)
48+
49+
if submenu:
50+
submenu = submenu_utils.calculate_submenu_dishes(submenu)
51+
return submenu
52+
53+
raise HTTPException(status_code=404, detail="submenu not found")
54+
55+
56+
@submenu_router.delete("/{submenu_id}")
57+
async def delete_submenu(submenu_id: int, repo: Repo = Depends(repo_stub)):
58+
menu = await repo.submenu.delete_submenu(submenu_id)
59+
60+
if menu:
61+
return JSONResponse(content={"status": menu, "message": "The submenu has been deleted"})
62+
63+
raise HTTPException(status_code=404, detail="menu not found")

0 commit comments

Comments
 (0)
0