Skip to content

Commit

Permalink
Merge branches 'main' and 'main' of https://github.com/Moujuruo/AI_Of…
Browse files Browse the repository at this point in the history
  • Loading branch information
Moujuruo committed Jun 7, 2024
2 parents cf035cd + 4a8bd07 commit 8527bcc
Show file tree
Hide file tree
Showing 23 changed files with 21,748 additions and 2,341 deletions.
24 changes: 22 additions & 2 deletions backend/SqliteUtil.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import json
import csv
from sqlite3 import Error
import threading

db_name = 'Ai_work'

conn = sqlite3.connect(db_name + '.db', check_same_thread=False)
lock_threading = threading.Lock()

conn = sqlite3.connect(db_name + '.db', check_same_thread=Fallse)
# conn.execute("PRAGMA foreign_keys = ON") # 启用外键支持
cursor = conn.cursor()

Expand Down Expand Up @@ -113,6 +116,7 @@ def get_user_avatar(username):

def get_user_avatar_by_id(id):
try:
lock_threading.acquire()
cursor.execute("SELECT avatar FROM users WHERE id = ?", (id,))
result = cursor.fetchone()
if result:
Expand All @@ -121,6 +125,22 @@ def get_user_avatar_by_id(id):
return {"status": 404, "message": "用户不存在"}
except Exception as e:
return {"status": 500, "message": f"数据库错误: {e}"}
finally:
lock_threading.release()

def get_user_status(id):
try:
lock_threading.acquire()
cursor.execute("SELECT status FROM users WHERE id = ?", (id,))
result = cursor.fetchone()
if result:
return {"status": 200, "message": result[0]}
else:
return {"status": 404, "message": "用户不存在"}
except Exception as e:
return {"status": 500, "message": f"数据库错误: {e}"}
finally:
lock_threading.release()

def change_user_status(userID, status):
try:
Expand Down Expand Up @@ -316,7 +336,7 @@ def getActivitiesFromData(dataList):
activity[columnName] = columnValue
activities.append(activity)
return activities

def deleteActivity(activity_id):
try:
sql = 'DELETE FROM TodoActivity WHERE ActivityID = ?'
Expand Down
3 changes: 2 additions & 1 deletion backend/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Flask_Cors==4.0.1
Flask==2.3.2
openai
arrow
arrow
qianfan
116 changes: 111 additions & 5 deletions backend/run_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import json
import SqliteUtil as DBUtil
import sqlite_roombooking as RBooking
import sqlite_note as RNote
import sqlite_team as Team
from werkzeug.utils import secure_filename
import os
Expand Down Expand Up @@ -131,11 +132,17 @@ def uploaded_file(filename):
def assets_file(filename):
return send_from_directory("../assets", filename)

@app.route(apiPrefix + 'getAvatarById', methods=['POST'])
@app.route(apiPrefix + 'getAvatarById', methods=['GET'])
def getAvatarById():
data = request.get_json()
result = DBUtil.get_user_avatar_by_id(data['userID'])
return jsonify({'status': 200, 'message': 'success', 'data': result}), 200
user_id = request.args.get('user_id')
if not user_id:
return jsonify({'status': 400, 'message': 'user_id is required'}), 400

result = DBUtil.get_user_avatar_by_id(user_id)
if result:
return jsonify({'status': 200, 'message': 'success', 'data': result}), 200
else:
return jsonify({'status': 404, 'message': 'User not found'}), 404

################## TodoActivity接口 ##################
@app.route(apiPrefix + 'updateActivity', methods=['POST'])
Expand All @@ -155,7 +162,8 @@ def insertActivity():
}
return json.dumps(re)
except Exception as e:
return json.dumps({'code': 1, 'message': str(e)}), 500
return json.dumps({'code': -4, 'message': str(e)}), 500
return json.dumps({'code': -4, 'message': str(e)}), 500

