-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
151 lines (149 loc) · 3.8 KB
/
index.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const mongoose = require("mongoose");
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const sample = require("lodash/sample");
const forEa = require("lodash/forEach");
const { v4: uuidv4 } = require("uuid");
const ApiModel = require("./model/musk.model")(mongoose);
const MuskAdmin = require("./model/muskAdmin.model")(mongoose);
const port = 3002;
const cors = require("cors");
const corsOptions = {
origin: "*",
};
app.use(cors(corsOptions));
require("dotenv").config();
app.use(bodyParser.json());
app.use(express());
app.use(bodyParser.urlencoded({ extended: true }));
app.listen(process.env.PORT || port);
app.get("/", (req, res) => {
res.redirect("https://muskapi.hinzwifi.xyz");
});
app.get("/api/random", (req, res) => {
ApiModel.find({}).exec(function (err, books) {
if (err) {
res.send("error has occured");
} else {
const random = sample(books);
res.json({
message: {
type: "success",
quoteId: random._id,
quote: random.quote,
URL: `https://${req.headers.host}/api/quote/${random._id}`,
},
});
}
});
});
app.get("/api", (req, res) => {
ApiModel.find({}).exec(async function (err, books) {
if (err) {
res.send("error has occured");
} else {
let data = [];
const bruh = await forEa(books, function (value) {
data.push({
type: "success",
quoteId: value._id,
quote: value.quote,
kekw: `https://${req.headers.host}/api/quote/${value._id}`,
});
});
res.json(data);
}
});
});
app.get("/api/quote/:id", async (req, res) => {
try {
const tweet_id = await ApiModel.findById({ _id: req.params.id });
if (!tweet_id) {
res.json({
message: {
type: "error",
text: "Quote not found",
},
});
} else {
res.json({
message: {
type: "success",
quoteId: tweet_id._id,
quotes: tweet_id.quote,
URL: `https://${req.headers.host}${tweet_id.quoteURL}`,
},
});
}
} catch (err) {
res.status(400).json({ message: err });
}
});
app.post("/api/admin", async (req, res) => {
const admin_id = await MuskAdmin.findById({ _id: req.body.adminId });
if (!admin_id) {
res.json({
message: {
type: "error",
text: "No user found",
},
});
} else {
if (!admin_id.admin) {
res.json({
message: {
type: "error",
text: "Not an Admin",
},
});
} else {
const randomID = uuidv4();
let newQuote = new ApiModel({
_id: randomID,
quote: req.body.quote,
quoteURL: `/api/quote/${randomID}`,
addedBy: admin_id.username,
});
try {
await newQuote.save();
res.json(newQuote);
} catch (err) {
if (err.code === 11000) {
res.status(400).json({
message: {
type: "Duplication",
text: "It is already in the database",
},
});
}
}
}
}
});
app.get("*", function (req, res) {
res.redirect("/api");
});
// NOT YET SURE IF WILL ADD
// app.get("/api/category/:category", (req, res) => {
// try {
// const quote_category = req.params.category;
// ApiModel.find({ category: quote_category }).exec(function (err, books) {
// if (err) {
// res.send("error has occured");
// } else {
// const random = sample(books);
// res.json({
// message: {
// type: "success",
// quotes: random.quote,
// URL: random.quoteURL,
// },
// });
// }
// });
// } catch (err) {
// res.status(400).json({ message: err });
// }
// });
mongoose.connect(process.env.MONGO_URL);