From 46eef76d01c0e46412535212a6ad38214b8273e9 Mon Sep 17 00:00:00 2001 From: Fantasy lee <129943055+Fantasylee21@users.noreply.github.com> Date: Sun, 25 May 2025 18:03:55 +0800 Subject: [PATCH 1/2] =?UTF-8?q?[feat]:=20=E5=AE=8C=E6=88=90=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...34\350\200\205\345\220\215\345\255\227.py" | 32 +++++++++++++++++++ app/api/v1/endpoints/articleDB.py | 21 ++++++++++-- app/curd/articleDB.py | 29 +++++++++++++++-- app/curd/note.py | 3 +- app/main.py | 2 +- app/schemas/articleDB.py | 7 ++++ app/schemas/note.py | 1 + 7 files changed, 89 insertions(+), 6 deletions(-) create mode 100644 "alembic/versions/f89d896e9b57_\344\275\234\350\200\205\345\220\215\345\255\227.py" diff --git "a/alembic/versions/f89d896e9b57_\344\275\234\350\200\205\345\220\215\345\255\227.py" "b/alembic/versions/f89d896e9b57_\344\275\234\350\200\205\345\220\215\345\255\227.py" new file mode 100644 index 0000000..404dded --- /dev/null +++ "b/alembic/versions/f89d896e9b57_\344\275\234\350\200\205\345\220\215\345\255\227.py" @@ -0,0 +1,32 @@ +"""作者名字 + +Revision ID: f89d896e9b57 +Revises: fd8714315ad3 +Create Date: 2025-05-25 17:48:14.924693 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = 'f89d896e9b57' +down_revision: Union[str, None] = 'fd8714315ad3' +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + """Upgrade schema.""" + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### + + +def downgrade() -> None: + """Downgrade schema.""" + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### diff --git a/app/api/v1/endpoints/articleDB.py b/app/api/v1/endpoints/articleDB.py index 9b0e030..ec212df 100644 --- a/app/api/v1/endpoints/articleDB.py +++ b/app/api/v1/endpoints/articleDB.py @@ -1,8 +1,8 @@ from fastapi import APIRouter, HTTPException, Depends, UploadFile, Form, File from sqlalchemy.ext.asyncio import AsyncSession from app.utils.get_db import get_db -from app.schemas.articleDB import UploadArticle, GetArticle, DeLArticle, GetResponse -from app.curd.articleDB import create_article_in_db, get_article_in_db, get_article_in_db_by_id, get_article_info_in_db_by_id +from app.schemas.articleDB import UploadArticle, GetArticle, DeLArticle, GetResponse, SearchArticle +from app.curd.articleDB import create_article_in_db, get_article_in_db, get_article_in_db_by_id, get_article_info_in_db_by_id, search_article_in_db from app.core.config import settings import os import uuid @@ -54,6 +54,23 @@ async def get_article(get_article: GetArticle = Depends(), db: AsyncSession = De "articles": [articles.model_dump() for articles in articles] } + +@router.get("/search", response_model=dict) +async def search_article(search_article: SearchArticle = Depends(), db: AsyncSession = Depends(get_db)): + """ + Search for an article by title. + """ + # 根据标题查询文章信息 + articles, total_count = await search_article_in_db(db=db, search_article=search_article) + return { + "pagination": { + "page": search_article.page, + "page_size": search_article.page_size, + "total_count": total_count + }, + "articles": [articles.model_dump() for articles in articles] + } + @router.get("/download/{article_id}", response_model=dict) async def download_article(article_id: int, db: AsyncSession = Depends(get_db)): """ diff --git a/app/curd/articleDB.py b/app/curd/articleDB.py index 9af3af5..b11e9f5 100644 --- a/app/curd/articleDB.py +++ b/app/curd/articleDB.py @@ -2,7 +2,7 @@ from sqlalchemy.future import select from sqlalchemy import func from app.models.model import ArticleDB -from app.schemas.articleDB import UploadArticle, GetArticle, DeLArticle, GetResponse +from app.schemas.articleDB import UploadArticle, GetArticle, DeLArticle, GetResponse, SearchArticle async def create_article_in_db(db: AsyncSession, upload_article: UploadArticle): """ @@ -22,7 +22,7 @@ async def get_article_in_db(db: AsyncSession, get_article: GetArticle): total_count = 1 articles = [articles] if articles else [] elif get_article.page and get_article.page_size: - count_result = await db.execute(select(func.count()).select_from(UploadArticle)) + count_result = await db.execute(select(func.count()).select_from(ArticleDB)) total_count = count_result.scalar() # 获取总数 # 分页查询文章 result = await db.execute( @@ -37,6 +37,31 @@ async def get_article_in_db(db: AsyncSession, get_article: GetArticle): total_count = len(articles) return [GetResponse.model_validate(article) for article in articles], total_count + +async def search_article_in_db(db: AsyncSession, search_article: SearchArticle): + """ + Search for articles by title. + """ + if search_article.author: + result = await db.execute(select(ArticleDB).where(ArticleDB.title.like(f"%{search_article.query}%"), ArticleDB.author.like(f"%{search_article.author}%"))) + articles = result.scalars().all() + total_count = len(articles) + elif search_article.page and search_article.page_size: + count_result = await db.execute(select(func.count()).select_from(ArticleDB).where(ArticleDB.title.like(f"%{search_article.query}%"))) + total_count = count_result.scalar() + # 分页查询文章 + result = await db.execute( + select(ArticleDB) + .where(ArticleDB.title.like(f"%{search_article.query}%")) + .offset((search_article.page - 1) * search_article.page_size) + .limit(search_article.page_size) + ) + articles = result.scalars().all() + else: + result = await db.execute(select(ArticleDB).where(ArticleDB.title.like(f"%{search_article.query}%"))) + articles = result.scalars().all() + total_count = len(articles) + return [GetResponse.model_validate(article) for article in articles], total_count async def get_article_in_db_by_id(db: AsyncSession, article_id: int): """ diff --git a/app/curd/note.py b/app/curd/note.py index 92f007b..3705f6b 100644 --- a/app/curd/note.py +++ b/app/curd/note.py @@ -49,7 +49,8 @@ async def find_notes_in_db(note_find: NoteFind, db: AsyncSession): stmt = stmt.where(Note.id == note_find.id) elif note_find.article_id is not None: stmt = stmt.where(Note.article_id == note_find.article_id) - + if note_find.query is not None: + stmt = stmt.where(Note.content.like(f"%{note_find.query}%") | Note.title.like(f"%{note_find.query}%")) total_count_stmt = select(func.count()).select_from(stmt) total_count_result = await db.execute(total_count_stmt) total_count = total_count_result.scalar() diff --git a/app/main.py b/app/main.py index ae95cc1..6b2cf17 100644 --- a/app/main.py +++ b/app/main.py @@ -59,5 +59,5 @@ async def log_requests(request: Request, call_next): os.makedirs(IMAGES_DIR, exist_ok=True) # 挂载静态文件目录 -app.mount("/avatar", StaticFiles(directory=AVATAR_DIR), name="avatar") +app.mount("/lhcos-data/avatar", StaticFiles(directory=AVATAR_DIR), name="avatar") app.mount("/images", StaticFiles(directory=IMAGES_DIR), name="images") \ No newline at end of file diff --git a/app/schemas/articleDB.py b/app/schemas/articleDB.py index 688188e..46de335 100644 --- a/app/schemas/articleDB.py +++ b/app/schemas/articleDB.py @@ -12,6 +12,12 @@ class GetArticle(BaseModel): page: int | None = None page_size: int | None = None +class SearchArticle(BaseModel): + query: str + author: str | None = None + page: int | None = None + page_size: int | None = None + class DeLArticle(BaseModel): id: int @@ -21,6 +27,7 @@ class GetResponse(BaseModel): url: str create_time: datetime update_time: datetime + author: str file_path: str class Config: diff --git a/app/schemas/note.py b/app/schemas/note.py index 4800eb9..2197f06 100644 --- a/app/schemas/note.py +++ b/app/schemas/note.py @@ -19,6 +19,7 @@ class NoteFind(BaseModel): article_id: int | None = None page: int | None = None page_size: int | None = None + query: str | None = None class NoteResponse(BaseModel): id: int From 9d9ff2833c7b2605dfd809e7e8c5326e466d19a2 Mon Sep 17 00:00:00 2001 From: Fantasy lee <129943055+Fantasylee21@users.noreply.github.com> Date: Sun, 25 May 2025 19:03:49 +0800 Subject: [PATCH 2/2] =?UTF-8?q?[feat]:=20=E5=A2=9E=E5=8A=A0=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=9D=A1=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/v1/endpoints/note.py | 5 +++-- app/curd/note.py | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/app/api/v1/endpoints/note.py b/app/api/v1/endpoints/note.py index 56cb98c..4d2bf52 100644 --- a/app/api/v1/endpoints/note.py +++ b/app/api/v1/endpoints/note.py @@ -32,8 +32,9 @@ async def update_note(note_id: int, content: Optional[str] = None, title: Option return {"msg": "Note updated successfully", "note_id": updated_note.id} @router.get("/get", response_model=dict) -async def get_notes(note_find: NoteFind = Depends(), db: AsyncSession = Depends(get_db)): - notes, total_count = await find_notes_in_db(note_find, db) +async def get_notes(note_find: NoteFind = Depends(), db: AsyncSession = Depends(get_db), current_user: dict = Depends(get_current_user)): + user_id = current_user["id"] + notes, total_count = await find_notes_in_db(note_find, db, user_id) return { "pagination": { "total_count": total_count, diff --git a/app/curd/note.py b/app/curd/note.py index 3705f6b..aa48945 100644 --- a/app/curd/note.py +++ b/app/curd/note.py @@ -42,7 +42,8 @@ async def update_note_in_db(note_id: int, note: NoteUpdate, db: AsyncSession): await db.refresh(existing_note) return existing_note -async def find_notes_in_db(note_find: NoteFind, db: AsyncSession): +async def find_notes_in_db(note_find: NoteFind, db: AsyncSession, user_id: int): + stmt = select(Note).where(Note.visible == True) # 只查询可见的笔记 if note_find.id is not None: @@ -50,7 +51,7 @@ async def find_notes_in_db(note_find: NoteFind, db: AsyncSession): elif note_find.article_id is not None: stmt = stmt.where(Note.article_id == note_find.article_id) if note_find.query is not None: - stmt = stmt.where(Note.content.like(f"%{note_find.query}%") | Note.title.like(f"%{note_find.query}%")) + stmt = stmt.where((Note.content.like(f"%{note_find.query}%") | Note.title.like(f"%{note_find.query}%")) & Note.creator_id == user_id) total_count_stmt = select(func.count()).select_from(stmt) total_count_result = await db.execute(total_count_stmt) total_count = total_count_result.scalar()