This repository has been archived by the owner on Nov 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
executable file
·145 lines (124 loc) · 4.42 KB
/
server.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
var express = require("express");
var bodyParser = require("body-parser");
var mongodb = require("mongodb");
var ObjectID = mongodb.ObjectID;
var LOCALIZATIONS_COLLECTION = "localization";
var app = express();
app.use(bodyParser.json());
// Create link to Angular build directory
var distDir = __dirname + "/dist/";
app.use(express.static(distDir));
// Create a database variable outside of the database connection callback to reuse the connection pool in your app.
var db;
var languages = ['English', 'French', 'German', 'Italian', 'Spanish'];
// Connect to the database before starting the application server.
mongodb.MongoClient.connect(process.env.MONGODB_URI || "mongodb://localhost:27017/test", function (err, client) {
if (err) {
console.log(err);
process.exit(1);
}
// Save database object from the callback for reuse.
db = client.db();
console.log("Database connection ready");
// Initialize the app.
var server = app.listen(process.env.PORT || 8080, function () {
var port = server.address().port;
console.log("App now running on port", port);
});
});
// LCOALIZATIONS API ROUTES BELOW
// Generic error handler used by all endpoints.
function handleError(res, reason, message, code) {
console.log("ERROR: " + reason);
console.log("Message: " + message);
console.log("code: " + code);
res.status(code || 500).json({ "error": message });
}
/* "/api/localization"
* GET: finds all localization
* POST: creates a new localization
*/
app.get("/api/localization", function (req, res) {
db.collection(LOCALIZATIONS_COLLECTION).find({}).toArray(function (err, docs) {
if (err) {
handleError(res, err.message, "Failed to get localization.");
} else {
res.status(200).json(docs);
}
});
});
app.post("/api/localization", function (req, res) {
var newLocalization = req.body;
newLocalization.createDate = new Date();
if (!req.body.string) {
handleError(res, "Invalid user input", "Must provide a string.", 400);
} else {
db.collection(LOCALIZATIONS_COLLECTION).insertOne(newLocalization, function (err, doc) {
if (err) {
handleError(res, err.message, "Failed to create new localization.");
} else {
res.status(201).json(doc.ops[0]);
}
});
}
});
/* "/api/localization/:id"
* GET: find localization by id
* PUT: update localization by id
* DELETE: deletes localization by id
*/
app.get("/api/localization/:id", function (req, res) {
db.collection(LOCALIZATIONS_COLLECTION).findOne({ _id: new ObjectID(req.params.id) }, function (err, doc) {
if (err) {
handleError(res, err.message, "Failed to get localization");
} else {
res.status(200).json(doc);
}
});
});
app.put("/api/localization/:id", function (req, res) {
var updateDoc = req.body;
console.log(updateDoc._id);
db.collection(LOCALIZATIONS_COLLECTION).updateOne({ _id: new ObjectID(req.params.id) }, { $set: { "string": updateDoc.string, "localization": updateDoc.localization, "comment": updateDoc.comment, "language": updateDoc.language } }, function (err, doc) {
if (err) {
handleError(res, err.message, "Failed to update localization - " + updateDoc.string);
console.log("error");
} else {
console.log(req.param.id);
// updateDoc._id = req.params.id;
res.status(200).json(doc);
}
});
});
app.delete("/api/localization/:id", function (req, res) {
db.collection(LOCALIZATIONS_COLLECTION).deleteOne({ _id: new ObjectID(req.params.id) }, function (err, result) {
if (err) {
handleError(res, err.message, "Failed to delete localization");
} else {
res.status(200).json(req.params.id);
}
});
});
/* "/api/duplicate"
* POST: duplicate localization across languages
*/
app.post("/api/duplicate", function (req, res) {
if (!req.body.string) {
handleError(res, "Invalid user input", "Must provide a string.", 400);
} else {
var newLocalizations = [];
for (var i = 0; i < languages.length; i++) {
var newLocalization = JSON.parse(JSON.stringify(req.body));
newLocalization.createDate = new Date();
newLocalization.language = languages[i];
newLocalizations.push(newLocalization);
}
db.collection(LOCALIZATIONS_COLLECTION).insertMany(newLocalizations, function (err, doc) {
if (err) {
handleError(res, err.message, "Failed to create new localization.");
} else {
res.status(201).json(doc.ops[0]);
}
});
}
});