Skip to content

Commit 3beb80b

Browse files
authored
Merge pull request #37 from BUAA-SE-coders007/feature/note_time
Feature/note time
2 parents 61dbd95 + 152de9c commit 3beb80b

File tree

5 files changed

+57
-5
lines changed

5 files changed

+57
-5
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
env
22
__pycache__
3-
articles
3+
articles
4+
app.log

app/api/v1/endpoints/note.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from sqlalchemy.ext.asyncio import AsyncSession
33
from app.schemas.note import NoteCreate, NoteUpdate, NoteFind
44
from app.utils.get_db import get_db
5-
from app.curd.note import create_note_in_db, delete_note_in_db, update_note_in_db, find_notes_in_db, find_notes_title_in_db
5+
from app.curd.note import create_note_in_db, delete_note_in_db, update_note_in_db, find_notes_in_db, find_notes_title_in_db, find_recent_notes_in_db
66
from typing import Optional
77

88
router = APIRouter()
@@ -51,4 +51,12 @@ async def get_notes_title(note_find: NoteFind = Depends(), db: AsyncSession = De
5151
"page_size": note_find.page_size
5252
},
5353
"notes": notes
54+
}
55+
56+
57+
@router.get("/recent", response_model=dict)
58+
async def get_recent_notes(db: AsyncSession = Depends(get_db)):
59+
notes = await find_recent_notes_in_db(db)
60+
return {
61+
"notes": notes
5462
}

app/curd/note.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from sqlalchemy.ext.asyncio import AsyncSession
22
from sqlalchemy.future import select
3-
from sqlalchemy import func
3+
from sqlalchemy import func, cast, Date
4+
from datetime import datetime, timedelta
45
from app.models.model import Note
56
from app.schemas.note import NoteCreate, NoteUpdate, NoteFind, NoteResponse
67

@@ -73,3 +74,34 @@ async def find_notes_title_in_db(note_find: NoteFind, db: AsyncSession):
7374
result = await db.execute(stmt)
7475
notes = [row[0] for row in result.fetchall()]
7576
return notes, total_count
77+
78+
async def find_recent_notes_in_db(db: AsyncSession):
79+
"""
80+
返回近7天内创建的笔记的数目和对应日期
81+
"""
82+
# 获取当前日期和7天前的日期
83+
today = datetime.now().date()
84+
seven_days_ago = today - timedelta(days=6)
85+
86+
# 查询近7天内的笔记数目,按日期分组
87+
stmt = (
88+
select(
89+
cast(Note.create_time, Date).label("date"), # 按日期分组
90+
func.count(Note.id).label("count") # 统计每日期的笔记数
91+
)
92+
.where(
93+
Note.create_time >= seven_days_ago, # 筛选近7天的笔记
94+
Note.create_time <= today # 包括今天
95+
)
96+
.group_by(cast(Note.create_time, Date)) # 按日期分组
97+
.order_by(cast(Note.create_time, Date)) # 按日期排序
98+
)
99+
100+
# 执行查询
101+
result = await db.execute(stmt)
102+
data = result.fetchall()
103+
104+
# 格式化结果为字典列表
105+
recent_notes = [{"date": row.date, "count": row.count} for row in data]
106+
107+
return recent_notes

app/main.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
from fastapi import FastAPI
1+
from fastapi import FastAPI, Request
22
from app.routers.router import include_routers
33
from fastapi_pagination import add_pagination
4+
from loguru import logger
45

56
app = FastAPI()
67

@@ -16,4 +17,14 @@ def read_item(item_id: int, q: str = None):
1617
include_routers(app)
1718

1819
# 注册分页功能
19-
add_pagination(app)
20+
add_pagination(app)
21+
22+
# 设置日志配置
23+
logger.add("app.log", rotation="1 MB", retention="7 days", level="INFO")
24+
25+
@app.middleware("http")
26+
async def log_requests(request: Request, call_next):
27+
logger.info(f"Request: {request.method} {request.url}")
28+
response = await call_next(request)
29+
logger.info(f"Response status: {response.status_code}")
30+
return response

requirements.txt

-345 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)