From 6fad3b8fdaa4bbcae14e74bac2e38d18fcd1116f Mon Sep 17 00:00:00 2001 From: KhoaHocMai113 Date: Thu, 5 Oct 2023 17:16:25 +0700 Subject: [PATCH 1/3] router: room --- routes/room.js | 160 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 155 insertions(+), 5 deletions(-) diff --git a/routes/room.js b/routes/room.js index 3b488ab6..1e6e1939 100644 --- a/routes/room.js +++ b/routes/room.js @@ -2,7 +2,8 @@ import express from 'express' import { DataResponse, InternalErrResponse, InvalidTypeResponse, MessageResponse, NotFoundResponse } from '../common/reponses.js' import { requireRole } from '../middlewares/auth.js' import Room from '../models/Room.js' - +import RoomLogTime from '../models/RoomLogTime.js' +import { Op } from 'sequelize' const router = express.Router() @@ -44,12 +45,161 @@ router.delete('/', async (req, res) => { } }) -export async function randomRoom(){ +router.get('/', async (req, res) => { // Get all room + const room = await Room.findAll() + res.json(DataResponse(room)) +}) + +router.get('/roomInUse', async (req, res) => { // Get all room in use + const roomId = parseInt(req.body.roomId) + + try { + const roomLogTime = await RoomLogTime.findAll({ + where: { + roomId: roomId + } + }) + res.json(DataResponse(roomLogTime)) + } catch (error) { + console.log(error) + res.json(MessageResponse('Error found')); + } +}) + +router.get('/roomFree', async (req, res) => { // Get room not in use + const roomIdInUse = [] + const roomIdNotUse = [] + + try { + const roomLogTime = await RoomLogTime.findAll() + const room = await Room.findAll() + + for (let i = 0; i < roomLogTime.length; i++) { + const index = roomIdInUse.indexOf(roomLogTime[i].roomId); + + if (index === -1) { + roomIdInUse.push(roomLogTime[i].roomId); + } + } + + room.forEach(element => { + if (!roomIdInUse.includes(element.id)) { + roomIdNotUse.push(element.id) + } + }); + + const roomNotUse = await Room.findAll({ + where: { + id: { + [Op.or]: roomIdNotUse + } + } + }) + + res.json(DataResponse(roomNotUse)) + } catch (error) { + console.log(error); + res.json(MessageResponse("Error found")) + } + +}) + +router.get('/roomFreeSlot', async (req, res) => { // Get room not in use in 1 day and slot + const { day, timeSlotId } = req.body + const roomIdInUse = [] + const roomIdNotUse = [] + + try { + const roomLogTime = await RoomLogTime.findAll({ + where: { + [Op.and]: { + day: day, + timeSlotId: timeSlotId + } + } + }) + const room = await Room.findAll() + + for (let i = 0; i < roomLogTime.length; i++) { + const index = roomIdInUse.indexOf(roomLogTime[i].roomId); + + if (index === -1) { + roomIdInUse.push(roomLogTime[i].roomId); + } + } + + room.forEach(element => { + if (!roomIdInUse.includes(element.id)) { + roomIdNotUse.push(element.id) + } + }); + + const roomNotUse = await Room.findAll({ + where: { + id: { + [Op.or]: roomIdNotUse + } + } + }) + + res.json(DataResponse(roomNotUse)) + } catch (error) { + console.log(error); + res.json(MessageResponse("Error found")) + } +}) + +router.get('/roomUseSlot', async (req, res) => { // Get room in use in 1 day and 1 slot specifically + const { day, timeSlotId } = req.body + const roomIdInUse = [] + const roomIdUseInSLot = [] + + try { + const roomLogTime = await RoomLogTime.findAll({ + where: { + [Op.and]: { + day: day, + timeSlotId: timeSlotId + } + } + }) + const room = await Room.findAll() + + for (let i = 0; i < roomLogTime.length; i++) { + const index = roomIdInUse.indexOf(roomLogTime[i].roomId); + + if (index === -1) { + roomIdInUse.push(roomLogTime[i].roomId); + } + } + + room.forEach(element => { + if (roomIdInUse.includes(element.id)) { + roomIdUseInSLot.push(element.id) + } + }); + + const roomUse = await Room.findAll({ + where: { + id: { + [Op.or]: roomIdUseInSLot + } + } + }) + + res.json(DataResponse(roomUse)) + } catch (error) { + console.log(error); + res.json(MessageResponse("Error found")) + } +}) + +export async function randomRoom() { let roomList = await Room.findAll() - let ranId = Math.floor(Math.random()*(roomList.length)) + let ranId = Math.floor(Math.random() * (roomList.length)) let room = await Room.findOne({ - where : { - id : ranId + where: { + id: ranId } }) return room From 7008fda53f220acd9b3beffa274a446f01cf2923 Mon Sep 17 00:00:00 2001 From: KhoaHocMai113 Date: Thu, 5 Oct 2023 17:32:54 +0700 Subject: [PATCH 2/3] add note --- routes/room.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routes/room.js b/routes/room.js index 1e6e1939..2ee1308b 100644 --- a/routes/room.js +++ b/routes/room.js @@ -50,7 +50,7 @@ router.get('/', async (req, res) => { // Get all room res.json(DataResponse(room)) }) -router.get('/roomInUse', async (req, res) => { // Get all room in use +router.get('/roomInUse', async (req, res) => { // Get room has been used in day + which slots const roomId = parseInt(req.body.roomId) try { From 8f552b589f73117e354de3271cd73c5c3c5e3013 Mon Sep 17 00:00:00 2001 From: KhoaHocMai113 Date: Fri, 6 Oct 2023 07:41:03 +0700 Subject: [PATCH 3/3] fix error --- routes/room.js | 150 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) diff --git a/routes/room.js b/routes/room.js index fc84af69..3d23d15d 100644 --- a/routes/room.js +++ b/routes/room.js @@ -44,6 +44,156 @@ router.delete('/', async (req, res) => { res.json(MessageResponse('Error found')); } }) + +router.get('/', async (req, res) => { // Get all room + const room = await Room.findAll() + res.json(DataResponse(room)) +}) + +router.get('/roomInUse', async (req, res) => { // Get room has been used in day + which slots + const roomId = parseInt(req.body.roomId) + + try { + const roomLogTime = await RoomLogTime.findAll({ + where: { + roomId: roomId + } + }) + res.json(DataResponse(roomLogTime)) + } catch (error) { + console.log(error) + res.json(MessageResponse('Error found')); + } +}) + +router.get('/roomFree', async (req, res) => { // Get room not in use + const roomIdInUse = [] + const roomIdNotUse = [] + + try { + const roomLogTime = await RoomLogTime.findAll() + const room = await Room.findAll() + + for (let i = 0; i < roomLogTime.length; i++) { + const index = roomIdInUse.indexOf(roomLogTime[i].roomId); + + if (index === -1) { + roomIdInUse.push(roomLogTime[i].roomId); + } + } + + room.forEach(element => { + if (!roomIdInUse.includes(element.id)) { + roomIdNotUse.push(element.id) + } + }); + + const roomNotUse = await Room.findAll({ + where: { + id: { + [Op.or]: roomIdNotUse + } + } + }) + + res.json(DataResponse(roomNotUse)) + } catch (error) { + console.log(error); + res.json(MessageResponse("Error found")) + } + +}) + +router.get('/roomFreeSlot', async (req, res) => { // Get room not in use in 1 day and slot + const { day, timeSlotId } = req.body + const roomIdInUse = [] + const roomIdNotUse = [] + + try { + const roomLogTime = await RoomLogTime.findAll({ + where: { + [Op.and]: { + day: day, + timeSlotId: timeSlotId + } + } + }) + const room = await Room.findAll() + + for (let i = 0; i < roomLogTime.length; i++) { + const index = roomIdInUse.indexOf(roomLogTime[i].roomId); + + if (index === -1) { + roomIdInUse.push(roomLogTime[i].roomId); + } + } + + room.forEach(element => { + if (!roomIdInUse.includes(element.id)) { + roomIdNotUse.push(element.id) + } + }); + + const roomNotUse = await Room.findAll({ + where: { + id: { + [Op.or]: roomIdNotUse + } + } + }) + + res.json(DataResponse(roomNotUse)) + } catch (error) { + console.log(error); + res.json(MessageResponse("Error found")) + } +}) + +router.get('/roomUseSlot', async (req, res) => { // Get room in use in 1 day and 1 slot specifically + const { day, timeSlotId } = req.body + const roomIdInUse = [] + const roomIdUseInSLot = [] + + try { + const roomLogTime = await RoomLogTime.findAll({ + where: { + [Op.and]: { + day: day, + timeSlotId: timeSlotId + } + } + }) + const room = await Room.findAll() + + for (let i = 0; i < roomLogTime.length; i++) { + const index = roomIdInUse.indexOf(roomLogTime[i].roomId); + + if (index === -1) { + roomIdInUse.push(roomLogTime[i].roomId); + } + } + + room.forEach(element => { + if (roomIdInUse.includes(element.id)) { + roomIdUseInSLot.push(element.id) + } + }); + + const roomUse = await Room.findAll({ + where: { + id: { + [Op.or]: roomIdUseInSLot + } + } + }) + + res.json(DataResponse(roomUse)) + } catch (error) { + console.log(error); + res.json(MessageResponse("Error found")) + } +}) + export async function randomRoom() { let roomList = await Room.findAll() let ranId = Math.floor(Math.random() * (roomList.length - 1)) + 1