-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.ts
More file actions
92 lines (77 loc) · 2.35 KB
/
app.ts
File metadata and controls
92 lines (77 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { client } from './src/lib/dynamoClient'
import {
QueryCommand,
QueryCommandOutput,
ScanCommand,
} from '@aws-sdk/client-dynamodb'
import { marshall, unmarshall } from '@aws-sdk/util-dynamodb'
import express, { Request, Response } from 'express'
import path from 'path'
const logger = require('morgan')
const cookieParser = require('cookie-parser')
const bodyParser = require('body-parser')
export const app = express()
app.listen(3221, () => console.log('Cars API listening on port 3221!'))
app.use(logger('dev'))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(cookieParser())
app.set('views', path.join(__dirname, 'src', 'views'))
app.set('view engine', 'jade')
app.get('/', function (_: Request, res: Response) {
res.send({ title: 'API Entry Point' })
})
app.get('/error', function (_: Request, res: Response) {
res.render('error')
})
app.get('/mail', async function (_: Request, res: Response) {
const params = {
TableName: 'vigistats',
ProjectionExpression: 'PK, SK, Inspector, Product, UpdatedAt, Decision',
}
console.log('Scanning vigistats table.')
const command = new ScanCommand(params)
try {
const { Items } = await client.send(command)
const jsonItens = Items.map((item) => {
return unmarshall(item)
})
return res.send(jsonItens)
} catch (error) {
console.error(
'Unable to scan the table. Error JSON:',
JSON.stringify(error, null, 2),
)
}
})
app.get('/mail/:id', async function (req: Request, res: Response) {
const mailID = req.params.id
const params = {
TableName: 'vigistats',
KeyConditionExpression: 'PK = :PK',
ExpressionAttributeValues: marshall({
':PK': `INSPECTED#${mailID}`,
}),
}
try {
const command = new QueryCommand(params)
const { Items } = (await client.send(command)) as QueryCommandOutput
if (Items.length === 0) {
const errorStatus = {
status: '404',
stack: 'Mail not found!',
}
return res.render('error', {
error: errorStatus,
})
}
console.log('Query succeeded.')
console.log(Items[0])
return res.send(unmarshall(Items[0]))
} catch (error) {
console.error('Unable to query. Error:', JSON.stringify(error, null, 2))
}
})
app.use((_: Request, res: Response) => {
res.status(404).send("Error 404. Sorry can't find that!")
})