deliveryman-api/app/api/endpoints/address.py
2025-01-02 18:11:01 +08:00

129 lines
3.9 KiB
Python

from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session
from sqlalchemy import and_
from typing import List
from app.models.address import AddressDB, AddressCreate, AddressUpdate, AddressInfo
from app.models.database import get_db
from app.api.deps import get_current_user
from app.models.user import UserDB
from app.core.response import success_response, error_response, ResponseModel
router = APIRouter()
@router.post("/", response_model=ResponseModel)
async def create_address(
address: AddressCreate,
db: Session = Depends(get_db),
current_user: UserDB = Depends(get_current_user)
):
"""创建配送地址"""
if address.is_default:
db.query(AddressDB).filter(
and_(
AddressDB.user_id == current_user.userid,
AddressDB.is_default == True
)
).update({"is_default": False})
db_address = AddressDB(
user_id=current_user.userid,
**address.model_dump()
)
db.add(db_address)
db.commit()
db.refresh(db_address)
return success_response(data=AddressInfo.model_validate(db_address))
@router.get("/", response_model=ResponseModel)
async def get_addresses(
db: Session = Depends(get_db),
current_user: UserDB = Depends(get_current_user)
):
"""获取用户的所有配送地址"""
addresses = db.query(AddressDB).filter(
AddressDB.user_id == current_user.userid
).all()
return success_response(data=[AddressInfo.model_validate(a) for a in addresses])
@router.put("/{address_id}", response_model=ResponseModel)
async def update_address(
address_id: int,
address: AddressUpdate,
db: Session = Depends(get_db),
current_user: UserDB = Depends(get_current_user)
):
"""更新配送地址"""
db_address = db.query(AddressDB).filter(
and_(
AddressDB.id == address_id,
AddressDB.user_id == current_user.userid
)
).first()
if not db_address:
return error_response(code=404, message="地址不存在")
update_data = address.model_dump(exclude_unset=True)
if update_data.get("is_default"):
db.query(AddressDB).filter(
and_(
AddressDB.user_id == current_user.userid,
AddressDB.is_default == True
)
).update({"is_default": False})
for key, value in update_data.items():
setattr(db_address, key, value)
db.commit()
db.refresh(db_address)
return success_response(data=AddressInfo.model_validate(db_address))
@router.delete("/{address_id}", response_model=ResponseModel)
async def delete_address(
address_id: int,
db: Session = Depends(get_db),
current_user: UserDB = Depends(get_current_user)
):
"""删除配送地址"""
result = db.query(AddressDB).filter(
and_(
AddressDB.id == address_id,
AddressDB.user_id == current_user.userid
)
).delete()
if not result:
return error_response(code=404, message="地址不存在")
db.commit()
return success_response(message="地址已删除")
@router.post("/{address_id}/set-default", response_model=ResponseModel)
async def set_default_address(
address_id: int,
db: Session = Depends(get_db),
current_user: UserDB = Depends(get_current_user)
):
"""设置默认地址"""
db.query(AddressDB).filter(
and_(
AddressDB.user_id == current_user.userid,
AddressDB.is_default == True
)
).update({"is_default": False})
db_address = db.query(AddressDB).filter(
and_(
AddressDB.id == address_id,
AddressDB.user_id == current_user.userid
)
).first()
if not db_address:
return error_response(code=404, message="地址不存在")
db_address.is_default = True
db.commit()
db.refresh(db_address)
return success_response(data=AddressInfo.model_validate(db_address))