-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
132 lines (109 loc) · 3.77 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
// SERVER-SIDE JAVASCRIPT
//require express in our app
var express = require('express');
// generate a new express app and call it 'app'
var app = express();
var mongoose = require("mongoose");
var bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({extended: true}));
// serve static files from public folder
app.use(express.static(__dirname + '/public'));
var db = require("./models");
/**********
* ROUTES *
**********/
/*
* HTML Endpoints
*/
app.get('/', function homepage (req, res) {
res.sendFile(__dirname + '/views/index.html');
});
app.get('/lists', function homepage (req, res) {
res.sendFile(__dirname + '/views/lists/index.html');
});
/*
* JSON API Endpoints - main page
*/
app.get('/api/verbs', function verbList (req, res) {
db.Verb.find({}, function (err, success) {
if (err) { return console.log(err); }
res.send(success);
});
});
app.post('/api/verbs', function newVerb (req, res) {
db.Verb.create(req.body, function (err, success) {
if (err) { return console.log(err); }
res.send(success);
});
});
// searching by infinitive
app.get('/api/verbname/:infinitive', function verbList (req, res) {
db.Verb.find(req.params, function (err, success) {
if (err) { return console.log(err); }
res.send(success);
});
});
//searching by infinitive ID
app.get('/api/verbid/:_id', function verbList (req, res) {
db.Verb.findById(req.params, function (err, success) {
if (err) { return console.log(err); }
res.send(success);
});
});
app.put('/api/verbid/:_id', function updateVerb (req, res) {
db.Verb.findById(req.params, function (err, success) {
if (err) { return console.log(err); }
var selectedTense = req.body.tense;
var updatedVerb = success;
//Checks to see whether anything has actually been changed. If there is a difference, it will save it.
if (updatedVerb.tense[selectedTense].je !== req.body.je) { updatedVerb.tense[selectedTense].je = req.body.je; }
if (updatedVerb.tense[selectedTense].tu !== req.body.tu) { updatedVerb.tense[selectedTense].tu = req.body.tu; }
if (updatedVerb.tense[selectedTense].il !== req.body.il) { updatedVerb.tense[selectedTense].il = req.body.il; }
if (updatedVerb.tense[selectedTense].nous !== req.body.nous) { updatedVerb.tense[selectedTense].nous = req.body.nous; }
if (updatedVerb.tense[selectedTense].vous !== req.body.vous) { updatedVerb.tense[selectedTense].vous = req.body.vous; }
if (updatedVerb.tense[selectedTense].ils !== req.body.ils) { updatedVerb.tense[selectedTense].ils = req.body.ils; }
updatedVerb.save(function (err, savedVerb) {
res.send(updatedVerb);
});
});
});
/*
* JSON API Endpoints - List page
*/
app.get('/api/list', function listsList (req, res) {
db.List.find({}).populate('verbs').exec(function (err, success) {
if (err) { return console.log(err); }
res.send(success);
});
});
app.get('/api/list/:_id', function getListData (req, res) {
db.List.findById(req.params._id).populate('verbs').exec(function (err, success) {
if (err) { return console.log(err); }
res.send(success);
});
});
app.put('/api/list/:_id', function updateListData (req, res) {
db.List.findById(req.params._id, function (err, foundList) {
var updatedList = foundList;
updatedList.name = req.body.name;
updatedList.description = req.body.description;
updatedList.verbs = req.body.verbs;
updatedList.save(function (err, savedList) {
res.send(updatedList);
});
});
});
app.post('/api/list', function newList (req, res) {
db.List.create(req.body, function (err, success) {
if (err) { return console.log(err); }
res.send(success);
});
});
app.delete('/api/list/:_id', function removeList (req, res) {
db.List.remove(req.params, function (err) {
if (err) { return console.log(err); }
res.send("List deleted.");
});
});
// listen on port 3000
app.listen(process.env.PORT || 3000);