@app.route(apiPrefix + 'getActivityList/<int:job>')
def getActivityList(job):
Expand Down Expand Up @@ -397,6 +405,7 @@ def getAIResult():
############ team接口 ############
@app.route(apiPrefix + 'getAllTeams', methods=['POST'])
def getAllTeams():

data = request.get_json()
userID = data['userID']

Expand Down Expand Up @@ -430,7 +439,104 @@ def getAllTeams():
# return jsonify({'code': 0, 'message': '获取团队列表成功', 'status': 200, 'data': result}), 200
return jsonify({'code': 0, 'message': '获取团队列表成功', 'status': 200, 'data': team_list}), 200

# insertTeam
@app.route(apiPrefix + 'insertTeam', methods=['POST'])
def insertTeam():
data = request.get_json()
userID = data['userID']
teamName = data['teamName']

result = Team.insertTeam(teamName, userID)
if result is False:
return jsonify({"error": "Failed to insert team", "status": 500}), 500
return jsonify({"message": "Team created successfully", "status": 200}), 200

# inviteMember
@app.route(apiPrefix + 'inviteMember', methods=['POST'])
def inviteMember():
data = request.get_json()
teamID = data['teamID']
membername = data['member_name']
memberID = DBUtil.get_user_ID(membername)
if memberID["status"] == 404:
return jsonify({"data": "Member not found", "status": 404}), 404

member_status = DBUtil.get_user_status(memberID)
if member_status["status"] == 404:
return jsonify({"data": "Member not found", "status": 404}), 404
if member_status["message"] == 0:
return jsonify({"data": "成员不在线", "status": 400}), 400

result = Team.insertTeamInvitation(teamID, memberID["id"])
if result is False:
return jsonify({"data": "Failed to invite member", "status": 500}), 500
return jsonify({"data": "Member invited successfully", "status": 200}), 200

# getBeInvitedTeams
@app.route(apiPrefix + 'getBeInvitedTeams', methods=['POST'])
def getBeInvitedTeams():
data = request.get_json()
userID = data['userID']
result = Team.search_in_team_invitation(userID)
if result is False:
return jsonify({"data": "Failed to get team list", "status": 500}), 500
key = ['team_id', 'user_id', 'captain_id']
# captain_id 要通过result中的team_id去查询get_team_captatin
# result每个元组的第一个元素是team_id
for i in range(len(result)):
result[i] = [result[i][0], result[i][1], Team.get_team_captain(result[i][0])[0]]
result = list(map(lambda x: dict(zip(key, x)), result))
return jsonify({"data": result, "status": 200}), 200

# return jsonify({"data": result, "status": 200}), 200


################## Note接口 ##################
@app.route(apiPrefix + 'updateNote', methods=['POST'])
def insertNote():
data = request.get_json()
note_title = data.get('title')
note_content = data.get('content')
user_id = data.get('userName')

existing_note = RNote.getNoteByTitle(note_title, user_id)
if existing_note:
result = RNote.updateNote(note_title, note_content, user_id)
else:
result = RNote.insertNote(note_title, note_content, user_id)
return jsonify(result), result['status']

@app.route(apiPrefix + 'getNoteList/<user>')
def getNoteList(user):
try:
array = RNote.getNoteTitleList(user)
response = {
'status': 200,
'data': array
}
return json.dumps(response)
except Exception as e:
return json.dumps({'status': 500, 'data': None})

@app.route(apiPrefix + 'getNoteContent/<user>/<title>')
def getNoteContent(user, title):
try:
content = RNote.getNoteContent(user, title);
response = {
'status': 200,
'data': content
}
return json.dumps(response)
except Exception as e:
return json.dumps({'status': 500, 'data': None})

@app.route(apiPrefix + 'deleteNote/<user>/<title>')
def deleteNoteByTitle(user, title):
try:
RNote.deleteNoteByTitle(user, title)
return json.dumps({'status': 200, 'data': None})
except Exception as e:
return json.dumps({'status': 500, 'data': None})

