diff --git a/__pycache__/main.cpython-310.pyc b/__pycache__/main.cpython-310.pyc index 8271456..ec99461 100644 Binary files a/__pycache__/main.cpython-310.pyc and b/__pycache__/main.cpython-310.pyc differ diff --git a/internal/__pycache__/database.cpython-310.pyc b/internal/__pycache__/database.cpython-310.pyc index 34b51f4..c61ba96 100644 Binary files a/internal/__pycache__/database.cpython-310.pyc and b/internal/__pycache__/database.cpython-310.pyc differ diff --git a/internal/__pycache__/models.cpython-310.pyc b/internal/__pycache__/models.cpython-310.pyc index ee7717d..4a6fcde 100644 Binary files a/internal/__pycache__/models.cpython-310.pyc and b/internal/__pycache__/models.cpython-310.pyc differ diff --git a/internal/database.py b/internal/database.py index 298451e..81983df 100644 --- a/internal/database.py +++ b/internal/database.py @@ -4,7 +4,7 @@ DB_CONFIG = { "host": "111.229.38.129", "user": "root", "password": "zl981023", - "database": "test", + "database": "blogapi", "charset": "utf8mb4", "cursorclass": pymysql.cursors.DictCursor, } diff --git a/internal/models.py b/internal/models.py index f9a90bb..860aed4 100644 --- a/internal/models.py +++ b/internal/models.py @@ -1,6 +1,6 @@ from pydantic import BaseModel,Field from typing import Annotated - +from datetime import datetime # Token相关的模型 class Token(BaseModel): access_token: str @@ -32,3 +32,33 @@ class User(BaseModel): class UserInDB(User): hashed_password: str = None + +class BlogList(BaseModel): + blogname:Annotated[str,Field( + title="博客名", + examples=['blogname'], + pattern=r'^.{2,50}$', + description="允许2-50个字符" + )] + blogtype:Annotated[str,Field( + title="博客类型", + examples=['blogtype'], + description="博客类型允许为空" + )] + addtime:Annotated[datetime,Field( + title="发布时间", + description="数据库中提供了默认值" + )] + descr:Annotated[str,Field( + title="备注", + examples=['descr'], + description="备注允许为空" + )] + +class BlogAdd(BlogList): + blogcontent:Annotated[str,Field( + title="博客内容", + examples=['blogcontent'], + pattern=r'^.{2,}$', + description="不能为空,允许2个或更多字符" + )] \ No newline at end of file diff --git a/main.py b/main.py index f634ae6..8ae1d12 100644 --- a/main.py +++ b/main.py @@ -4,8 +4,12 @@ from fastapi import Depends, FastAPI, HTTPException, status from dependencies import * from internal.models import Token from fastapi.middleware.cors import CORSMiddleware +from routers import usermanage,typemanage +from internal.models import BlogList +from typing import List app=FastAPI() - +# app.include_router(usermanage.router) +# app.include_router(typemanage.router) app.add_middleware( CORSMiddleware, allow_origins=['http://localhost:5173'], # 允许的源 @@ -57,3 +61,9 @@ async def read_users_me(current_user: User = Depends(get_current_active_user)): @app.get("/users/me/items/") async def read_own_items(current_user: User = Depends(get_current_active_user)): return [{"item_id": "Foo", "owner": current_user.username}] + +@app.get("/list",response_model=List[BlogList]) +def read_type_all(): + typelist="SELECT blogname,blogtype,addtime,descr FROM blogs" + result=execute_query(typelist,fetchall=True) + return result \ No newline at end of file diff --git a/routers/__pycache__/__init__.cpython-310.pyc b/routers/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000..8cb2262 Binary files /dev/null and b/routers/__pycache__/__init__.cpython-310.pyc differ diff --git a/routers/__pycache__/typemanage.cpython-310.pyc b/routers/__pycache__/typemanage.cpython-310.pyc new file mode 100644 index 0000000..6e1f47d Binary files /dev/null and b/routers/__pycache__/typemanage.cpython-310.pyc differ diff --git a/routers/__pycache__/usermanage.cpython-310.pyc b/routers/__pycache__/usermanage.cpython-310.pyc new file mode 100644 index 0000000..2806411 Binary files /dev/null and b/routers/__pycache__/usermanage.cpython-310.pyc differ diff --git a/routers/__pycache__/users.cpython-310.pyc b/routers/__pycache__/users.cpython-310.pyc new file mode 100644 index 0000000..d0d4c59 Binary files /dev/null and b/routers/__pycache__/users.cpython-310.pyc differ diff --git a/routers/typemanage.py b/routers/typemanage.py new file mode 100644 index 0000000..4368017 --- /dev/null +++ b/routers/typemanage.py @@ -0,0 +1,13 @@ +from fastapi import APIRouter,Depends +from internal.models import* +from dependencies import get_current_active_user,execute_query +router=APIRouter( + prefix="/type", + tags=["分类管理"] +) + +@router.get("/list",response_model=BlogList) +def read_type_all(type:BlogList,_: User = Depends(get_current_active_user)): + typelist="SELECT blogname,blogtype,addtime,descr FROM blogs" + execute_query(typelist) + return type \ No newline at end of file diff --git a/routers/usermanage.py b/routers/usermanage.py new file mode 100644 index 0000000..aa4ecb1 --- /dev/null +++ b/routers/usermanage.py @@ -0,0 +1,30 @@ +from fastapi import APIRouter,Depends +from fastapi.security import OAuth2PasswordRequestForm +from datetime import timedelta +from fastapi.security import OAuth2PasswordRequestForm +from fastapi import Depends, FastAPI, HTTPException, status +from dependencies import * +from internal.models import Token +from fastapi.middleware.cors import CORSMiddleware +router=APIRouter( + prefix="/users", + tags=["用户管理"] +) + +# 用户登录 +@router.post("/token", response_model=Token) +async def login_for_access_token( + form_data: OAuth2PasswordRequestForm = Depends(), +) -> Token: + user = authenticate_user(form_data.username, form_data.password) + if not user: + raise HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail="Incorrect username or password", + headers={"WWW-Authenticate": "Bearer"}, + ) + access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES) + access_token = create_access_token( + data={"sub": user.username}, expires_delta=access_token_expires + ) + return {"access_token": access_token, "token_type": "bearer"} \ No newline at end of file