返回字段名更改
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, Field, ConfigDict, field_serializer
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
@@ -20,13 +20,27 @@ class AccountUpdate(BaseModel):
|
|||||||
project_name: Optional[str] = None
|
project_name: Optional[str] = None
|
||||||
|
|
||||||
class AccountResponse(AccountBase):
|
class AccountResponse(AccountBase):
|
||||||
account_id: int = Field(alias="id")
|
account_id: int
|
||||||
created_at: datetime
|
created_at: datetime
|
||||||
updated_at: datetime
|
updated_at: datetime
|
||||||
|
|
||||||
class Config:
|
model_config = ConfigDict(
|
||||||
from_attributes = True
|
from_attributes=True
|
||||||
populate_by_name = True
|
)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_orm_account(cls, account):
|
||||||
|
"""从ORM对象创建响应对象"""
|
||||||
|
return cls(
|
||||||
|
account_id=account.id,
|
||||||
|
username=account.username,
|
||||||
|
password=account.password,
|
||||||
|
status=account.status,
|
||||||
|
today_updated=account.today_updated,
|
||||||
|
project_name=account.project_name,
|
||||||
|
created_at=account.created_at,
|
||||||
|
updated_at=account.updated_at
|
||||||
|
)
|
||||||
|
|
||||||
class AccountListRequest(BaseModel):
|
class AccountListRequest(BaseModel):
|
||||||
skip: Optional[int] = 0
|
skip: Optional[int] = 0
|
||||||
|
|||||||
@@ -1,22 +1,22 @@
|
|||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
from ..models.account import Account
|
from ..models.account import Account
|
||||||
from ..schemas.account import AccountCreate, AccountUpdate
|
from ..schemas.account import AccountCreate, AccountUpdate, AccountResponse
|
||||||
from typing import List, Optional
|
from typing import List, Optional
|
||||||
|
|
||||||
class AccountService:
|
class AccountService:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def create_account(db: Session, account_data: AccountCreate) -> Account:
|
def create_account(db: Session, account_data: AccountCreate) -> AccountResponse:
|
||||||
"""创建账号"""
|
"""创建账号"""
|
||||||
db_account = Account(**account_data.dict())
|
db_account = Account(**account_data.dict())
|
||||||
db.add(db_account)
|
db.add(db_account)
|
||||||
db.commit()
|
db.commit()
|
||||||
db.refresh(db_account)
|
db.refresh(db_account)
|
||||||
return db_account
|
return AccountResponse.from_orm_account(db_account)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def search_accounts(db: Session, account_id: Optional[int] = None,
|
def search_accounts(db: Session, account_id: Optional[int] = None,
|
||||||
username: Optional[str] = None, project_name: Optional[str] = None,
|
username: Optional[str] = None, project_name: Optional[str] = None,
|
||||||
status: Optional[int] = None, today_updated: Optional[int] = None) -> List[Account]:
|
status: Optional[int] = None, today_updated: Optional[int] = None) -> List[AccountResponse]:
|
||||||
"""根据多种条件搜索账号"""
|
"""根据多种条件搜索账号"""
|
||||||
query = db.query(Account)
|
query = db.query(Account)
|
||||||
|
|
||||||
@@ -31,12 +31,16 @@ class AccountService:
|
|||||||
if today_updated is not None:
|
if today_updated is not None:
|
||||||
query = query.filter(Account.today_updated == today_updated)
|
query = query.filter(Account.today_updated == today_updated)
|
||||||
|
|
||||||
return query.all()
|
accounts = query.all()
|
||||||
|
return [AccountResponse.from_orm_account(account) for account in accounts]
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_account(db: Session, account_id: int) -> Optional[Account]:
|
def get_account(db: Session, account_id: int) -> Optional[AccountResponse]:
|
||||||
"""根据ID获取账号"""
|
"""根据ID获取账号"""
|
||||||
return db.query(Account).filter(Account.id == account_id).first()
|
account = db.query(Account).filter(Account.id == account_id).first()
|
||||||
|
if account:
|
||||||
|
return AccountResponse.from_orm_account(account)
|
||||||
|
return None
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_account_by_username(db: Session, username: str) -> Optional[Account]:
|
def get_account_by_username(db: Session, username: str) -> Optional[Account]:
|
||||||
@@ -44,12 +48,13 @@ class AccountService:
|
|||||||
return db.query(Account).filter(Account.username == username).first()
|
return db.query(Account).filter(Account.username == username).first()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_accounts(db: Session, skip: int = 0, limit: int = 100) -> List[Account]:
|
def get_accounts(db: Session, skip: int = 0, limit: int = 100) -> List[AccountResponse]:
|
||||||
"""获取账号列表"""
|
"""获取账号列表"""
|
||||||
return db.query(Account).offset(skip).limit(limit).all()
|
accounts = db.query(Account).offset(skip).limit(limit).all()
|
||||||
|
return [AccountResponse.from_orm_account(account) for account in accounts]
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def update_account(db: Session, account_id: int, account_data: AccountUpdate) -> Optional[Account]:
|
def update_account(db: Session, account_id: int, account_data: AccountUpdate) -> Optional[AccountResponse]:
|
||||||
"""更新账号"""
|
"""更新账号"""
|
||||||
db_account = db.query(Account).filter(Account.id == account_id).first()
|
db_account = db.query(Account).filter(Account.id == account_id).first()
|
||||||
if db_account:
|
if db_account:
|
||||||
@@ -58,8 +63,8 @@ class AccountService:
|
|||||||
setattr(db_account, field, value)
|
setattr(db_account, field, value)
|
||||||
db.commit()
|
db.commit()
|
||||||
db.refresh(db_account)
|
db.refresh(db_account)
|
||||||
|
return AccountResponse.from_orm_account(db_account)
|
||||||
return db_account
|
return None
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def delete_account(db: Session, account_id: int) -> bool:
|
def delete_account(db: Session, account_id: int) -> bool:
|
||||||
|
|||||||
Reference in New Issue
Block a user