""" 投递条件模板路由:增删改查 + 套用 """ from typing import Optional from fastapi import APIRouter, Depends, HTTPException from sqlalchemy.orm import Session from ..auth import get_current_user from ..database import get_db from ..models import ApplyTemplate, User from ..schemas import TemplateIn, TemplateOut router = APIRouter(prefix="/api/templates", tags=["投递模板"]) def _template_out(t: ApplyTemplate) -> dict: return { "id": t.id, "name": t.name, "keywords": t.keywords, "city": t.city, "salary_min": t.salary_min, "salary_max": t.salary_max, "min_score": t.min_score, "daily_limit": t.daily_limit, "boss_online_only": t.boss_online_only, "allow_headhunter": t.allow_headhunter, "prompt": t.prompt, "created_at": t.created_at, "updated_at": t.updated_at, } @router.get("", response_model=list[TemplateOut]) def list_templates( current_user: User = Depends(get_current_user), db: Session = Depends(get_db), ): """获取当前用户的所有投递模板(按更新时间倒序)""" rows = db.query(ApplyTemplate).filter( ApplyTemplate.user_id == current_user.id ).order_by(ApplyTemplate.updated_at.desc()).all() return rows @router.post("", response_model=TemplateOut) def create_template( req: TemplateIn, current_user: User = Depends(get_current_user), db: Session = Depends(get_db), ): """创建投递模板""" t = ApplyTemplate( user_id=current_user.id, name=req.name, keywords=req.keywords, city=req.city, salary_min=req.salary_min, salary_max=req.salary_max, min_score=req.min_score, daily_limit=req.daily_limit, boss_online_only=req.boss_online_only, allow_headhunter=req.allow_headhunter, prompt=req.prompt, ) db.add(t) db.commit() db.refresh(t) return t @router.get("/{template_id}", response_model=TemplateOut) def get_template( template_id: int, current_user: User = Depends(get_current_user), db: Session = Depends(get_db), ): """获取单个模板详情""" t = db.query(ApplyTemplate).filter( ApplyTemplate.id == template_id, ApplyTemplate.user_id == current_user.id, ).first() if not t: raise HTTPException(status_code=404, detail="模板不存在") return t @router.put("/{template_id}", response_model=TemplateOut) def update_template( template_id: int, req: TemplateIn, current_user: User = Depends(get_current_user), db: Session = Depends(get_db), ): """更新投递模板""" t = db.query(ApplyTemplate).filter( ApplyTemplate.id == template_id, ApplyTemplate.user_id == current_user.id, ).first() if not t: raise HTTPException(status_code=404, detail="模板不存在") t.name = req.name t.keywords = req.keywords t.city = req.city t.salary_min = req.salary_min t.salary_max = req.salary_max t.min_score = req.min_score t.daily_limit = req.daily_limit t.boss_online_only = req.boss_online_only t.allow_headhunter = req.allow_headhunter t.prompt = req.prompt db.commit() db.refresh(t) return t @router.delete("/{template_id}") def delete_template( template_id: int, current_user: User = Depends(get_current_user), db: Session = Depends(get_db), ): """删除投递模板""" t = db.query(ApplyTemplate).filter( ApplyTemplate.id == template_id, ApplyTemplate.user_id == current_user.id, ).first() if not t: raise HTTPException(status_code=404, detail="模板不存在") db.delete(t) db.commit() return {"ok": True}