if __name__ == "__main__":
app.run(debug=True, port=5001)
68 changes: 68 additions & 0 deletions backend/sqlite_note.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import hashlib
import sqlite3
import json
import csv
from sqlite3 import Error
import threading

db_name = 'Ai_work'

lock_threading = threading.Lock()
conn = sqlite3.connect(db_name + '.db', check_same_thread=False)
# conn.execute("PRAGMA foreign_keys = ON") # 启用外键支持
cursor = conn.cursor()

def createTables():
# 笔记标题,笔记内容,用户id
cursor.execute('''CREATE TABLE IF NOT EXISTS notes
(note_id INTEGER PRIMARY KEY AUTOINCREMENT,
note_title TEXT NOT NULL,
note_content TEXT,
user_id INTEGER NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id))''')
conn.commit()


createTables()

def insertNote(note_title, note_content, user_id):
try:
cursor.execute("INSERT INTO notes (note_title, note_content, user_id) VALUES (?, ?, ?)",
(note_title, note_content, user_id))
conn.commit()
return {"status": 200, "message": "添加成功"}
except Exception as e:
return {"status": 500, "message": f"数据库错误: {e}"}

def updateNote(note_title, note_content, user_id):
try:
cursor.execute("UPDATE notes SET note_content = ? WHERE note_title = ? AND user_id = ?",
(note_content, note_title, user_id))
conn.commit()
return {"status": 200, "message": "更新成功"}
except Exception as e:
return {"status": 500, "message": f"数据库错误: {e}"}

def getNoteByTitle(note_title, user_id):
cursor.execute("SELECT * FROM notes WHERE note_title = ? AND user_id = ?", (note_title, user_id))
note = cursor.fetchone()
if note is None:
return False
else:
return True

def getNoteTitleList(user_id):
cursor.execute("SELECT note_title FROM notes WHERE user_id = ?", (user_id,))
notes = cursor.fetchall()
return notes

def getNoteContent(userid, title):
cursor.execute("SELECT note_content FROM notes WHERE user_id = ? AND note_title = ?", (userid, title))
note = cursor.fetchone()
print(note)
return note

def deleteNoteByTitle(user_id, note_title):
cursor.execute("DELETE FROM notes WHERE note_title = ? AND user_id = ?", (note_title, user_id))
conn.commit()
return
24 changes: 24 additions & 0 deletions backend/sqlite_team.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def insertTeam(team_name, captain_id):
def insertTeamInvitation(team_id, member_id):
try:
lock_threading.acquire()
print(team_id, member_id)
cursor.execute('''INSERT INTO team_invitation (team_id, member_id) VALUES (?, ?)''', (team_id, member_id))
conn.commit()
return True
Expand Down Expand Up @@ -133,6 +134,17 @@ def get_team_info(team_id):
return False
finally:
lock_threading.release()

def get_team_captain(team_id):
try:
lock_threading.acquire()
cursor.execute('''SELECT captain_id FROM team WHERE id = ? ''', (team_id,))
return cursor.fetchone()
except sqlite3.Error as e:
print(e)
return False
finally:
lock_threading.release()

def get_user_teams_with_captain_flag(user_id):
try:
Expand Down Expand Up @@ -168,3 +180,15 @@ def get_team_members_with_details(team_id):
finally:
lock_threading.release()

def search_in_team_invitation(member_id):
try:
lock_threading.acquire()
cursor.execute('''SELECT * FROM team_invitation WHERE member_id = ?''', (member_id,))
return cursor.fetchall()
except sqlite3.Error as e:
print(e)
return False
finally:
lock_threading.release()


3 changes: 2 additions & 1 deletion frontend/.env
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
DANGEROUSLY_DISABLE_HOST_CHECK=true
DANGEROUSLY_DISABLE_HOST_CHECK=true
PUBLIC_URL=.
Loading

0 comments on commit 8527bcc

Please sign in to comment.