FastAPI-fastkit 1.0.0__py3-none-any.whl → 1.0.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (128) hide show
  1. fastapi_fastkit/__init__.py +10 -1
  2. fastapi_fastkit/backend/main.py +482 -482
  3. fastapi_fastkit/backend/transducer.py +106 -106
  4. fastapi_fastkit/cli.py +664 -631
  5. fastapi_fastkit/core/settings.py +155 -155
  6. fastapi_fastkit/fastapi_project_template/README.md +90 -90
  7. fastapi_fastkit/fastapi_project_template/fastapi-async-crud/.env-tpl +1 -1
  8. fastapi_fastkit/fastapi_project_template/fastapi-async-crud/.gitignore-tpl +30 -30
  9. fastapi_fastkit/fastapi_project_template/fastapi-async-crud/README.md-tpl +107 -107
  10. fastapi_fastkit/fastapi_project_template/fastapi-async-crud/requirements.txt-tpl +36 -36
  11. fastapi_fastkit/fastapi_project_template/fastapi-async-crud/scripts/format.sh-tpl +5 -5
  12. fastapi_fastkit/fastapi_project_template/fastapi-async-crud/scripts/lint.sh-tpl +5 -5
  13. fastapi_fastkit/fastapi_project_template/fastapi-async-crud/scripts/run-server.sh-tpl +8 -8
  14. fastapi_fastkit/fastapi_project_template/fastapi-async-crud/scripts/test.sh-tpl +6 -6
  15. fastapi_fastkit/fastapi_project_template/fastapi-async-crud/setup.cfg-tpl +14 -14
  16. fastapi_fastkit/fastapi_project_template/fastapi-async-crud/setup.py-tpl +25 -25
  17. fastapi_fastkit/fastapi_project_template/fastapi-async-crud/src/api/api.py-tpl +9 -9
  18. fastapi_fastkit/fastapi_project_template/fastapi-async-crud/src/api/routes/items.py-tpl +57 -57
  19. fastapi_fastkit/fastapi_project_template/fastapi-async-crud/src/core/config.py-tpl +64 -64
  20. fastapi_fastkit/fastapi_project_template/fastapi-async-crud/src/crud/items.py-tpl +21 -21
  21. fastapi_fastkit/fastapi_project_template/fastapi-async-crud/src/main.py-tpl +26 -26
  22. fastapi_fastkit/fastapi_project_template/fastapi-async-crud/src/mocks/mock_items.json-tpl +8 -8
  23. fastapi_fastkit/fastapi_project_template/fastapi-async-crud/src/schemas/items.py-tpl +22 -22
  24. fastapi_fastkit/fastapi_project_template/fastapi-async-crud/tests/conftest.py-tpl +42 -42
  25. fastapi_fastkit/fastapi_project_template/fastapi-async-crud/tests/test_items.py-tpl +48 -48
  26. fastapi_fastkit/fastapi_project_template/fastapi-custom-response/.env-tpl +1 -1
  27. fastapi_fastkit/fastapi_project_template/fastapi-custom-response/.gitignore-tpl +191 -191
  28. fastapi_fastkit/fastapi_project_template/fastapi-custom-response/README.md-tpl +156 -156
  29. fastapi_fastkit/fastapi_project_template/fastapi-custom-response/requirements.txt-tpl +36 -36
  30. fastapi_fastkit/fastapi_project_template/fastapi-custom-response/scripts/format.sh-tpl +5 -5
  31. fastapi_fastkit/fastapi_project_template/fastapi-custom-response/scripts/lint.sh-tpl +5 -5
  32. fastapi_fastkit/fastapi_project_template/fastapi-custom-response/scripts/run-server.sh-tpl +8 -8
  33. fastapi_fastkit/fastapi_project_template/fastapi-custom-response/scripts/test.sh-tpl +6 -6
  34. fastapi_fastkit/fastapi_project_template/fastapi-custom-response/setup.cfg-tpl +12 -12
  35. fastapi_fastkit/fastapi_project_template/fastapi-custom-response/setup.py-tpl +27 -27
  36. fastapi_fastkit/fastapi_project_template/fastapi-custom-response/src/.DS_Store +0 -0
  37. fastapi_fastkit/fastapi_project_template/fastapi-custom-response/src/api/api.py-tpl +9 -9
  38. fastapi_fastkit/fastapi_project_template/fastapi-custom-response/src/api/routes/items.py-tpl +172 -172
  39. fastapi_fastkit/fastapi_project_template/fastapi-custom-response/src/core/config.py-tpl +64 -64
  40. fastapi_fastkit/fastapi_project_template/fastapi-custom-response/src/crud/items.py-tpl +21 -21
  41. fastapi_fastkit/fastapi_project_template/fastapi-custom-response/src/helper/exceptions.py-tpl +103 -103
  42. fastapi_fastkit/fastapi_project_template/fastapi-custom-response/src/helper/pagination.py-tpl +72 -72
  43. fastapi_fastkit/fastapi_project_template/fastapi-custom-response/src/main.py-tpl +39 -39
  44. fastapi_fastkit/fastapi_project_template/fastapi-custom-response/src/mocks/mock_items.json-tpl +8 -8
  45. fastapi_fastkit/fastapi_project_template/fastapi-custom-response/src/schemas/base.py-tpl +49 -49
  46. fastapi_fastkit/fastapi_project_template/fastapi-custom-response/src/schemas/items.py-tpl +22 -22
  47. fastapi_fastkit/fastapi_project_template/fastapi-custom-response/src/utils/documents.py-tpl +20 -20
  48. fastapi_fastkit/fastapi_project_template/fastapi-custom-response/tests/conftest.py-tpl +42 -42
  49. fastapi_fastkit/fastapi_project_template/fastapi-custom-response/tests/test_items.py-tpl +61 -61
  50. fastapi_fastkit/fastapi_project_template/fastapi-default/README.md-tpl +101 -101
  51. fastapi_fastkit/fastapi_project_template/fastapi-default/scripts/run-server.sh-tpl +8 -8
  52. fastapi_fastkit/fastapi_project_template/fastapi-default/setup.py-tpl +22 -22
  53. fastapi_fastkit/fastapi_project_template/fastapi-default/src/mocks/mock_items.json-tpl +8 -8
  54. fastapi_fastkit/fastapi_project_template/fastapi-default/tests/conftest.py-tpl +30 -30
  55. fastapi_fastkit/fastapi_project_template/fastapi-default/tests/test_items.py-tpl +44 -44
  56. fastapi_fastkit/fastapi_project_template/fastapi-dockerized/.env-tpl +1 -1
  57. fastapi_fastkit/fastapi_project_template/fastapi-dockerized/.gitignore-tpl +30 -30
  58. fastapi_fastkit/fastapi_project_template/fastapi-dockerized/Dockerfile-tpl +23 -23
  59. fastapi_fastkit/fastapi_project_template/fastapi-dockerized/README.md-tpl +119 -119
  60. fastapi_fastkit/fastapi_project_template/fastapi-dockerized/requirements.txt-tpl +36 -36
  61. fastapi_fastkit/fastapi_project_template/fastapi-dockerized/scripts/format.sh-tpl +5 -5
  62. fastapi_fastkit/fastapi_project_template/fastapi-dockerized/scripts/lint.sh-tpl +5 -5
  63. fastapi_fastkit/fastapi_project_template/fastapi-dockerized/scripts/run-server.sh-tpl +8 -8
  64. fastapi_fastkit/fastapi_project_template/fastapi-dockerized/scripts/test.sh-tpl +6 -6
  65. fastapi_fastkit/fastapi_project_template/fastapi-dockerized/setup.cfg-tpl +13 -13
  66. fastapi_fastkit/fastapi_project_template/fastapi-dockerized/setup.py-tpl +22 -22
  67. fastapi_fastkit/fastapi_project_template/fastapi-dockerized/src/api/api.py-tpl +6 -6
  68. fastapi_fastkit/fastapi_project_template/fastapi-dockerized/src/api/routes/items.py-tpl +54 -54
  69. fastapi_fastkit/fastapi_project_template/fastapi-dockerized/src/core/config.py-tpl +64 -64
  70. fastapi_fastkit/fastapi_project_template/fastapi-dockerized/src/crud/items.py-tpl +15 -15
  71. fastapi_fastkit/fastapi_project_template/fastapi-dockerized/src/main.py-tpl +26 -26
  72. fastapi_fastkit/fastapi_project_template/fastapi-dockerized/src/mocks/mock_items.json-tpl +8 -8
  73. fastapi_fastkit/fastapi_project_template/fastapi-dockerized/src/schemas/items.py-tpl +19 -19
  74. fastapi_fastkit/fastapi_project_template/fastapi-dockerized/tests/conftest.py-tpl +30 -30
  75. fastapi_fastkit/fastapi_project_template/fastapi-dockerized/tests/test_items.py-tpl +44 -44
  76. fastapi_fastkit/fastapi_project_template/fastapi-empty/.env-tpl +1 -1
  77. fastapi_fastkit/fastapi_project_template/fastapi-empty/setup.py-tpl +22 -22
  78. fastapi_fastkit/fastapi_project_template/fastapi-empty/src/core/config.py-tpl +75 -75
  79. fastapi_fastkit/fastapi_project_template/fastapi-empty/src/main.py-tpl +36 -36
  80. fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/.env-tpl +9 -9
  81. fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/.gitignore-tpl +30 -30
  82. fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/Dockerfile-tpl +23 -23
  83. fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/README.md-tpl +144 -144
  84. fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/alembic.ini-tpl +119 -119
  85. fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/docker-compose.yml-tpl +44 -44
  86. fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/requirements.txt-tpl +41 -41
  87. fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/scripts/format.sh-tpl +5 -5
  88. fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/scripts/lint.sh-tpl +5 -5
  89. fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/scripts/pre-start.sh-tpl +16 -16
  90. fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/scripts/run-server.sh-tpl +8 -8
  91. fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/scripts/test.sh-tpl +10 -10
  92. fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/setup.cfg-tpl +13 -13
  93. fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/setup.py-tpl +25 -25
  94. fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/alembic/README-tpl +1 -1
  95. fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/alembic/env.py-tpl +85 -85
  96. fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/alembic/script.py.mako-tpl +26 -26
  97. fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/alembic/versions/bedcdc35b64a_first_alembic.py-tpl +37 -37
  98. fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/api/api.py-tpl +11 -11
  99. fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/api/deps.py-tpl +19 -19
  100. fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/api/routes/items.py-tpl +109 -109
  101. fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/core/config.py-tpl +88 -88
  102. fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/core/db.py-tpl +53 -53
  103. fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/crud/items.py-tpl +102 -102
  104. fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/main.py-tpl +45 -45
  105. fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/schemas/items.py-tpl +54 -54
  106. fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/utils/backend_pre_start.py-tpl +39 -39
  107. fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/utils/init_data.py-tpl +23 -23
  108. fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/src/utils/tests_pre_start.py-tpl +39 -39
  109. fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/tests/conftest.py-tpl +63 -63
  110. fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/tests/test_items.py-tpl +44 -44
  111. fastapi_fastkit/fastapi_project_template/modules/api/routes/new_route.py-tpl +64 -64
  112. fastapi_fastkit/fastapi_project_template/modules/crud/new_route.py-tpl +29 -29
  113. fastapi_fastkit/fastapi_project_template/modules/schemas/new_route.py-tpl +23 -23
  114. fastapi_fastkit/logs/fastkit_debug_20250702_161423.log +21 -0
  115. fastapi_fastkit/logs/fastkit_debug_20250702_162706.log +20 -0
  116. fastapi_fastkit/logs/fastkit_debug_20250702_163010.log +21 -0
  117. fastapi_fastkit/logs/fastkit_debug_20250702_163526.log +21 -0
  118. fastapi_fastkit/logs/fastkit_debug_20250702_164053.log +21 -0
  119. fastapi_fastkit/logs/fastkit_debug_20250702_165220.log +21 -0
  120. fastapi_fastkit/logs/fastkit_debug_20250702_165647.log +21 -0
  121. fastapi_fastkit/utils/logging.py +112 -1
  122. fastapi_fastkit/utils/main.py +140 -129
  123. fastapi_fastkit-1.0.2.dist-info/METADATA +320 -0
  124. {fastapi_fastkit-1.0.0.dist-info → fastapi_fastkit-1.0.2.dist-info}/RECORD +127 -119
  125. {fastapi_fastkit-1.0.0.dist-info → fastapi_fastkit-1.0.2.dist-info}/WHEEL +1 -1
  126. {fastapi_fastkit-1.0.0.dist-info → fastapi_fastkit-1.0.2.dist-info}/licenses/LICENSE +21 -21
  127. fastapi_fastkit-1.0.0.dist-info/METADATA +0 -208
  128. {fastapi_fastkit-1.0.0.dist-info → fastapi_fastkit-1.0.2.dist-info}/entry_points.txt +0 -0
