Skip to content

Commit

Permalink
Merge pull request #18 from Moujuruo/booking_backend
Browse files Browse the repository at this point in the history
会议室预约、看板,取消没写,会议内容没写,邀请成员没写
  • Loading branch information
Moujuruo authored Jun 3, 2024
2 parents 9235935 + 26ecc37 commit 812405a
Show file tree
Hide file tree
Showing 8 changed files with 420 additions and 121 deletions.
Binary file added Ai_work.db
Binary file not shown.
2 changes: 1 addition & 1 deletion backend/SqliteUtil.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import csv
from sqlite3 import Error

db_name = 'from_zero'
db_name = 'Ai_work'

conn = sqlite3.connect(db_name + '.db', check_same_thread=False)
# conn.execute("PRAGMA foreign_keys = ON") # 启用外键支持
Expand Down
58 changes: 58 additions & 0 deletions backend/run_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from flask_cors import CORS
import json
import SqliteUtil as DBUtil
import sqlite_roombooking as RBooking

app = Flask(__name__, template_folder='../front-end', static_folder='../front-end')
CORS(app) # 启用CORS
Expand Down Expand Up @@ -185,6 +186,63 @@ def getItemsByActivity(activity_id):
except Exception as e:
return json.dumps({'code': 1, 'message': str(e)}), 500

########## meeting_room 接口 ##########
@app.route(apiPrefix + 'getAllRooms', methods=['POST'])
def getAllRooms():
rooms = RBooking.getallrooms()
if rooms is None:
return jsonify({'code': 1, 'message': '获取会议室列表失败', 'status': 500 }), 500

keys = ['id', 'name', 'floor', 'capacity', 'equipment']
rooms_list = [dict(zip(keys, room)) for room in rooms]

return jsonify({'code': 0, 'message': '获取会议室列表成功', 'status': 200, 'data': rooms_list}), 200

@app.route(apiPrefix + 'getAllReservations', methods=['POST'])
def getAllReservations():
data = request.get_json()
print(data)

date = data.get('date')
reservations = RBooking.getallreservations(date)
if reservations is None:
return jsonify({'code': 1, 'message': '获取预约列表失败', 'status': 500 })

keys = ['id', 'room_id', 'user_id', 'start_time', 'end_time', 'date']
reservations_list = [dict(zip(keys, reservation)) for reservation in reservations]

return jsonify({'code': 0, 'message': '获取预约列表成功', 'status': 200, 'data': reservations_list}), 200

@app.route(apiPrefix + 'getRoomReservations', methods=['POST'])
def getRoomReservations():
data = request.get_json()
print(data)

room_id = data.get('room_id')
date = data.get('date')
reservations = RBooking.getallreservationsbyroom(room_id, date)
if reservations is None:
return jsonify({'code': 1, 'message': '获取会议室预约列表失败', 'status': 500 })
keys = ['id', 'room_id', 'user_id', 'start_time', 'end_time', 'date']
reservations_list = [dict(zip(keys, reservation)) for reservation in reservations]
return jsonify({'code': 0, 'message': '获取会议室预约列表成功', 'status': 200, 'data': reservations_list}), 200

@app.route(apiPrefix + 'insertReservation', methods=['POST'])
def insertReservation():
data = request.get_json()
print(data)

room_id = data.get('room_id')
user_id = data.get('user_id')
start_time = data.get('start_time')
end_time = data.get('end_time')
date = data.get('date')
reservation = RBooking.insertreservation(room_id, user_id, start_time, end_time, date)
if reservation == False:
return jsonify({'code': 1, 'message': '添加会议室预约失败', 'status': 500 })
return jsonify({'code': 0, 'message': '添加会议室预约成功', 'status': 200, 'data': reservation
}), 200


if __name__ == "__main__":
app.run(debug=True, port=5001)
98 changes: 98 additions & 0 deletions backend/sqlite_roombooking.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
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,会议室名称,会议室楼层,会议室容量,会议室信息(json)
cursor.execute('''CREATE TABLE IF NOT EXISTS meeting_room
(id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
floor INTEGER NOT NULL,
capacity INTEGER NOT NULL,
info TEXT NOT NULL)''')
# 会议室预定id,会议室id,预定人id,预定开始时间,预定结束时间,预定日期
cursor.execute('''CREATE TABLE IF NOT EXISTS booking
(id INTEGER PRIMARY KEY AUTOINCREMENT,
room_id INTEGER NOT NULL,
user_id INTEGER NOT NULL,
start_time TEXT NOT NULL,
end_time TEXT NOT NULL,
date TEXT NOT NULL,
FOREIGN KEY (room_id) REFERENCES meeting_room(id),
FOREIGN KEY (user_id) REFERENCES users(id))''')
conn.commit()

createTables()

def insertMeetingRoom(name, floor, capacity, info):
cursor.execute("INSERT INTO meeting_room (name, floor, capacity, info) VALUES (?, ?, ?, ?)",
(name, floor, capacity, info))
conn.commit()

## 测试用,先插入一些数据 ##
def insertTestData():
insertMeetingRoom("会议室1", 1, 10, '{"设备": ["投影仪", "话筒"]}')
insertMeetingRoom("会议室2", 1, 20, '{"设备": ["投影仪", "话筒", "白板"]}')
insertMeetingRoom("会议室3", 2, 15, '{"设备": ["投影仪", "话筒", "白板", "电脑"]}')

# insertTestData()

def getallrooms():
# cursor.execute("SELECT * FROM meeting_room")
# return cursor.fetchall()
try:
lock_threading.acquire()
cursor.execute("SELECT * FROM meeting_room")
return cursor.fetchall()
except sqlite3.Error as e:
print(e)
return None
finally:
lock_threading.release()

def getallreservations(date):
# cursor.execute("SELECT * FROM booking WHERE date=?", (date,))
# return cursor.fetchall()
try:
lock_threading.acquire()
cursor.execute("SELECT * FROM booking WHERE date=?", (date,))
return cursor.fetchall()
except sqlite3.Error as e:
print(e)
return None
finally:
lock_threading.release()

def getallreservationsbyroom(room_id, date):
try:
lock_threading.acquire()
cursor.execute("SELECT * FROM booking WHERE room_id=? AND date=?", (room_id, date))
return cursor.fetchall()
except sqlite3.Error as e:
print(e)
return None
finally:
lock_threading.release()

def insertreservation(room_id, user_id, start_time, end_time, date):
try:
lock_threading.acquire()
cursor.execute("INSERT INTO booking (room_id, user_id, start_time, end_time, date) VALUES (?, ?, ?, ?, ?)",
(room_id, user_id, start_time, end_time, date))
conn.commit()
return True
except sqlite3.Error as e:
print(e)
return False
finally:
lock_threading.release()
4 changes: 2 additions & 2 deletions frontend/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ const root = ReactDOM.createRoot(
document.getElementById('root') as HTMLElement
);
root.render(
<React.StrictMode>
// <React.StrictMode>
<App />
</React.StrictMode>
// </React.StrictMode>
);

// If you want to start measuring performance in your app, pass a function
Expand Down
Loading

0 comments on commit 812405a

Please sign in to comment.