-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
186 lines (175 loc) · 6.39 KB
/
app.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
"use strict";
import "module-alias/register";
import bodyParser from "body-parser";
import cors from "cors";
import dotenv from "dotenv";
import express, {Request, Response} from "express";
import helmet from "helmet";
import mongoose from "mongoose";
import morgan from "morgan";
import compression from "compression"
import {default as authWebService} from "./api/v1/authentication-web-service";
import {default as Roles} from "@models/roles";
import {shortForRoles} from "./utils/resolvers";
import {default as RolesPolicy} from "@policies/RolesPolicy"
class Server {
public app: express.Application;
public constructor() {
this.app = express();
this.config();
this.mongo();
this.routes();
this.testEndpoint();
this.roles();
}
public config(): void {
dotenv.config({path: ".env"});
this.app.set("PORT", process.env.PORT || 8088);
// setting up the default header configurations here
this.app.use(cors());
this.app.use(helmet());
this.app.use(bodyParser.json());
this.app.use(bodyParser.urlencoded({
extended: true
}));
this.app.use(morgan("dev"));
this.app.use(compression());
}
public testEndpoint(): void {
this.app.get("/testing", (req: Request, res: Response) => {
res.status(200)
.json({
message: "Server Up And Running !!!",
success: true
});
});
}
/**
* This configurations start the web server
* @return null
*/
public start(): void {
this.app.listen(this.app.get("PORT"), () => {
console.log(process.env.API_NAME);
console.log("Version Number ::->" + process.env.API_VERSION_NUMBER);
console.log(`Server up and running: http://localhost:${this.app.get("PORT")}`);
});
}
protected mongo(): void {
// making the database connection here to the mongoose
mongoose.set("useCreateIndex", true);
mongoose.connect(process.env.MONGO_URL_LOCAL, {
dbName: "schools",
useNewUrlParser: true
});
const db = mongoose.connection;
db.on("error", console.error.bind(console, "Connection error"));
db.once("open", () => {
console.log("Connection opened and available to accept data");
});
}
/**
* contains the endpoints for CRUD operations of roles
* @return null
*/
protected roles(): void {
// getting of all roles
this.app.get("/role/all", (req: Request, res: Response) => {
Roles.find({}, (err, results) => {
if (!err && results) {
res.status(200)
.json({
message: "List of Roles",
success: true,
results: results
})
}
});
});
// saving of roles endpoint
this.app.post("/role/add", RolesPolicy.rolesPolicy, (req: Request, res: Response) => {
Roles.findOne({name: req.body.name, short: shortForRoles(req.body.name)}, (err, results) => {
if (!err && results) {
res.status(200)
.json({
message: "Role already exists",
success: false,
results: results
})
} else {
const roleReq = new Roles({
name: req.body.name,
short: shortForRoles(req.body.name),
description: req.body.description
});
roleReq.save((err, role) => {
if (!err && role) {
res.status(201)
.json({
message: "Role successfully saved",
success: true,
results: role
})
} else {
res.status(500)
.json({
message: "An error occurred while saving role",
success: false,
results: err
})
}
})
}
});
});
// delete a role endpoint
this.app.route("/role/:id")
.patch((req: Request, res: Response) => {
Roles.findByIdAndUpdate(req.params.id, {
$set: {
name: req.body.name,
short: shortForRoles(req.body.name),
description: req.body.description
}
}, {new: true}, (err, results) => {
if (!err && results) {
res.status(200)
.json({
message: "Role updated successfully",
success: true,
results: results
})
} else {
res.status(500)
.json({
message: "An error occurred while updating role",
success: false,
results: err
})
}
})
})
.delete((req: Request, res: Response) => {
Roles.findByIdAndRemove(req.params.id, {
select: "name"
}, (err, results) => {
if (!err && results) {
res.status(200)
.json({
message: "Role deleted successfully",
success: true,
results: results
});
}
})
});
}
/**
* This configurations contains the endpoint mouthpiece
* @return null
*/
protected routes(): void {
this.app.use("/api/v1", authWebService);
}
}
export default Server;