-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
60 lines (55 loc) · 1.31 KB
/
index.ts
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
import express from 'express';
import dao from './src/modules/ads/dao';
const app = express();
const adsDAO = dao.getInstance();
const mq = require('./src/mq');
app.use(express.json());
app.use(
express.urlencoded({
extended: true,
}),
);
app.get('/ads', async (req, res) => {
// TODO: COMPLETE LOGIC.
try {
const ads = await adsDAO.getAds(req.query);
res.json({ ads });
} catch (error) {
res.status(400).json({
status: 'failure',
massage: error.message,
});
}
});
app.get('/ads/best', async (req, res) => {
// TODO: COMPLETE LOGIC.
try {
const ad = await adsDAO.getBestAd(req.query);
if (ad) {
res.json(ad);
} else throw new Error("can't find best ad");
} catch (error) {
res.status(400).json({
status: 'failure',
massage: error.message,
});
}
});
mq.subscribe('create-ad', (newAd: {} = {}, ack: () => void = () => {}, nack: () => void = () => {}) => {
// TODO: COMPLETE LOGIC.
try {
adsDAO.createAd(newAd);
ack();
} catch (error) {
console.error(error);
nack();
throw error;
}
});
app.listen(3333, async () => {
try {
await adsDAO.connect();
} catch {
process.exit(1);
}
});