Skip to content

Commit

Permalink
Adding routes and controllers for reading data
Browse files Browse the repository at this point in the history
  • Loading branch information
mzkrasner committed Aug 1, 2024
1 parent c46cab6 commit 4ef67c3
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 26 deletions.
22 changes: 22 additions & 0 deletions src/controllers/readController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { getOrbisContext } from '@/utils/context.js'
import { Request, Response, NextFunction } from 'express'

const readValidations = async (_req: Request, res: Response, next: NextFunction): Promise<void> => {
try {
const { orbis, table, context } = await getOrbisContext()
const validationResults = await orbis.select().from(table).context(context).run()

res.locals = {
...res.locals,
validations: validationResults.rows.length ? validationResults.rows : null,
}
return next()
} catch (error) {
console.error(error)
next(error)
}
}

export const readController = {
readValidations,
}
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import express, { json, Request, Response, NextFunction } from 'express'
import cors from 'cors'
import writeRouter from './routes/write.js'
import router from './routes/all.js'

const app = express()
const port = process.env.PORT || 8080
Expand All @@ -22,7 +22,7 @@ const allowCrossDomain = (_req: Request, res: Response, next: NextFunction) => {

app.use(allowCrossDomain)

app.use('/', writeRouter)
app.use('/', router)

app.listen(port, () => {
console.log(`server started at http://localhost:${port}`)
Expand Down
41 changes: 41 additions & 0 deletions src/routes/all.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { RequestHandler, Request, Response, Router } from 'express'
import { writeController } from '@/controllers/writeController.js'
import { readController } from '@/controllers/readController.js'

const router: Router = Router()

type PostResponse = Response & {
locals: {
validationUpdate: unknown
streamID: string
}
}

type ReadResponse = Response & {
locals: {
validations: unknown
}
}

router.post(
'/write',
writeController.writeValidation as RequestHandler,
(_req: Request, res: PostResponse) => {
return res.json({
validationUpdate: res.locals.validationUpdate,
streamID: res.locals.streamID,
})
},
)

router.get(
'/read',
readController.readValidations as RequestHandler,
(_req: Request, res: ReadResponse) => {
return res.json({
validations: res.locals.validations,
})
},
)

export default router
24 changes: 0 additions & 24 deletions src/routes/write.ts

This file was deleted.

0 comments on commit 4ef67c3

Please sign in to comment.