Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

router: room #31

Merged
merged 4 commits into from
Oct 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 155 additions & 5 deletions routes/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -44,13 +45,162 @@ 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 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
let ranId = Math.floor(Math.random() * (roomList.length - 1)) + 1
console.log("log Function ranId: " + ranId);
let room = await Room.findOne({
where : {
id : ranId
where: {
id: ranId
}
})
return room
Expand Down