@@ -1,36 +1,36 @@
1
- aiofiles==24.1.0
2
- annotated-types==0.7.0
3
- anyio==4.8.0
4
- black==25.1.0
5
- certifi==2025.1.31
6
- click==8.1.8
7
- fastapi==0.115.8
8
- h11==0.14.0
9
- httpcore==1.0.7
10
- httptools==0.6.4
11
- httpx==0.28.1
12
- idna==3.10
13
- iniconfig==2.0.0
14
- isort==6.0.0
15
- mypy==1.15.0
16
- mypy-extensions==1.0.0
17
- packaging==24.2
18
- pathspec==0.12.1
19
- platformdirs==4.3.6
20
- pluggy==1.5.0
21
- pydantic==2.10.6
22
- pydantic-settings==2.7.1
23
- pydantic_core==2.27.2
24
- pytest==8.3.4
25
- pytest-asyncio==0.25.3
26
- python-dotenv==1.0.1
27
- PyYAML==6.0.2
28
- setuptools==75.8.0
29
- sniffio==1.3.1
30
- SQLAlchemy==2.0.38
31
- starlette==0.45.3
32
- typing_extensions==4.12.2
33
- uvicorn==0.34.0
34
- uvloop==0.21.0
35
- watchfiles==1.0.4
36
- websockets==15.0
1
+ aiofiles==24.1.0
2
+ annotated-types==0.7.0
3
+ anyio==4.8.0
4
+ black==25.1.0
5
+ certifi==2025.1.31
6
+ click==8.1.8
7
+ fastapi==0.115.8
8
+ h11==0.14.0
9
+ httpcore==1.0.7
10
+ httptools==0.6.4
11
+ httpx==0.28.1
12
+ idna==3.10
13
+ iniconfig==2.0.0
14
+ isort==6.0.0
15
+ mypy==1.15.0
16
+ mypy-extensions==1.0.0
17
+ packaging==24.2
18
+ pathspec==0.12.1
19
+ platformdirs==4.3.6
20
+ pluggy==1.5.0
21
+ pydantic==2.10.6
22
+ pydantic-settings==2.7.1
23
+ pydantic_core==2.27.2
24
+ pytest==8.3.4
25
+ pytest-asyncio==0.25.3
26
+ python-dotenv==1.0.1
27
+ PyYAML==6.0.2
28
+ setuptools==75.8.0
29
+ sniffio==1.3.1
30
+ SQLAlchemy==2.0.38
31
+ starlette==0.45.3
32
+ typing_extensions==4.12.2
33
+ uvicorn==0.34.0
34
+ uvloop==0.21.0
35
+ watchfiles==1.0.4
36
+ websockets==15.0
@@ -1,5 +1,5 @@
1
- #!/usr/bin/env bash
2
- set -x
3
-
4
- black .
5
- isort .
1
+ #!/usr/bin/env bash
2
+ set -x
3
+
4
+ black .
5
+ isort .
@@ -1,5 +1,5 @@
1
- #!/usr/bin/env bash
2
- set -x
3
-
4
- black . --check
5
- mypy src
1
+ #!/usr/bin/env bash
2
+ set -x
3
+
4
+ black . --check
5
+ mypy src
@@ -1,8 +1,8 @@
1
- #!/usr/bin/env bash
2
-
3
- set -e
4
- set -x
5
-
6
- source .venv/bin/activate
7
-
8
- uvicorn src.main:app --reload
1
+ #!/usr/bin/env bash
2
+
3
+ set -e
4
+ set -x
5
+
6
+ source .venv/bin/activate
7
+
8
+ uvicorn src.main:app --reload
@@ -1,6 +1,6 @@
1
- #!/usr/bin/env bash
2
-
3
- set -e
4
- set -x
5
-
6
- pytest
1
+ #!/usr/bin/env bash
2
+
3
+ set -e
4
+ set -x
5
+
6
+ pytest
@@ -1,12 +1,12 @@
1
- [flake8]
2
- ignore = E501, E305
3
-
4
- [isort]
5
- line_length=100
6
- virtual_env=venv
7
-
8
- [tool:pytest]
9
- testpaths = tests
10
- addopts = -ra -q
11
- asyncio_mode = auto
12
- asyncio_default_fixture_loop_scope=session
1
+ [flake8]
2
+ ignore = E501, E305
3
+
4
+ [isort]
5
+ line_length=100
6
+ virtual_env=venv
7
+
8
+ [tool:pytest]
9
+ testpaths = tests
10
+ addopts = -ra -q
11
+ asyncio_mode = auto
12
+ asyncio_default_fixture_loop_scope=session
@@ -1,27 +1,27 @@
1
- # --------------------------------------------------------------------------
2
- # module description here
3
- # --------------------------------------------------------------------------
4
- from setuptools import find_packages, setup
5
-
6
- install_requires = [
7
- "fastapi",
8
- "uvicorn",
9
- "httpx",
10
- "pydantic",
11
- "pydantic_core",
12
- "pydantic-settings",
13
- "pytest",
14
- "pytest-asyncio",
15
- ]
16
-
17
- # IDE will watch this setup config through your project src, and help you to set up your environment
18
- setup(
19
- name="<project_name>",
20
- description="[FastAPI-fastkit templated] <description>",
21
- author="<author>",
22
- author_email=f"<author_email>",
23
- packages=find_packages(where="src"),
24
- use_scm_version=True,
25
- requires=["python (>=3.11)"],
26
- install_requires=install_requires,
27
- )
1
+ # --------------------------------------------------------------------------
2
+ # module description here
3
+ # --------------------------------------------------------------------------
4
+ from setuptools import find_packages, setup
5
+
6
+ install_requires = [
7
+ "fastapi",
8
+ "uvicorn",
9
+ "httpx",
10
+ "pydantic",
11
+ "pydantic_core",
12
+ "pydantic-settings",
13
+ "pytest",
14
+ "pytest-asyncio",
15
+ ]
16
+
17
+ # IDE will watch this setup config through your project src, and help you to set up your environment
18
+ setup(
19
+ name="<project_name>",
20
+ description="[FastAPI-fastkit templated] <description>",
21
+ author="<author>",
22
+ author_email=f"<author_email>",
23
+ packages=find_packages(where="src"),
24
+ use_scm_version=True,
25
+ requires=["python (>=3.11)"],
26
+ install_requires=install_requires,
27
+ )
@@ -1,9 +1,9 @@
1
- # --------------------------------------------------------------------------
2
- # API router connector module
3
- # --------------------------------------------------------------------------
4
- from fastapi import APIRouter
5
-
6
- from src.api.routes import items
7
-
8
- api_router = APIRouter()
9
- api_router.include_router(items.router, prefix="/items", tags=["items"])
1
+ # --------------------------------------------------------------------------
2
+ # API router connector module
3
+ # --------------------------------------------------------------------------
4
+ from fastapi import APIRouter
5
+
6
+ from src.api.routes import items
7
+
8
+ api_router = APIRouter()
9
+ api_router.include_router(items.router, prefix="/items", tags=["items"])
@@ -1,172 +1,172 @@
1
- # --------------------------------------------------------------------------
2
- # Item CRUD Endpoint
3
- # --------------------------------------------------------------------------
4
- from datetime import UTC, datetime
5
- from typing import Any, List, Union
6
-
7
- from fastapi import APIRouter, Request
8
-
9
- from src.crud.items import read_items, write_items
10
- from src.helper.exceptions import ErrorCode, InternalException
11
- from src.schemas.base import ResponseSchema
12
- from src.schemas.items import ItemCreate, ItemResponse
13
-
14
- router = APIRouter(tags=["items"])
15
-
16
- error_responses: dict[Union[int, str], dict[str, Any]] = {
17
- 404: {"model": ResponseSchema[str], "description": "Item not found"},
18
- 500: {"model": ResponseSchema[str], "description": "Server error"},
19
- }
20
-
21
-
22
- @router.get(
23
- "/",
24
- summary="Get all items",
25
- response_model=ResponseSchema[List[ItemResponse]],
26
- responses=error_responses,
27
- )
28
- async def read_all_items(request: Request):
29
- try:
30
- items = await read_items()
31
- return ResponseSchema(
32
- timestamp=datetime.now(UTC)
33
- .isoformat(timespec="milliseconds")
34
- .replace("+00:00", "Z"),
35
- status=200,
36
- code="HTTP-200",
37
- path=str(request.url),
38
- message=items,
39
- )
40
- except Exception as e:
41
- # You can raise any custom exception you want here
42
- raise InternalException(
43
- message=f"Error retrieving items: {str(e)}",
44
- error_code=ErrorCode.UNKNOWN_ERROR, # Put your custom error code here
45
- )
46
-
47
-
48
- @router.get(
49
- "/{item_id}",
50
- summary="Get item by ID",
51
- response_model=ResponseSchema[ItemResponse],
52
- responses=error_responses,
53
- )
54
- async def read_item(item_id: int, request: Request):
55
- try:
56
- items = await read_items()
57
- item = next((i for i in items if i["id"] == item_id), None)
58
- if not item:
59
- raise InternalException(
60
- message="Item not found", error_code=ErrorCode.NOT_FOUND
61
- )
62
- return ResponseSchema(
63
- timestamp=datetime.now(UTC)
64
- .isoformat(timespec="milliseconds")
65
- .replace("+00:00", "Z"),
66
- status=200,
67
- code="HTTP-200",
68
- path=str(request.url),
69
- message=item,
70
- )
71
- except InternalException as e:
72
- raise e
73
- except Exception as e:
74
- raise InternalException(
75
- message=f"Error retrieving item: {str(e)}",
76
- error_code=ErrorCode.UNKNOWN_ERROR,
77
- )
78
-
79
-
80
- @router.post(
81
- "/",
82
- summary="Create new item",
83
- response_model=ResponseSchema[ItemResponse],
84
- status_code=201,
85
- responses=error_responses,
86
- )
87
- async def create_item(item: ItemCreate, request: Request):
88
- try:
89
- items = await read_items()
90
- new_id = max(i["id"] for i in items) + 1 if items else 1
91
- new_item = {**item.model_dump(), "id": new_id}
92
- items.append(new_item)
93
- await write_items(items)
94
- return ResponseSchema(
95
- timestamp=datetime.now(UTC)
96
- .isoformat(timespec="milliseconds")
97
- .replace("+00:00", "Z"),
98
- status=201,
99
- code="HTTP-201",
100
- path=str(request.url),
101
- message=new_item,
102
- )
103
- except Exception as e:
104
- raise InternalException(
105
- message=f"Error creating item: {str(e)}", error_code=ErrorCode.UNKNOWN_ERROR
106
- )
107
-
108
-
109
- @router.put(
110
- "/{item_id}",
111
- summary="Update item",
112
- response_model=ResponseSchema[ItemResponse],
113
- responses=error_responses,
114
- )
115
- async def update_item(item_id: int, item: ItemCreate, request: Request):
116
- try:
117
- items = await read_items()
118
- index = next((i for i, v in enumerate(items) if v["id"] == item_id), None)
119
- if index is None:
120
- raise InternalException(
121
- message="Item not found", error_code=ErrorCode.NOT_FOUND
122
- )
123
- updated_item = {**item.model_dump(), "id": item_id}
124
- items[index] = updated_item
125
- await write_items(items)
126
- return ResponseSchema(
127
- timestamp=datetime.now(UTC)
128
- .isoformat(timespec="milliseconds")
129
- .replace("+00:00", "Z"),
130
- status=200,
131
- code="HTTP-200",
132
- path=str(request.url),
133
- message=updated_item,
134
- )
135
- except InternalException as e:
136
- raise e
137
- except Exception as e:
138
- raise InternalException(
139
- message=f"Error updating item: {str(e)}", error_code=ErrorCode.UNKNOWN_ERROR
140
- )
141
-
142
-
143
- @router.delete(
144
- "/{item_id}",
145
- summary="Delete item",
146
- response_model=ResponseSchema[str],
147
- responses=error_responses,
148
- )
149
- async def delete_item(item_id: int, request: Request):
150
- try:
151
- items = await read_items()
152
- new_items = [i for i in items if i["id"] != item_id]
153
- if len(new_items) == len(items):
154
- raise InternalException(
155
- message="Item not found", error_code=ErrorCode.NOT_FOUND
156
- )
157
- await write_items(new_items)
158
- return ResponseSchema(
159
- timestamp=datetime.now(UTC)
160
- .isoformat(timespec="milliseconds")
161
- .replace("+00:00", "Z"),
162
- status=200,
163
- code="HTTP-200",
164
- path=str(request.url),
165
- message="Item deleted successfully",
166
- )
167
- except InternalException as e:
168
- raise e
169
- except Exception as e:
170
- raise InternalException(
171
- message=f"Error deleting item: {str(e)}", error_code=ErrorCode.UNKNOWN_ERROR
172
- )
1
+ # --------------------------------------------------------------------------
2
+ # Item CRUD Endpoint
3
+ # --------------------------------------------------------------------------
4
+ from datetime import UTC, datetime
5
+ from typing import Any, List, Union
6
+
7
+ from fastapi import APIRouter, Request
8
+
9
+ from src.crud.items import read_items, write_items
10
+ from src.helper.exceptions import ErrorCode, InternalException
11
+ from src.schemas.base import ResponseSchema
12
+ from src.schemas.items import ItemCreate, ItemResponse
13
+
14
+ router = APIRouter(tags=["items"])
15
+
16
+ error_responses: dict[Union[int, str], dict[str, Any]] = {
17
+ 404: {"model": ResponseSchema[str], "description": "Item not found"},
18
+ 500: {"model": ResponseSchema[str], "description": "Server error"},
19
+ }
20
+
21
+
22
+ @router.get(
23
+ "/",
24
+ summary="Get all items",
25
+ response_model=ResponseSchema[List[ItemResponse]],
26
+ responses=error_responses,
27
+ )
28
+ async def read_all_items(request: Request):
29
+ try:
30
+ items = await read_items()
31
+ return ResponseSchema(
32
+ timestamp=datetime.now(UTC)
33
+ .isoformat(timespec="milliseconds")
34
+ .replace("+00:00", "Z"),
35
+ status=200,
36
+ code="HTTP-200",
37
+ path=str(request.url),
38
+ message=items,
39
+ )
40
+ except Exception as e:
41
+ # You can raise any custom exception you want here
42
+ raise InternalException(
43
+ message=f"Error retrieving items: {str(e)}",
44
+ error_code=ErrorCode.UNKNOWN_ERROR, # Put your custom error code here
45
+ )
46
+
47
+
48
+ @router.get(
49
+ "/{item_id}",
50
+ summary="Get item by ID",
51
+ response_model=ResponseSchema[ItemResponse],
52
+ responses=error_responses,
53
+ )
54
+ async def read_item(item_id: int, request: Request):
55
+ try:
56
+ items = await read_items()
57
+ item = next((i for i in items if i["id"] == item_id), None)
58
+ if not item:
59
+ raise InternalException(
60
+ message="Item not found", error_code=ErrorCode.NOT_FOUND
61
+ )
62
+ return ResponseSchema(
63
+ timestamp=datetime.now(UTC)
64
+ .isoformat(timespec="milliseconds")
65
+ .replace("+00:00", "Z"),
66
+ status=200,
67
+ code="HTTP-200",
68
+ path=str(request.url),
69
+ message=item,
70
+ )
71
+ except InternalException as e:
72
+ raise e
73
+ except Exception as e:
74
+ raise InternalException(
75
+ message=f"Error retrieving item: {str(e)}",
76
+ error_code=ErrorCode.UNKNOWN_ERROR,
77
+ )
78
+
79
+
80
+ @router.post(
81
+ "/",
82
+ summary="Create new item",
83
+ response_model=ResponseSchema[ItemResponse],
84
+ status_code=201,
85
+ responses=error_responses,
86
+ )
87
+ async def create_item(item: ItemCreate, request: Request):
88
+ try:
89
+ items = await read_items()
90
+ new_id = max(i["id"] for i in items) + 1 if items else 1
91
+ new_item = {**item.model_dump(), "id": new_id}
92
+ items.append(new_item)
93
+ await write_items(items)
94
+ return ResponseSchema(
95
+ timestamp=datetime.now(UTC)
96
+ .isoformat(timespec="milliseconds")
97
+ .replace("+00:00", "Z"),
98
+ status=201,
99
+ code="HTTP-201",
100
+ path=str(request.url),
101
+ message=new_item,
102
+ )
103
+ except Exception as e:
104
+ raise InternalException(
105
+ message=f"Error creating item: {str(e)}", error_code=ErrorCode.UNKNOWN_ERROR
106
+ )
107
+
108
+
109
+ @router.put(
110
+ "/{item_id}",
111
+ summary="Update item",
112
+ response_model=ResponseSchema[ItemResponse],
113
+ responses=error_responses,
114
+ )
115
+ async def update_item(item_id: int, item: ItemCreate, request: Request):
116
+ try:
117
+ items = await read_items()
118
+ index = next((i for i, v in enumerate(items) if v["id"] == item_id), None)
119
+ if index is None:
120
+ raise InternalException(
121
+ message="Item not found", error_code=ErrorCode.NOT_FOUND
122
+ )
123
+ updated_item = {**item.model_dump(), "id": item_id}
124
+ items[index] = updated_item
125
+ await write_items(items)
126
+ return ResponseSchema(
127
+ timestamp=datetime.now(UTC)
128
+ .isoformat(timespec="milliseconds")
129
+ .replace("+00:00", "Z"),
130
+ status=200,
131
+ code="HTTP-200",
132
+ path=str(request.url),
133
+ message=updated_item,
134
+ )
135
+ except InternalException as e:
136
+ raise e
137
+ except Exception as e:
138
+ raise InternalException(
139
+ message=f"Error updating item: {str(e)}", error_code=ErrorCode.UNKNOWN_ERROR
140
+ )
141
+
142
+
143
+ @router.delete(
144
+ "/{item_id}",
145
+ summary="Delete item",
146
+ response_model=ResponseSchema[str],
147
+ responses=error_responses,
148
+ )
149
+ async def delete_item(item_id: int, request: Request):
150
+ try:
151
+ items = await read_items()
152
+ new_items = [i for i in items if i["id"] != item_id]
153
+ if len(new_items) == len(items):
154
+ raise InternalException(
155
+ message="Item not found", error_code=ErrorCode.NOT_FOUND
156
+ )
157
+ await write_items(new_items)
158
+ return ResponseSchema(
159
+ timestamp=datetime.now(UTC)
160
+ .isoformat(timespec="milliseconds")
161
+ .replace("+00:00", "Z"),
162
+ status=200,
163
+ code="HTTP-200",
164
+ path=str(request.url),
165
+ message="Item deleted successfully",
166
+ )
167
+ except InternalException as e:
168
+ raise e
169
+ except Exception as e:
170
+ raise InternalException(
171
+ message=f"Error deleting item: {str(e)}", error_code=ErrorCode.UNKNOWN_ERROR
172
+ )