Skip to content

Commit

Permalink
Merge pull request #31 from EmTanIT/khoaBE
Browse files Browse the repository at this point in the history
router: room
  • Loading branch information
bentran1vn committed Oct 6, 2023
2 parents 1502fca + 8f552b5 commit 37db45d
Showing 1 changed file with 155 additions and 5 deletions.
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

0 comments on commit 37db45d

Please sign in to comment.