-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
63 lines (53 loc) · 1.63 KB
/
app.js
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
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const Topic = require("./models/Topic");
const Content = require("./models/Content");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
require("./connections/database");
const { client, redisConnection } = require("./connections/redis");
redisConnection();
app.post("/api/addSubs", async (req, res) => {
const { email, topic } = req.body;
try {
const doc = await Topic.find({ topic });
if (doc.length == 0) {
const topicDoc = await Topic.create({
topic: topic,
subscribers: [email],
});
return;
}
doc[0].subscribers.push(email);
await doc[0].save();
} catch (e) {
console.log(e);
}
});
app.post("/api/addContent", async (req, res) => {
const { contentText, contentTime, topic } = req.body;
try {
const contentDoc = await Content.create({
contentText,
contentTime: new Date(contentTime),
topic,
});
console.log("Content Saved");
const latestTime = await client.get("latestTime");
const contentDateTime = new Date(contentTime);
const latestDateTime = new Date(latestTime);
if (latestTime === null || contentDateTime < latestDateTime) {
// Adding keys in the redis db
await client.set("latestTime", contentDateTime.toISOString());
await client.set("latestData", contentText);
await client.set("latestId", contentDoc.id);
await client.set("latestTopic", contentDoc.topic);
}
} catch (e) {
console.log(e);
}
});
app.listen(3000, () => {
console.log("Server running");
});