feat: 初始化工程
This commit is contained in:
53
service/comment_service.py
Normal file
53
service/comment_service.py
Normal file
@@ -0,0 +1,53 @@
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from models.family import Comments
|
||||
from schemas.comment import CommentCreate, CommentUpdate
|
||||
from service.record_service import get_record_by_id
|
||||
|
||||
|
||||
def get_comment_by_id(db: Session, comment_id: int) -> Comments | None:
|
||||
result = db.execute(select(Comments).where(Comments.id == comment_id))
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
|
||||
def create_comment(db: Session, comment_dto: CommentCreate) -> bool:
|
||||
comment = Comments(**comment_dto.model_dump())
|
||||
db.add(comment)
|
||||
|
||||
record = get_record_by_id(db, comment_dto.record_id)
|
||||
if record:
|
||||
record.comment_count = (record.comment_count or 0) + 1
|
||||
|
||||
db.commit()
|
||||
db.refresh(comment)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def update_comment(db: Session, comment_id: int, comment_dto: CommentUpdate) -> bool:
|
||||
comment = get_comment_by_id(db, comment_id)
|
||||
if not comment:
|
||||
return False
|
||||
|
||||
comment.content = comment_dto.content
|
||||
db.commit()
|
||||
db.refresh(comment)
|
||||
return True
|
||||
|
||||
|
||||
def delete_comment(db: Session, comment_id: int, current_user_id: int) -> bool:
|
||||
comment = get_comment_by_id(db, comment_id)
|
||||
if not comment:
|
||||
return False
|
||||
if comment.user_id != current_user_id:
|
||||
return False
|
||||
|
||||
record = get_record_by_id(db, comment.record_id)
|
||||
if record and record.comment_count and record.comment_count > 0:
|
||||
record.comment_count -= 1
|
||||
|
||||
db.delete(comment)
|
||||
db.commit()
|
||||
|
||||
return True
|
||||
48
service/file_service.py
Normal file
48
service/file_service.py
Normal file
@@ -0,0 +1,48 @@
|
||||
from fastapi import UploadFile, File, HTTPException
|
||||
|
||||
from config.rustfs import s3
|
||||
|
||||
ALLOWED_IMAGE_TYPES = [
|
||||
# 图片
|
||||
"image/jpeg",
|
||||
"image/png",
|
||||
"image/gif",
|
||||
"image/webp",
|
||||
"image/svg+xml",
|
||||
# 视频
|
||||
"video/mp4",
|
||||
"video/mpeg",
|
||||
"video/quicktime",
|
||||
"video/x-msvideo",
|
||||
"video/webm",
|
||||
"video/x-matroska",
|
||||
"video/ogg",
|
||||
]
|
||||
|
||||
BUCKET = 'family'
|
||||
NGINX_PROXY = 'rustfs'
|
||||
|
||||
|
||||
async def upload_file(md5: str, file: UploadFile = File(...)) -> str:
|
||||
# 校验文件类型
|
||||
if file.content_type not in ALLOWED_IMAGE_TYPES:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="只允许上传图片或视频文件 (JPEG, PNG, GIF, WEBP, SVG, MP4, MOV, AVI, WEBM, MKV, OGV)"
|
||||
)
|
||||
|
||||
file_ext = file.filename.split('.')[-1]
|
||||
unique_filename = f"{md5}.{file_ext}"
|
||||
|
||||
file_content = await file.read()
|
||||
|
||||
# 上传到S3
|
||||
s3.put_object(
|
||||
Bucket=BUCKET,
|
||||
Key=unique_filename,
|
||||
Body=file_content,
|
||||
ContentType=file.content_type
|
||||
)
|
||||
|
||||
# 返回文件url
|
||||
return f"{NGINX_PROXY}/{BUCKET}/{unique_filename}"
|
||||
46
service/like_service.py
Normal file
46
service/like_service.py
Normal file
@@ -0,0 +1,46 @@
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from models.family import Likes
|
||||
from schemas.like import LikeCreate
|
||||
from service.record_service import get_record_by_id
|
||||
|
||||
|
||||
def get_like(db: Session, record_id: int, user_id: int) -> Likes | None:
|
||||
result = db.execute(
|
||||
select(Likes).where(Likes.record_id == record_id, Likes.user_id == user_id)
|
||||
)
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
|
||||
def create_like(db: Session, like_dto: LikeCreate) -> bool:
|
||||
exist = get_like(db, like_dto.record_id, like_dto.user_id)
|
||||
if exist:
|
||||
return False
|
||||
|
||||
like = Likes(**like_dto.model_dump())
|
||||
db.add(like)
|
||||
|
||||
record = get_record_by_id(db, like_dto.record_id)
|
||||
if record:
|
||||
record.like_count = (record.like_count or 0) + 1
|
||||
|
||||
db.commit()
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def delete_like(db: Session, record_id: int, user_id: int) -> bool:
|
||||
like = get_like(db, record_id, user_id)
|
||||
if not like:
|
||||
return False
|
||||
|
||||
db.delete(like)
|
||||
|
||||
record = get_record_by_id(db, record_id)
|
||||
if record and record.like_count and record.like_count > 0:
|
||||
record.like_count -= 1
|
||||
|
||||
db.commit()
|
||||
|
||||
return True
|
||||
59
service/record_service.py
Normal file
59
service/record_service.py
Normal file
@@ -0,0 +1,59 @@
|
||||
from sqlalchemy import select, desc
|
||||
from sqlalchemy.orm import Session, selectinload
|
||||
|
||||
from models.family import Records, Comments
|
||||
from schemas.record import RecordCreate, RecordUpdate, RecordResponse
|
||||
|
||||
|
||||
def get_all_records(db: Session, skip: int = 0, limit: int = 20) -> list[RecordResponse]:
|
||||
result = db.execute(
|
||||
select(Records)
|
||||
.options(
|
||||
selectinload(Records.user),
|
||||
selectinload(Records.comments).selectinload(Comments.user),
|
||||
)
|
||||
.order_by(desc(Records.create_time))
|
||||
.offset(skip)
|
||||
.limit(limit)
|
||||
)
|
||||
records = result.scalars().all()
|
||||
return [RecordResponse.model_validate(r) for r in records]
|
||||
|
||||
|
||||
def create_record(db: Session, record_dto: RecordCreate) -> bool:
|
||||
data = record_dto.model_dump()
|
||||
record = Records(**data)
|
||||
db.add(record)
|
||||
db.commit()
|
||||
db.refresh(record)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def get_record_by_id(db: Session, record_id: int) -> Records | None:
|
||||
result = db.execute(select(Records).where(Records.id == record_id))
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
|
||||
def update_record(db: Session, record_id: int, record_dto: RecordUpdate) -> bool:
|
||||
record = get_record_by_id(db, record_id)
|
||||
if not record:
|
||||
return False
|
||||
|
||||
for field, value in record_dto.model_dump(exclude_unset=True).items():
|
||||
setattr(record, field, value)
|
||||
db.commit()
|
||||
db.refresh(record)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def delete_record(db: Session, record_id: int) -> bool:
|
||||
record = get_record_by_id(db, record_id)
|
||||
if not record:
|
||||
return False
|
||||
|
||||
db.delete(record)
|
||||
db.commit()
|
||||
|
||||
return True
|
||||
43
service/user_service.py
Normal file
43
service/user_service.py
Normal file
@@ -0,0 +1,43 @@
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from models.family import Users
|
||||
from schemas.user import UserCreate, UserUpdate
|
||||
|
||||
|
||||
def get_user_by_id(db: Session, user_id: int) -> Users | None:
|
||||
result = db.execute(select(Users).where(Users.id == user_id))
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
|
||||
def create_user(db: Session, user: UserCreate) -> Users:
|
||||
user = Users(**user.model_dump())
|
||||
db.add(user)
|
||||
db.commit()
|
||||
db.refresh(user)
|
||||
|
||||
return user
|
||||
|
||||
|
||||
def update_user(db: Session, user_id: int, obj_in: UserUpdate) -> Users:
|
||||
user = get_user_by_id(db, user_id)
|
||||
if not user:
|
||||
return False
|
||||
|
||||
for field, value in obj_in.model_dump(exclude_unset=True).items():
|
||||
setattr(user, field, value)
|
||||
db.commit()
|
||||
db.refresh(user)
|
||||
|
||||
return user
|
||||
|
||||
|
||||
def delete_user(db: Session, user_id: int) -> bool:
|
||||
user = get_user_by_id(db, user_id)
|
||||
if not user:
|
||||
return False
|
||||
|
||||
db.delete(user)
|
||||
db.commit()
|
||||
|
||||
return True
|
||||
Reference in New Issue
Block a user