PyArch-CLI 0.1.0__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.
- pyarch/__init__.py +1 -0
- pyarch/cli/__init__.py +1 -0
- pyarch/cli/add.py +27 -0
- pyarch/cli/app.py +83 -0
- pyarch/cli/common.py +28 -0
- pyarch/cli/generate.py +34 -0
- pyarch/config/__init__.py +1 -0
- pyarch/config/manifest.py +161 -0
- pyarch/config/models.py +51 -0
- pyarch/generators/__init__.py +1 -0
- pyarch/generators/common/__init__.py +1 -0
- pyarch/generators/common/commands.py +6 -0
- pyarch/generators/common/filesystem.py +55 -0
- pyarch/generators/common/gitignore.py +65 -0
- pyarch/generators/common/renderer.py +32 -0
- pyarch/generators/integration/__init__.py +1 -0
- pyarch/generators/integration/auth.py +158 -0
- pyarch/generators/module/__init__.py +1 -0
- pyarch/generators/module/layered.py +186 -0
- pyarch/generators/project/__init__.py +1 -0
- pyarch/generators/project/base.py +52 -0
- pyarch/generators/project/layered.py +167 -0
- pyarch/main.py +9 -0
- pyarch/services/__init__.py +1 -0
- pyarch/services/add_integration.py +37 -0
- pyarch/services/create_module.py +46 -0
- pyarch/services/create_project.py +20 -0
- pyarch/templates/layered/alembic_env.py.j2 +58 -0
- pyarch/templates/layered/auth/config.py.j2 +30 -0
- pyarch/templates/layered/auth/dependencies.py.j2 +75 -0
- pyarch/templates/layered/auth/refresh_session_model.py.j2 +31 -0
- pyarch/templates/layered/auth/routes.py.j2 +51 -0
- pyarch/templates/layered/auth/schemas.py.j2 +31 -0
- pyarch/templates/layered/auth/service.py.j2 +160 -0
- pyarch/templates/layered/auth/tokens.py.j2 +29 -0
- pyarch/templates/layered/auth/user_model.py.j2 +52 -0
- pyarch/templates/layered/auth/utils.py.j2 +70 -0
- pyarch/templates/layered/config.py.j2 +14 -0
- pyarch/templates/layered/env.example.j2 +7 -0
- pyarch/templates/layered/models_init.py.j2 +5 -0
- pyarch/templates/layered/module/module_model.py.j2 +25 -0
- pyarch/templates/layered/module/module_repo.py.j2 +49 -0
- pyarch/templates/layered/module/module_router.py.j2 +87 -0
- pyarch/templates/layered/module/module_router_protected.py.j2 +97 -0
- pyarch/templates/layered/module/module_schema.py.j2 +30 -0
- pyarch/templates/layered/module/module_service.py.j2 +76 -0
- pyarch/templates/layered/mongodb/config.py.j2 +15 -0
- pyarch/templates/layered/mongodb/conftest.py.j2 +36 -0
- pyarch/templates/layered/mongodb/env.example.j2 +2 -0
- pyarch/templates/layered/mongodb/module/module_model.py.j2 +11 -0
- pyarch/templates/layered/mongodb/module/module_repo.py.j2 +99 -0
- pyarch/templates/layered/mongodb/module/module_router.py.j2 +97 -0
- pyarch/templates/layered/mongodb/module/module_service.py.j2 +69 -0
- pyarch/templates/layered/mongodb/session.py.j2 +14 -0
- pyarch/templates/layered/router.py.j2 +8 -0
- pyarch/templates/layered/session.py.j2 +33 -0
- pyarch/templates/project/base/README.md.j2 +22 -0
- pyarch/templates/project/base/conftest.py.j2 +101 -0
- pyarch/templates/project/base/main.py.j2 +11 -0
- pyarch_cli-0.1.0.dist-info/METADATA +378 -0
- pyarch_cli-0.1.0.dist-info/RECORD +64 -0
- pyarch_cli-0.1.0.dist-info/WHEEL +4 -0
- pyarch_cli-0.1.0.dist-info/entry_points.txt +2 -0
- pyarch_cli-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
from typing import Annotated
|
|
2
|
+
|
|
3
|
+
from fastapi import APIRouter, Depends, HTTPException, Query, Response, status
|
|
4
|
+
from sqlalchemy.orm import Session
|
|
5
|
+
|
|
6
|
+
from app.db.session import get_db
|
|
7
|
+
from app.schemas.{{ module_name }} import (
|
|
8
|
+
{{ model_name }}Create,
|
|
9
|
+
{{ model_name }}Read,
|
|
10
|
+
{{ model_name }}Update,
|
|
11
|
+
)
|
|
12
|
+
from app.services import {{ module_name }} as {{ module_name }}_service
|
|
13
|
+
from app.services.{{ module_name }} import {{ model_name }}NotFoundError
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
router = APIRouter(prefix="/{{ resource_name }}", tags=["{{ resource_name }}"])
|
|
17
|
+
DbSession = Annotated[Session, Depends(get_db)]
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def not_found_error(error: {{ model_name }}NotFoundError) -> HTTPException:
|
|
21
|
+
return HTTPException(
|
|
22
|
+
status_code=status.HTTP_404_NOT_FOUND,
|
|
23
|
+
detail=str(error),
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@router.post(
|
|
28
|
+
"",
|
|
29
|
+
response_model={{ model_name }}Read,
|
|
30
|
+
status_code=status.HTTP_201_CREATED,
|
|
31
|
+
)
|
|
32
|
+
def add_item(
|
|
33
|
+
payload: {{ model_name }}Create,
|
|
34
|
+
db: DbSession,
|
|
35
|
+
) -> {{ model_name }}Read:
|
|
36
|
+
item = {{ module_name }}_service.add_item(db, payload)
|
|
37
|
+
return {{ model_name }}Read.model_validate(item)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@router.get("", response_model=list[{{ model_name }}Read])
|
|
41
|
+
def get_items(
|
|
42
|
+
db: DbSession,
|
|
43
|
+
offset: Annotated[int, Query(ge=0)] = 0,
|
|
44
|
+
limit: Annotated[int, Query(ge=1, le=100)] = 100,
|
|
45
|
+
) -> list[{{ model_name }}Read]:
|
|
46
|
+
items = {{ module_name }}_service.get_items(
|
|
47
|
+
db,
|
|
48
|
+
offset=offset,
|
|
49
|
+
limit=limit,
|
|
50
|
+
)
|
|
51
|
+
return [{{ model_name }}Read.model_validate(item) for item in items]
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
@router.get("/{item_id}", response_model={{ model_name }}Read)
|
|
55
|
+
def get_item(item_id: int, db: DbSession) -> {{ model_name }}Read:
|
|
56
|
+
try:
|
|
57
|
+
item = {{ module_name }}_service.get_item(db, item_id)
|
|
58
|
+
return {{ model_name }}Read.model_validate(item)
|
|
59
|
+
except {{ model_name }}NotFoundError as error:
|
|
60
|
+
raise not_found_error(error) from error
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
@router.patch("/{item_id}", response_model={{ model_name }}Read)
|
|
64
|
+
def patch_item(
|
|
65
|
+
item_id: int,
|
|
66
|
+
payload: {{ model_name }}Update,
|
|
67
|
+
db: DbSession,
|
|
68
|
+
) -> {{ model_name }}Read:
|
|
69
|
+
try:
|
|
70
|
+
item = {{ module_name }}_service.patch_item(db, item_id, payload)
|
|
71
|
+
return {{ model_name }}Read.model_validate(item)
|
|
72
|
+
except {{ model_name }}NotFoundError as error:
|
|
73
|
+
raise not_found_error(error) from error
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
@router.delete(
|
|
77
|
+
"/{item_id}",
|
|
78
|
+
status_code=status.HTTP_204_NO_CONTENT,
|
|
79
|
+
response_class=Response,
|
|
80
|
+
)
|
|
81
|
+
def delete_item(item_id: int, db: DbSession) -> Response:
|
|
82
|
+
try:
|
|
83
|
+
{{ module_name }}_service.delete_item(db, item_id)
|
|
84
|
+
except {{ model_name }}NotFoundError as error:
|
|
85
|
+
raise not_found_error(error) from error
|
|
86
|
+
|
|
87
|
+
return Response(status_code=status.HTTP_204_NO_CONTENT)
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
from typing import Annotated
|
|
2
|
+
|
|
3
|
+
from fastapi import APIRouter, Depends, HTTPException, Query, Response, status
|
|
4
|
+
from sqlalchemy.orm import Session
|
|
5
|
+
|
|
6
|
+
from app.auth.dependencies import get_current_active_user
|
|
7
|
+
from app.db.session import get_db
|
|
8
|
+
from app.models.user import User
|
|
9
|
+
from app.schemas.{{ module_name }} import (
|
|
10
|
+
{{ model_name }}Create,
|
|
11
|
+
{{ model_name }}Read,
|
|
12
|
+
{{ model_name }}Update,
|
|
13
|
+
)
|
|
14
|
+
from app.services import {{ module_name }} as {{ module_name }}_service
|
|
15
|
+
from app.services.{{ module_name }} import {{ model_name }}NotFoundError
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
router = APIRouter(prefix="/{{ resource_name }}", tags=["{{ resource_name }}"])
|
|
19
|
+
DbSession = Annotated[Session, Depends(get_db)]
|
|
20
|
+
CurrentUser = Annotated[User, Depends(get_current_active_user)]
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def not_found_error(error: {{ model_name }}NotFoundError) -> HTTPException:
|
|
24
|
+
return HTTPException(
|
|
25
|
+
status_code=status.HTTP_404_NOT_FOUND,
|
|
26
|
+
detail=str(error),
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@router.post(
|
|
31
|
+
"",
|
|
32
|
+
response_model={{ model_name }}Read,
|
|
33
|
+
status_code=status.HTTP_201_CREATED,
|
|
34
|
+
)
|
|
35
|
+
def add_item(
|
|
36
|
+
payload: {{ model_name }}Create,
|
|
37
|
+
db: DbSession,
|
|
38
|
+
user: CurrentUser,
|
|
39
|
+
) -> {{ model_name }}Read:
|
|
40
|
+
item = {{ module_name }}_service.add_item(db, payload)
|
|
41
|
+
return {{ model_name }}Read.model_validate(item)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@router.get("", response_model=list[{{ model_name }}Read])
|
|
45
|
+
def get_items(
|
|
46
|
+
db: DbSession,
|
|
47
|
+
user: CurrentUser,
|
|
48
|
+
offset: Annotated[int, Query(ge=0)] = 0,
|
|
49
|
+
limit: Annotated[int, Query(ge=1, le=100)] = 100,
|
|
50
|
+
) -> list[{{ model_name }}Read]:
|
|
51
|
+
items = {{ module_name }}_service.get_items(
|
|
52
|
+
db,
|
|
53
|
+
offset=offset,
|
|
54
|
+
limit=limit,
|
|
55
|
+
)
|
|
56
|
+
return [{{ model_name }}Read.model_validate(item) for item in items]
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
@router.get("/{item_id}", response_model={{ model_name }}Read)
|
|
60
|
+
def get_item(
|
|
61
|
+
item_id: int,
|
|
62
|
+
db: DbSession,
|
|
63
|
+
user: CurrentUser,
|
|
64
|
+
) -> {{ model_name }}Read:
|
|
65
|
+
try:
|
|
66
|
+
item = {{ module_name }}_service.get_item(db, item_id)
|
|
67
|
+
return {{ model_name }}Read.model_validate(item)
|
|
68
|
+
except {{ model_name }}NotFoundError as error:
|
|
69
|
+
raise not_found_error(error) from error
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
@router.patch("/{item_id}", response_model={{ model_name }}Read)
|
|
73
|
+
def patch_item(
|
|
74
|
+
item_id: int,
|
|
75
|
+
payload: {{ model_name }}Update,
|
|
76
|
+
db: DbSession,
|
|
77
|
+
user: CurrentUser,
|
|
78
|
+
) -> {{ model_name }}Read:
|
|
79
|
+
try:
|
|
80
|
+
item = {{ module_name }}_service.patch_item(db, item_id, payload)
|
|
81
|
+
return {{ model_name }}Read.model_validate(item)
|
|
82
|
+
except {{ model_name }}NotFoundError as error:
|
|
83
|
+
raise not_found_error(error) from error
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
@router.delete(
|
|
87
|
+
"/{item_id}",
|
|
88
|
+
status_code=status.HTTP_204_NO_CONTENT,
|
|
89
|
+
response_class=Response,
|
|
90
|
+
)
|
|
91
|
+
def delete_item(item_id: int, db: DbSession, user: CurrentUser) -> Response:
|
|
92
|
+
try:
|
|
93
|
+
{{ module_name }}_service.delete_item(db, item_id)
|
|
94
|
+
except {{ model_name }}NotFoundError as error:
|
|
95
|
+
raise not_found_error(error) from error
|
|
96
|
+
|
|
97
|
+
return Response(status_code=status.HTTP_204_NO_CONTENT)
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
from datetime import datetime
|
|
2
|
+
|
|
3
|
+
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class {{ model_name }}Create(BaseModel):
|
|
7
|
+
title: str = Field(min_length=1, max_length=55)
|
|
8
|
+
description: str | None = Field(default=None, max_length=255)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class {{ model_name }}Update(BaseModel):
|
|
12
|
+
title: str | None = Field(default=None, min_length=1, max_length=55)
|
|
13
|
+
description: str | None = Field(default=None, max_length=255)
|
|
14
|
+
|
|
15
|
+
@field_validator("title")
|
|
16
|
+
@classmethod
|
|
17
|
+
def title_must_not_be_null(cls, value: str | None) -> str | None:
|
|
18
|
+
if value is None:
|
|
19
|
+
raise ValueError("title cannot be null")
|
|
20
|
+
return value
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class {{ model_name }}Read(BaseModel):
|
|
24
|
+
id: {{ id_type }}
|
|
25
|
+
title: str
|
|
26
|
+
description: str | None
|
|
27
|
+
created_at: datetime
|
|
28
|
+
updated_at: datetime
|
|
29
|
+
|
|
30
|
+
model_config = ConfigDict(from_attributes=True)
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
from sqlalchemy.orm import Session
|
|
2
|
+
|
|
3
|
+
from app.models.{{ module_name }} import {{ model_name }}
|
|
4
|
+
from app.repositories import {{ module_name }} as {{ module_name }}_repository
|
|
5
|
+
from app.schemas.{{ module_name }} import {{ model_name }}Create, {{ model_name }}Update
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class {{ model_name }}NotFoundError(LookupError):
|
|
9
|
+
def __init__(self, item_id: int) -> None:
|
|
10
|
+
super().__init__(f"{{ model_name }} with id={item_id} was not found")
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def add_item(db: Session, payload: {{ model_name }}Create) -> {{ model_name }}:
|
|
14
|
+
try:
|
|
15
|
+
item = {{ module_name }}_repository.add_item(
|
|
16
|
+
db,
|
|
17
|
+
payload.model_dump(),
|
|
18
|
+
)
|
|
19
|
+
db.commit()
|
|
20
|
+
db.refresh(item)
|
|
21
|
+
return item
|
|
22
|
+
except Exception:
|
|
23
|
+
db.rollback()
|
|
24
|
+
raise
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def get_items(
|
|
28
|
+
db: Session,
|
|
29
|
+
*,
|
|
30
|
+
offset: int = 0,
|
|
31
|
+
limit: int = 100,
|
|
32
|
+
) -> list[{{ model_name }}]:
|
|
33
|
+
return {{ module_name }}_repository.get_items(
|
|
34
|
+
db,
|
|
35
|
+
offset=offset,
|
|
36
|
+
limit=limit,
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def get_item(db: Session, item_id: int) -> {{ model_name }}:
|
|
41
|
+
item = {{ module_name }}_repository.get_item(db, item_id)
|
|
42
|
+
if item is None:
|
|
43
|
+
raise {{ model_name }}NotFoundError(item_id)
|
|
44
|
+
return item
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def patch_item(
|
|
48
|
+
db: Session,
|
|
49
|
+
item_id: int,
|
|
50
|
+
payload: {{ model_name }}Update,
|
|
51
|
+
) -> {{ model_name }}:
|
|
52
|
+
item = get_item(db, item_id)
|
|
53
|
+
|
|
54
|
+
try:
|
|
55
|
+
updated_item = {{ module_name }}_repository.patch_item(
|
|
56
|
+
db,
|
|
57
|
+
item,
|
|
58
|
+
payload.model_dump(exclude_unset=True),
|
|
59
|
+
)
|
|
60
|
+
db.commit()
|
|
61
|
+
db.refresh(updated_item)
|
|
62
|
+
return updated_item
|
|
63
|
+
except Exception:
|
|
64
|
+
db.rollback()
|
|
65
|
+
raise
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def delete_item(db: Session, item_id: int) -> None:
|
|
69
|
+
item = get_item(db, item_id)
|
|
70
|
+
|
|
71
|
+
try:
|
|
72
|
+
{{ module_name }}_repository.delete_item(db, item)
|
|
73
|
+
db.commit()
|
|
74
|
+
except Exception:
|
|
75
|
+
db.rollback()
|
|
76
|
+
raise
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class Settings(BaseSettings):
|
|
5
|
+
database_url: str
|
|
6
|
+
database_name: str
|
|
7
|
+
|
|
8
|
+
model_config = SettingsConfigDict(
|
|
9
|
+
env_file=".env",
|
|
10
|
+
env_file_encoding="utf-8",
|
|
11
|
+
extra="ignore",
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
settings = Settings() # type: ignore[call-arg]
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
from collections.abc import Generator
|
|
2
|
+
from typing import Any
|
|
3
|
+
|
|
4
|
+
import mongomock
|
|
5
|
+
import pytest
|
|
6
|
+
from fastapi.testclient import TestClient
|
|
7
|
+
from pymongo.database import Database
|
|
8
|
+
|
|
9
|
+
from app.db.session import get_database
|
|
10
|
+
from app.main import app as fastapi_app
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@pytest.fixture()
|
|
14
|
+
def database() -> Generator[Database[dict[str, Any]], None, None]:
|
|
15
|
+
mongo_client: Any = mongomock.MongoClient()
|
|
16
|
+
yield mongo_client["app_test"]
|
|
17
|
+
mongo_client.close()
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@pytest.fixture()
|
|
21
|
+
def client(
|
|
22
|
+
database: Database[dict[str, Any]],
|
|
23
|
+
) -> Generator[TestClient, None, None]:
|
|
24
|
+
def override_get_database() -> Generator[
|
|
25
|
+
Database[dict[str, Any]],
|
|
26
|
+
None,
|
|
27
|
+
None,
|
|
28
|
+
]:
|
|
29
|
+
yield database
|
|
30
|
+
|
|
31
|
+
fastapi_app.dependency_overrides[get_database] = override_get_database
|
|
32
|
+
|
|
33
|
+
with TestClient(fastapi_app) as test_client:
|
|
34
|
+
yield test_client
|
|
35
|
+
|
|
36
|
+
fastapi_app.dependency_overrides.clear()
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
from datetime import UTC, datetime
|
|
2
|
+
from typing import Any
|
|
3
|
+
|
|
4
|
+
from bson import ObjectId
|
|
5
|
+
from bson.errors import InvalidId
|
|
6
|
+
from pymongo import ReturnDocument
|
|
7
|
+
from pymongo.collection import Collection
|
|
8
|
+
from pymongo.database import Database
|
|
9
|
+
|
|
10
|
+
from app.models.{{ module_name }} import {{ model_name }}
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def get_collection(
|
|
14
|
+
database: Database[dict[str, Any]],
|
|
15
|
+
) -> Collection[dict[str, Any]]:
|
|
16
|
+
return database["{{ resource_name }}"]
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def to_model(document: dict[str, Any]) -> {{ model_name }}:
|
|
20
|
+
return {{ model_name }}(
|
|
21
|
+
id=str(document["_id"]),
|
|
22
|
+
title=document["title"],
|
|
23
|
+
description=document.get("description"),
|
|
24
|
+
created_at=document["created_at"],
|
|
25
|
+
updated_at=document["updated_at"],
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def to_object_id(item_id: str) -> ObjectId | None:
|
|
30
|
+
try:
|
|
31
|
+
return ObjectId(item_id)
|
|
32
|
+
except InvalidId:
|
|
33
|
+
return None
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def add_item(
|
|
37
|
+
database: Database[dict[str, Any]],
|
|
38
|
+
values: dict[str, Any],
|
|
39
|
+
) -> {{ model_name }}:
|
|
40
|
+
now = datetime.now(UTC)
|
|
41
|
+
document = {
|
|
42
|
+
**values,
|
|
43
|
+
"created_at": now,
|
|
44
|
+
"updated_at": now,
|
|
45
|
+
}
|
|
46
|
+
result = get_collection(database).insert_one(document)
|
|
47
|
+
document["_id"] = result.inserted_id
|
|
48
|
+
return to_model(document)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def get_items(
|
|
52
|
+
database: Database[dict[str, Any]],
|
|
53
|
+
*,
|
|
54
|
+
offset: int = 0,
|
|
55
|
+
limit: int = 100,
|
|
56
|
+
) -> list[{{ model_name }}]:
|
|
57
|
+
cursor = get_collection(database).find().skip(offset).limit(limit)
|
|
58
|
+
return [to_model(document) for document in cursor]
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def get_item(
|
|
62
|
+
database: Database[dict[str, Any]],
|
|
63
|
+
item_id: str,
|
|
64
|
+
) -> {{ model_name }} | None:
|
|
65
|
+
object_id = to_object_id(item_id)
|
|
66
|
+
if object_id is None:
|
|
67
|
+
return None
|
|
68
|
+
|
|
69
|
+
document = get_collection(database).find_one({"_id": object_id})
|
|
70
|
+
return to_model(document) if document is not None else None
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def patch_item(
|
|
74
|
+
database: Database[dict[str, Any]],
|
|
75
|
+
item_id: str,
|
|
76
|
+
values: dict[str, Any],
|
|
77
|
+
) -> {{ model_name }} | None:
|
|
78
|
+
object_id = to_object_id(item_id)
|
|
79
|
+
if object_id is None:
|
|
80
|
+
return None
|
|
81
|
+
|
|
82
|
+
document = get_collection(database).find_one_and_update(
|
|
83
|
+
{"_id": object_id},
|
|
84
|
+
{"$set": {**values, "updated_at": datetime.now(UTC)}},
|
|
85
|
+
return_document=ReturnDocument.AFTER,
|
|
86
|
+
)
|
|
87
|
+
return to_model(document) if document is not None else None
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def delete_item(
|
|
91
|
+
database: Database[dict[str, Any]],
|
|
92
|
+
item_id: str,
|
|
93
|
+
) -> bool:
|
|
94
|
+
object_id = to_object_id(item_id)
|
|
95
|
+
if object_id is None:
|
|
96
|
+
return False
|
|
97
|
+
|
|
98
|
+
result = get_collection(database).delete_one({"_id": object_id})
|
|
99
|
+
return result.deleted_count == 1
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
from typing import Annotated, Any
|
|
2
|
+
|
|
3
|
+
from fastapi import APIRouter, Depends, HTTPException, Query, Response, status
|
|
4
|
+
from pymongo.database import Database
|
|
5
|
+
|
|
6
|
+
from app.db.session import get_database
|
|
7
|
+
from app.schemas.{{ module_name }} import (
|
|
8
|
+
{{ model_name }}Create,
|
|
9
|
+
{{ model_name }}Read,
|
|
10
|
+
{{ model_name }}Update,
|
|
11
|
+
)
|
|
12
|
+
from app.services import {{ module_name }} as {{ module_name }}_service
|
|
13
|
+
from app.services.{{ module_name }} import {{ model_name }}NotFoundError
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
router = APIRouter(prefix="/{{ resource_name }}", tags=["{{ resource_name }}"])
|
|
17
|
+
MongoDatabase = Annotated[
|
|
18
|
+
Database[dict[str, Any]],
|
|
19
|
+
Depends(get_database),
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def not_found_error(error: {{ model_name }}NotFoundError) -> HTTPException:
|
|
24
|
+
return HTTPException(
|
|
25
|
+
status_code=status.HTTP_404_NOT_FOUND,
|
|
26
|
+
detail=str(error),
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@router.post(
|
|
31
|
+
"",
|
|
32
|
+
response_model={{ model_name }}Read,
|
|
33
|
+
status_code=status.HTTP_201_CREATED,
|
|
34
|
+
)
|
|
35
|
+
def add_item(
|
|
36
|
+
payload: {{ model_name }}Create,
|
|
37
|
+
database: MongoDatabase,
|
|
38
|
+
) -> {{ model_name }}Read:
|
|
39
|
+
item = {{ module_name }}_service.add_item(database, payload)
|
|
40
|
+
return {{ model_name }}Read.model_validate(item)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@router.get("", response_model=list[{{ model_name }}Read])
|
|
44
|
+
def get_items(
|
|
45
|
+
database: MongoDatabase,
|
|
46
|
+
offset: Annotated[int, Query(ge=0)] = 0,
|
|
47
|
+
limit: Annotated[int, Query(ge=1, le=100)] = 100,
|
|
48
|
+
) -> list[{{ model_name }}Read]:
|
|
49
|
+
items = {{ module_name }}_service.get_items(
|
|
50
|
+
database,
|
|
51
|
+
offset=offset,
|
|
52
|
+
limit=limit,
|
|
53
|
+
)
|
|
54
|
+
return [{{ model_name }}Read.model_validate(item) for item in items]
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
@router.get("/{item_id}", response_model={{ model_name }}Read)
|
|
58
|
+
def get_item(
|
|
59
|
+
item_id: str,
|
|
60
|
+
database: MongoDatabase,
|
|
61
|
+
) -> {{ model_name }}Read:
|
|
62
|
+
try:
|
|
63
|
+
item = {{ module_name }}_service.get_item(database, item_id)
|
|
64
|
+
return {{ model_name }}Read.model_validate(item)
|
|
65
|
+
except {{ model_name }}NotFoundError as error:
|
|
66
|
+
raise not_found_error(error) from error
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
@router.patch("/{item_id}", response_model={{ model_name }}Read)
|
|
70
|
+
def patch_item(
|
|
71
|
+
item_id: str,
|
|
72
|
+
payload: {{ model_name }}Update,
|
|
73
|
+
database: MongoDatabase,
|
|
74
|
+
) -> {{ model_name }}Read:
|
|
75
|
+
try:
|
|
76
|
+
item = {{ module_name }}_service.patch_item(
|
|
77
|
+
database,
|
|
78
|
+
item_id,
|
|
79
|
+
payload,
|
|
80
|
+
)
|
|
81
|
+
return {{ model_name }}Read.model_validate(item)
|
|
82
|
+
except {{ model_name }}NotFoundError as error:
|
|
83
|
+
raise not_found_error(error) from error
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
@router.delete(
|
|
87
|
+
"/{item_id}",
|
|
88
|
+
status_code=status.HTTP_204_NO_CONTENT,
|
|
89
|
+
response_class=Response,
|
|
90
|
+
)
|
|
91
|
+
def delete_item(item_id: str, database: MongoDatabase) -> Response:
|
|
92
|
+
try:
|
|
93
|
+
{{ module_name }}_service.delete_item(database, item_id)
|
|
94
|
+
except {{ model_name }}NotFoundError as error:
|
|
95
|
+
raise not_found_error(error) from error
|
|
96
|
+
|
|
97
|
+
return Response(status_code=status.HTTP_204_NO_CONTENT)
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
from typing import Any
|
|
2
|
+
|
|
3
|
+
from pymongo.database import Database
|
|
4
|
+
|
|
5
|
+
from app.models.{{ module_name }} import {{ model_name }}
|
|
6
|
+
from app.repositories import {{ module_name }} as {{ module_name }}_repository
|
|
7
|
+
from app.schemas.{{ module_name }} import {{ model_name }}Create, {{ model_name }}Update
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class {{ model_name }}NotFoundError(LookupError):
|
|
11
|
+
def __init__(self, item_id: str) -> None:
|
|
12
|
+
super().__init__(f"{{ model_name }} with id={item_id} was not found")
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def add_item(
|
|
16
|
+
database: Database[dict[str, Any]],
|
|
17
|
+
payload: {{ model_name }}Create,
|
|
18
|
+
) -> {{ model_name }}:
|
|
19
|
+
return {{ module_name }}_repository.add_item(
|
|
20
|
+
database,
|
|
21
|
+
payload.model_dump(),
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def get_items(
|
|
26
|
+
database: Database[dict[str, Any]],
|
|
27
|
+
*,
|
|
28
|
+
offset: int = 0,
|
|
29
|
+
limit: int = 100,
|
|
30
|
+
) -> list[{{ model_name }}]:
|
|
31
|
+
return {{ module_name }}_repository.get_items(
|
|
32
|
+
database,
|
|
33
|
+
offset=offset,
|
|
34
|
+
limit=limit,
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def get_item(
|
|
39
|
+
database: Database[dict[str, Any]],
|
|
40
|
+
item_id: str,
|
|
41
|
+
) -> {{ model_name }}:
|
|
42
|
+
item = {{ module_name }}_repository.get_item(database, item_id)
|
|
43
|
+
if item is None:
|
|
44
|
+
raise {{ model_name }}NotFoundError(item_id)
|
|
45
|
+
return item
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def patch_item(
|
|
49
|
+
database: Database[dict[str, Any]],
|
|
50
|
+
item_id: str,
|
|
51
|
+
payload: {{ model_name }}Update,
|
|
52
|
+
) -> {{ model_name }}:
|
|
53
|
+
item = {{ module_name }}_repository.patch_item(
|
|
54
|
+
database,
|
|
55
|
+
item_id,
|
|
56
|
+
payload.model_dump(exclude_unset=True),
|
|
57
|
+
)
|
|
58
|
+
if item is None:
|
|
59
|
+
raise {{ model_name }}NotFoundError(item_id)
|
|
60
|
+
return item
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def delete_item(
|
|
64
|
+
database: Database[dict[str, Any]],
|
|
65
|
+
item_id: str,
|
|
66
|
+
) -> None:
|
|
67
|
+
deleted = {{ module_name }}_repository.delete_item(database, item_id)
|
|
68
|
+
if not deleted:
|
|
69
|
+
raise {{ model_name }}NotFoundError(item_id)
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
from collections.abc import Generator
|
|
2
|
+
from typing import Any
|
|
3
|
+
|
|
4
|
+
from pymongo import MongoClient
|
|
5
|
+
from pymongo.database import Database
|
|
6
|
+
|
|
7
|
+
from app.core.config import settings
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
client: MongoClient[dict[str, Any]] = MongoClient(settings.database_url)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def get_database() -> Generator[Database[dict[str, Any]], None, None]:
|
|
14
|
+
yield client[settings.database_name]
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
from collections.abc import Generator
|
|
2
|
+
|
|
3
|
+
from sqlalchemy import create_engine
|
|
4
|
+
from sqlalchemy.orm import DeclarativeBase, Session, sessionmaker
|
|
5
|
+
|
|
6
|
+
from app.core.config import settings
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class Base(DeclarativeBase):
|
|
10
|
+
pass
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
engine = create_engine(
|
|
14
|
+
settings.database_url,
|
|
15
|
+
echo=False,
|
|
16
|
+
{% if database == "sqlite" %}
|
|
17
|
+
connect_args={"check_same_thread": False},
|
|
18
|
+
{% endif %}
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
SessionLocal = sessionmaker(
|
|
22
|
+
bind=engine,
|
|
23
|
+
autoflush=False,
|
|
24
|
+
expire_on_commit=False,
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def get_db() -> Generator[Session, None, None]:
|
|
29
|
+
db = SessionLocal()
|
|
30
|
+
try:
|
|
31
|
+
yield db
|
|
32
|
+
finally:
|
|
33
|
+
db.close()
|