Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions alembic/versions/f89d896e9b57_作者名字.py
Original file line number Diff line number Diff line change
@@ -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 ###
21 changes: 19 additions & 2 deletions app/api/v1/endpoints/articleDB.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)):
"""
Expand Down
5 changes: 3 additions & 2 deletions app/api/v1/endpoints/note.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
29 changes: 27 additions & 2 deletions app/curd/articleDB.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand All @@ -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(
Expand All @@ -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):
"""
Expand Down
6 changes: 4 additions & 2 deletions app/curd/note.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,16 @@ 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:
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}%")) & 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()
Expand Down
2 changes: 1 addition & 1 deletion app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
7 changes: 7 additions & 0 deletions app/schemas/articleDB.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -21,6 +27,7 @@ class GetResponse(BaseModel):
url: str
create_time: datetime
update_time: datetime
author: str
file_path: str

class Config:
Expand Down
1 change: 1 addition & 0 deletions app/schemas/note.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down