Skip to content

Commit 5f1c20e

Browse files
committed
[feat]: 增加用户信息
1 parent a2ab942 commit 5f1c20e

File tree

5 files changed

+135
-8
lines changed

5 files changed

+135
-8
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""修改note
2+
3+
Revision ID: 7af566a6091b
4+
Revises: 949e5fc5dfc4
5+
Create Date: 2025-05-11 22:56:54.523911
6+
7+
"""
8+
from typing import Sequence, Union
9+
10+
from alembic import op
11+
import sqlalchemy as sa
12+
13+
14+
# revision identifiers, used by Alembic.
15+
revision: str = '7af566a6091b'
16+
down_revision: Union[str, None] = '949e5fc5dfc4'
17+
branch_labels: Union[str, Sequence[str], None] = None
18+
depends_on: Union[str, Sequence[str], None] = None
19+
20+
21+
def upgrade() -> None:
22+
"""Upgrade schema."""
23+
# ### commands auto generated by Alembic - please adjust! ###
24+
pass
25+
# ### end Alembic commands ###
26+
27+
28+
def downgrade() -> None:
29+
"""Downgrade schema."""
30+
# ### commands auto generated by Alembic - please adjust! ###
31+
pass
32+
# ### end Alembic commands ###
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""增加用户信息描述
2+
3+
Revision ID: 949e5fc5dfc4
4+
Revises: a434b17f5caf
5+
Create Date: 2025-05-11 22:31:11.511674
6+
7+
"""
8+
from typing import Sequence, Union
9+
10+
from alembic import op
11+
import sqlalchemy as sa
12+
13+
14+
# revision identifiers, used by Alembic.
15+
revision: str = '949e5fc5dfc4'
16+
down_revision: Union[str, None] = 'a434b17f5caf'
17+
branch_labels: Union[str, Sequence[str], None] = None
18+
depends_on: Union[str, Sequence[str], None] = None
19+
20+
21+
def upgrade() -> None:
22+
"""Upgrade schema."""
23+
# ### commands auto generated by Alembic - please adjust! ###
24+
op.add_column('notes', sa.Column('creator_id', sa.Integer(), nullable=True))
25+
op.create_foreign_key(None, 'notes', 'users', ['creator_id'], ['id'])
26+
# ### end Alembic commands ###
27+
28+
29+
def downgrade() -> None:
30+
"""Downgrade schema."""
31+
# ### commands auto generated by Alembic - please adjust! ###
32+
op.drop_constraint(None, 'notes', type_='foreignkey')
33+
op.drop_column('notes', 'creator_id')
34+
# ### end Alembic commands ###

app/api/v1/endpoints/note.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
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_self_notes_count_in_db, find_self_recent_notes_in_db
66
from typing import Optional
7-
7+
from app.utils.auth import get_current_user
88
router = APIRouter()
99

1010
@router.post("/create", response_model=dict)
11-
async def create_note(note: NoteCreate, db: AsyncSession = Depends(get_db)):
12-
new_note = await create_note_in_db(note, db)
11+
async def create_note(note: NoteCreate, db: AsyncSession = Depends(get_db), current_user: dict = Depends(get_current_user)):
12+
user_id = current_user["id"]
13+
new_note = await create_note_in_db(note, db, user_id)
1314
return {"msg": "Note created successfully", "note_id": new_note.id}
1415

1516
@router.delete("/{note_id}", response_model=dict)
@@ -52,3 +53,19 @@ async def get_notes_title(note_find: NoteFind = Depends(), db: AsyncSession = De
5253
},
5354
"notes": notes
5455
}
56+
57+
@router.get("/count", response_model=dict)
58+
async def get_notes_count(db: AsyncSession = Depends(get_db), current_user: dict = Depends(get_current_user)):
59+
user_id = current_user["id"]
60+
count = await find_self_notes_count_in_db(db, user_id)
61+
return {
62+
"count": count
63+
}
64+
65+
@router.get("/count/recent", response_model=dict)
66+
async def get_recent_notes_count(db: AsyncSession = Depends(get_db), current_user: dict = Depends(get_current_user)):
67+
user_id = current_user["id"]
68+
notes = await find_self_recent_notes_in_db(db, user_id)
69+
return {
70+
"notes": notes
71+
}

app/curd/note.py

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
from app.models.model import Note
66
from app.schemas.note import NoteCreate, NoteUpdate, NoteFind, NoteResponse
77

8-
async def create_note_in_db(note: NoteCreate, db: AsyncSession):
9-
new_note = Note(content=note.content, article_id=note.article_id, title=note.title)
8+
async def create_note_in_db(note: NoteCreate, db: AsyncSession, user_id: int):
9+
new_note = Note(content=note.content, article_id=note.article_id, title=note.title, creator_id=user_id)
1010
db.add(new_note)
1111
await db.commit()
1212
await db.refresh(new_note)
@@ -104,4 +104,48 @@ async def find_recent_notes_in_db(db: AsyncSession):
104104
# 格式化结果为字典列表
105105
recent_notes = [{"date": row.date, "count": row.count} for row in data]
106106

107-
return recent_notes
107+
return recent_notes
108+
109+
async def find_self_recent_notes_in_db(db: AsyncSession, user_id: int):
110+
"""
111+
返回近7天内创建的笔记的数目和对应日期
112+
"""
113+
# 获取当前日期和7天前的日期
114+
today = datetime.now().date()
115+
seven_days_ago = today - timedelta(days=6)
116+
117+
# 查询近7天内的笔记数目,按日期分组
118+
stmt = (
119+
select(
120+
cast(Note.create_time, Date).label("date"), # 按日期分组
121+
func.count(Note.id).label("count") # 统计每日期的笔记数
122+
)
123+
.where(
124+
Note.create_time >= seven_days_ago, # 筛选近7天的笔记
125+
Note.create_time <= today, # 包括今天
126+
Note.creator_id == user_id # 筛选特定用户的笔记
127+
)
128+
.group_by(cast(Note.create_time, Date)) # 按日期分组
129+
.order_by(cast(Note.create_time, Date)) # 按日期排序
130+
)
131+
132+
# 执行查询
133+
result = await db.execute(stmt)
134+
data = result.fetchall()
135+
136+
# 格式化结果为字典列表
137+
recent_notes = [{"date": row.date, "count": row.count} for row in data]
138+
139+
return recent_notes
140+
141+
async def find_self_notes_count_in_db(db: AsyncSession, user_id: int):
142+
"""
143+
返回用户的笔记数目
144+
"""
145+
stmt = (
146+
select(func.count(Note.id))
147+
.where(Note.creator_id == user_id)
148+
)
149+
result = await db.execute(stmt)
150+
count = result.scalar_one_or_none()
151+
return count

app/models/model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class Note(Base):
9393
article_id = Column(Integer, ForeignKey('articles.id'))
9494
create_time = Column(DateTime, default=func.now(), nullable=False) # 创建时间
9595
update_time = Column(DateTime, default=func.now(), onupdate=func.now(), nullable=False) # 更新时间
96-
96+
creator_id = Column(Integer, ForeignKey('users.id')) # 创建者ID
9797
visible = Column(Boolean, default=True, nullable=False) # 是否可见 False表示在回收站中
9898

9999
article = relationship('Article', back_populates='notes')

0 commit comments

Comments
 (0)