Skip to content

Commit

Permalink
Create room controller
Browse files Browse the repository at this point in the history
  • Loading branch information
jonfelixrico committed Apr 6, 2024
1 parent 4fb3732 commit eb1848b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 50 deletions.
52 changes: 2 additions & 50 deletions server/src/controllers/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Express } from 'express'

import roomService from '@/services/room.service'
import manifest from '@manifest'
import { initRoomControllers } from '@/controllers/room.controller'

export function initRestControllers(app: Express) {
app.get('/', (_, res) => {
Expand All @@ -10,53 +10,5 @@ export function initRestControllers(app: Express) {
})
})

app.post('/room', (_, res) => {
const room = roomService.createRoom()

res.json({
id: room.roomId,
})
})

app.get('/room/:roomId', (req, res) => {
const { roomId } = req.params

const room = roomService.getRoom(roomId)
if (!room) {
return res.sendStatus(404)
}

res.json({
id: roomId,
name: room.name,
})
})

app.get('/room/:roomId/event/length', (req, res) => {
const { roomId } = req.params

const room = roomService.getRoom(roomId)
if (!room) {
return res.sendStatus(404)
}

res.json({
length: room.history.length,
})
})

app.get('/room/:roomId/event', (req, res) => {
const { roomId } = req.params

const room = roomService.getRoom(roomId)
if (!room) {
return res.sendStatus(404)
}

const { start, end } = req.query

res.json(
room.history.slice(parseInt(start as string), parseInt(end as string))
)
})
initRoomControllers(app)
}
55 changes: 55 additions & 0 deletions server/src/controllers/room.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Express } from 'express'

import roomService from '@/services/room.service'

export function initRoomControllers(app: Express) {
app.post('/room', (_, res) => {
const room = roomService.createRoom()

res.json({
id: room.roomId,
})
})

app.get('/room/:roomId', (req, res) => {
const { roomId } = req.params

const room = roomService.getRoom(roomId)
if (!room) {
return res.sendStatus(404)
}

res.json({
id: roomId,
name: room.name,
})
})

app.get('/room/:roomId/event/length', (req, res) => {
const { roomId } = req.params

const room = roomService.getRoom(roomId)
if (!room) {
return res.sendStatus(404)
}

res.json({
length: room.history.length,
})
})

app.get('/room/:roomId/event', (req, res) => {
const { roomId } = req.params

const room = roomService.getRoom(roomId)
if (!room) {
return res.sendStatus(404)
}

const { start, end } = req.query

res.json(
room.history.slice(parseInt(start as string), parseInt(end as string))
)
})
}

0 comments on commit eb1848b

Please sign in to comment.