网站app封装怎么做,怎么把网站上传到域名,南通网站建设入门,织梦网站源码见#xff1a;fastapi_study_road-learning_system_online_courses: fastapi框架实战之--在线课程学习系统 之前我们分享了FastAPI#xff08;七十一#xff09;实战开发《在线课程学习系统》接口开发-- 查看留言#xff0c;这次我们分享留言列表开发。
获…源码见fastapi_study_road-learning_system_online_courses: fastapi框架实战之--在线课程学习系统 之前我们分享了FastAPI七十一实战开发《在线课程学习系统》接口开发-- 查看留言这次我们分享留言列表开发。
获取列表也需要登录根据登录用户来获取对应的留言逻辑梳理如下 1.判断用户是否登录 2.根据登录用户查询留言列表 3.留言列表中要根据是留言回复进行列表重组 首先在message_method.py中实现主要逻辑
def get_msg_list(db: Session, uid: int):# 查询留言回复return db.query(Message).filter(or_(Message.send_user uid, Message.accept_user uid)).filter(Message.status 0).all()def list_msg_method(user: UsernameRole, db: Session):获取留言列表db_user get_by_username(db, user.username)msg_list get_msg_list(db, db_user.id) # 获取与当前用户相关留言/回复to_client []main_msg_id_list []if msg_list:for _ in msg_list:# ① 如果是留言if _.pid is None:message_one MessageOne(id_.id,send_userget_by_uid(db, _.send_user).username,accept_userget_by_uid(db, _.accept_user).username,read_.read,send_time_.send_time,add_timestr(_.add_time),context_.context)main_msg_id_list.append(_.id) # 保存非回复信息的id即留言信息的id# 查询该留言信息的所有回复信息,并将回复信息一并返回all_pid_messages get_pid_message(db, _.id)if all_pid_messages:to_client_pid_msgs []for _ in all_pid_messages:message_pid MessagePid(id_.id,send_userget_by_uid(db, _.send_user).username,accept_userget_by_uid(db, _.accept_user).username,read_.read,send_time_.send_time,add_timestr(_.add_time),context_.context,pid_.pid)to_client_pid_msgs.append(message_pid.dict())message_one.pid to_client_pid_msgsto_client.append(message_one.dict())# ② 如果是回复,查询该回复信息是否有主留言信息如果有同上else:if _.pid not in main_msg_id_list:_msg get_msg_by_id(db, _.pid)if _msg:_all_pid_messages get_pid_message(db, _msg.id)_message_one MessageOne(id_msg.id,send_userget_by_uid(db, _msg.send_user).username,accept_userget_by_uid(db, _msg.accept_user).username,read_msg.read,send_time_msg.send_time,add_timestr(_msg.add_time),context_msg.context)if _all_pid_messages:_pid_list []for each in _all_pid_messages:_message_pid MessagePid(ideach.id,send_userget_by_uid(db, each.send_user).username,accept_userget_by_uid(db, each.accept_user).username,readeach.read,send_timeeach.send_time,add_timestr(each.acc_time),contexteach.context,pideach.pid)_pid_list.append(_message_pid.dict())_message_one.pid _pid_listto_client.append(_message_one.dict())return response(datato_client)然后在message.py中实现接口
message_router.get(/list, summary查询留言列表)
def view_message_list(user: UsernameRole Depends(get_current_user),db: Session Depends(create_db)
):return list_msg_method(user, db)
测试 查询留言列表完成