-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.js
113 lines (98 loc) · 2.83 KB
/
generate.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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
require("dotenv").config();
const mongoose = require("mongoose");
const nfetch = require("node-fetch");
const PORT = 4000;
// DB Config
const db = require("./config/keys").mongoURI;
// Connect to mongo
const connectToMongo = () => mongoose.connect(db, { useNewUrlParser: true });
// Clean database first
const dropMongo = () => mongoose.connection.dropDatabase();
const createEvent = requestBody => {
return new Promise(async (resolve, reject) => {
const response = await nfetch(`http://localhost:${PORT}/api/events/`, {
method: "POST",
body: JSON.stringify(requestBody),
headers: { "Content-Type": "application/json" }
});
const jsonResponse = await response.json();
if ("data" in jsonResponse) {
resolve(jsonResponse.data);
} else {
reject(jsonResponse.error);
}
});
};
const createPortalLibrary = async requestBody => {
return new Promise(async (resolve, reject) => {
const response = await nfetch(
`http://localhost:${PORT}/api/portal_libraries/`,
{
method: "POST",
body: JSON.stringify(requestBody),
headers: { "Content-Type": "application/json" }
}
);
const jsonResponse = await response.json();
if ("data" in jsonResponse) {
resolve(jsonResponse.data);
} else {
reject(jsonResponse.error);
}
});
};
const createFAQ = requestBody => {
return new Promise(async (resolve, reject) => {
const response = await nfetch(`http://localhost:${PORT}/api/faqs/`, {
method: "POST",
body: JSON.stringify(requestBody),
headers: { "Content-Type": "application/json" }
});
const jsonResponse = await response.json();
if ("data" in jsonResponse) {
resolve(jsonResponse.data);
} else {
reject(jsonResponse.error);
}
});
};
const createQuestion = async requestBody => {
return new Promise(async (resolve, reject) => {
const response = await nfetch(`http://localhost:${PORT}/api/questions/`, {
method: "POST",
body: JSON.stringify(requestBody),
headers: { "Content-Type": "application/json" }
});
const jsonResponse = await response.json();
if ("data" in jsonResponse) {
resolve(jsonResponse.data);
} else {
reject(jsonResponse.error);
}
});
};
const generateAll = async () => {
connectToMongo()
.then(() => console.log("Connected to MongoDB"))
.catch(err => console.log(err));
await dropMongo();
await createEvent({
name: "Sokna",
details: "from 12-5-2019 to 20-5-2019",
event_date: "2019-05-12"
});
await createPortalLibrary({
title: "Sokna",
details: "from 12-5-2019 to 20-5-2019"
});
await createQuestion({
quest: "What is the university's mission",
question_id: 600,
submit_user: "omar"
});
await createFAQ({
reply: "hello",
content: "where?"
});
};
generateAll();