Skip to content

Commit f6a760d

Browse files
committed
♻️ refactor(model): 调整模型文件结构及引用路径
- 将用户和条目模型文件重命名为user_model.py与items_model.py - 更新所有相关模块的模型引用路径以匹配新文件结构 - 移除旧的users.py和items.py模型文件 - 在服务层和测试用例中同步更新模型类导入路径
1 parent 2cb47d3 commit f6a760d

File tree

17 files changed

+154
-242
lines changed

17 files changed

+154
-242
lines changed

backend/app/api/routes/items.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66

77
from app.api.deps import CurrentUser, SessionDep
8-
from app.model.items import ItemCreate, ItemPublic, ItemsPublic, ItemUpdate
9-
from app.model.users import Message
8+
from backend.app.model.items_model import ItemCreate, ItemPublic, ItemsPublic, ItemUpdate
9+
from backend.app.model.user_model import Message
1010
from app.service.item_service import ItemService
1111

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

backend/app/api/routes/login.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
from app.core import security
1010
from app.core.config import settings
1111
from app.core.security import get_password_hash
12-
from app.model.users import UserPublic
12+
from backend.app.model.user_model import UserPublic
1313
from app.models import Message, NewPassword, Token
14-
from app.model.users import UserPublic
14+
from backend.app.model.user_model import UserPublic
1515
from app.service.user_service import UserService
1616
from app.utils import (
1717
generate_password_reset_token,

backend/app/api/routes/private.py

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

66
from app.api.deps import SessionDep
77
from app.core.security import get_password_hash
8-
from app.model.users import User, UserPublic
8+
from backend.app.model.user_model import User, UserPublic
99

1010
router = APIRouter(tags=["private"], prefix="/private")
1111

backend/app/api/routes/users.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
get_current_active_superuser,
1010
)
1111
from app.core.config import settings
12-
from app.model.users import (
12+
from backend.app.model.user_model import (
1313
UpdatePassword,
1414
UserCreate,
1515
UserPublic,

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

44
from app.core.config import settings
5-
from app.model.users import User, UserCreate
5+
from backend.app.model.user_model import User, UserCreate
66
from app.service.user_service import UserService
77

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

backend/app/model/items.py

Lines changed: 0 additions & 98 deletions
This file was deleted.

backend/app/model/items_model.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import uuid
2+
from typing import Any
3+
4+
from sqlalchemy import func
5+
from sqlmodel import Session, select
6+
7+
from app.core.security import get_password_hash
8+
from app.types import User, UserCreate, UserUpdate
9+
10+
11+
class UserModel:
12+
def __init__(self, session: Session):
13+
self.session = session
14+
15+
def create(cls, user_create: UserCreate) -> "User":
16+
db_obj = cls.model_validate(
17+
user_create,
18+
update={"hashed_password": get_password_hash(user_create.password)},
19+
)
20+
cls.session.add(db_obj)
21+
cls.session.commit()
22+
cls.session.refresh(db_obj)
23+
return db_obj
24+
25+
def update(cls, db_user: "User", user_in: UserUpdate) -> Any:
26+
user_data = user_in.model_dump(exclude_unset=True)
27+
extra_data = {}
28+
if "password" in user_data:
29+
password = user_data["password"]
30+
hashed_password = get_password_hash(password)
31+
extra_data["hashed_password"] = hashed_password
32+
db_user.sqlmodel_update(user_data, update=extra_data)
33+
cls.session.add(db_user)
34+
cls.session.commit()
35+
cls.session.refresh(db_user)
36+
return db_user
37+
38+
def get_by_email(cls, email: str) -> "User | None":
39+
statement = select(cls.__class__).where(cls.__class__.email == email)
40+
return cls.session.exec(statement).first()
41+
42+
def get_by_id(cls, user_id: str) -> "User | None":
43+
statement = select(cls.__class__).where(cls.__class__.id == uuid.UUID(user_id))
44+
return cls.session.exec(statement).first()
45+
46+
def get_users(cls, skip: int = 0, limit: int = 100) -> dict:
47+
count_statement = select(func.count()).select_from(cls.__class__)
48+
count = cls.session.exec(count_statement).one()
49+
statement = select(cls.__class__).offset(skip).limit(limit)
50+
users = cls.session.exec(statement).all()
51+
return {"data": users, "count": count}
52+
53+
def delete_user(cls, user_id: str) -> None:
54+
user = cls.get_by_id(user_id)
55+
if user:
56+
cls.session.delete(user)
57+
cls.session.commit()

backend/app/model/user_model.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import uuid
2+
from typing import Any
3+
4+
from sqlalchemy import func
5+
from sqlmodel import Session, select
6+
7+
from app.core.security import get_password_hash
8+
from app.models import User, UserCreate, UserUpdate
9+
10+
11+
class UserModel:
12+
def __init__(self, session: Session):
13+
self.session = session
14+
15+
def create(cls, user_create: UserCreate) -> "User":
16+
db_obj = cls.model_validate(
17+
user_create,
18+
update={"hashed_password": get_password_hash(user_create.password)},
19+
)
20+
cls.session.add(db_obj)
21+
cls.session.commit()
22+
cls.session.refresh(db_obj)
23+
return db_obj
24+
25+
def update(cls, db_user: "User", user_in: UserUpdate) -> Any:
26+
user_data = user_in.model_dump(exclude_unset=True)
27+
extra_data = {}
28+
if "password" in user_data:
29+
password = user_data["password"]
30+
hashed_password = get_password_hash(password)
31+
extra_data["hashed_password"] = hashed_password
32+
db_user.sqlmodel_update(user_data, update=extra_data)
33+
cls.session.add(db_user)
34+
cls.session.commit()
35+
cls.session.refresh(db_user)
36+
return db_user
37+
38+
def get_by_email(cls, email: str) -> "User | None":
39+
statement = select(cls.__class__).where(cls.__class__.email == email)
40+
return cls.session.exec(statement).first()
41+
42+
def get_by_id(cls, user_id: str) -> "User | None":
43+
statement = select(cls.__class__).where(cls.__class__.id == uuid.UUID(user_id))
44+
return cls.session.exec(statement).first()
45+
46+
def get_users(cls, skip: int = 0, limit: int = 100) -> dict:
47+
count_statement = select(func.count()).select_from(cls.__class__)
48+
count = cls.session.exec(count_statement).one()
49+
statement = select(cls.__class__).offset(skip).limit(limit)
50+
users = cls.session.exec(statement).all()
51+
return {"data": users, "count": count}
52+
53+
def delete_user(cls, user_id: str) -> None:
54+
user = cls.get_by_id(user_id)
55+
if user:
56+
cls.session.delete(user)
57+
cls.session.commit()

backend/app/model/users.py

Lines changed: 0 additions & 105 deletions
This file was deleted.

backend/app/service/item_service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from fastapi import HTTPException
55
from sqlmodel import Session
66

7-
from app.model.items import Item, ItemCreate, ItemUpdate
7+
from backend.app.model.items_model import Item, ItemCreate, ItemUpdate
88
from app.models import Message
99

1010
class ItemService:

0 commit comments

Comments
 (0)