Skip to content

Commit 1b76274

Browse files
author
Pau Tena
committed
Refactor backend structure
1 parent 787b1aa commit 1b76274

29 files changed

+183
-156
lines changed

backend/Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@ test:
1010
lint:
1111
uv run bash scripts/lint.sh
1212

13-
.PHONY: test lint
13+
format:
14+
uv run bash scripts/format.sh
15+
16+
.PHONY: test lint format

backend/app/api/main.py

Lines changed: 0 additions & 14 deletions
This file was deleted.
File renamed without changes.

backend/app/auth/models.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from sqlmodel import Field, SQLModel
2+
3+
4+
# JSON payload containing access token
5+
class Token(SQLModel):
6+
access_token: str
7+
token_type: str = "bearer"
8+
9+
10+
# Contents of JWT token
11+
class TokenPayload(SQLModel):
12+
sub: str | None = None
13+
14+
15+
class NewPassword(SQLModel):
16+
token: str
17+
new_password: str = Field(min_length=8, max_length=40)

backend/app/api/routes/login.py renamed to backend/app/auth/routes.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
from fastapi.security import OAuth2PasswordRequestForm
77

88
from app import crud
9-
from app.api.deps import CurrentUser, SessionDep, get_current_active_superuser
9+
from app.auth.models import NewPassword, Token
1010
from app.core import security
1111
from app.core.config import settings
1212
from app.core.security import get_password_hash
13-
from app.models import Message, NewPassword, Token, UserPublic
14-
from app.utils import (
13+
from app.models import Message
14+
from app.users.dependencies import CurrentUser, SessionDep, get_current_active_superuser
15+
from app.users.models import UserPublic
16+
from app.utils.utils import (
1517
generate_password_reset_token,
1618
generate_reset_password_email,
1719
send_email,

backend/app/core/db.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from app import crud
44
from app.core.config import settings
5-
from app.models import User, UserCreate
5+
from app.users.models import User, UserCreate
66

77
engine = create_engine(str(settings.SQLALCHEMY_DATABASE_URI))
88

backend/app/crud.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
from sqlmodel import Session, select
55

66
from app.core.security import get_password_hash, verify_password
7-
from app.models import Item, ItemCreate, User, UserCreate, UserUpdate
7+
from app.items.models import Item, ItemCreate
8+
from app.users.models import User, UserCreate, UserUpdate
89

910

1011
def create_user(*, session: Session, user_create: UserCreate) -> User:
File renamed without changes.

backend/app/items/models.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import uuid
2+
3+
from sqlmodel import Field, Relationship, SQLModel
4+
5+
from app.users.models import User
6+
7+
8+
# Shared properties
9+
class ItemBase(SQLModel):
10+
title: str = Field(min_length=1, max_length=255)
11+
description: str | None = Field(default=None, max_length=255)
12+
13+
14+
# Properties to receive on item creation
15+
class ItemCreate(ItemBase):
16+
pass
17+
18+
19+
# Properties to receive on item update
20+
class ItemUpdate(ItemBase):
21+
title: str | None = Field(default=None, min_length=1, max_length=255) # type: ignore
22+
23+
24+
# Database model, database table inferred from class name
25+
class Item(ItemBase, table=True):
26+
id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True)
27+
title: str = Field(max_length=255)
28+
owner_id: uuid.UUID = Field(
29+
foreign_key="user.id", nullable=False, ondelete="CASCADE"
30+
)
31+
owner: User | None = Relationship(back_populates="items")
32+
33+
34+
# Properties to return via API, id is always required
35+
class ItemPublic(ItemBase):
36+
id: uuid.UUID
37+
owner_id: uuid.UUID
38+
39+
40+
class ItemsPublic(SQLModel):
41+
data: list[ItemPublic]
42+
count: int

backend/app/api/routes/items.py renamed to backend/app/items/routes.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
from fastapi import APIRouter, HTTPException
55
from sqlmodel import func, select
66

7-
from app.api.deps import CurrentUser, SessionDep
8-
from app.models import Item, ItemCreate, ItemPublic, ItemsPublic, ItemUpdate, Message
7+
from app.items.models import Item, ItemCreate, ItemPublic, ItemsPublic, ItemUpdate
8+
from app.models import Message
9+
from app.users.dependencies import CurrentUser, SessionDep
910

1011
router = APIRouter(prefix="/items", tags=["items"])
1112

0 commit comments

Comments
 (0)