-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
157 lines (152 loc) · 3.87 KB
/
server.js
File metadata and controls
157 lines (152 loc) · 3.87 KB
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
const mongoose = require('mongoose');
const express = require('express');
const db = require('./models');
require('dotenv').config();
var bodyParser = require('body-parser');
//APP SETUP
const app = express();
const port = process.env.PORT || 3001;
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
//fix promise bug
mongoose.Promise = global.Promise;
//connect to mongoDB
mongoose.connection.openUri(process.env.MONGODB_URI || process.env.DB_CONN, {}, function(err,con){
if(err){
console.log('Error connecting to Mongo DB.', err);
}else{
console.log('Mongoose successfully connected to MongoDB.');
}
});
//ROUTES
app.get('/', function(req,res){
res.send(`
Welcome to the Nahuatl API.
This is an experimental, open API for use with the Study-Nahuatl app.
Check: https://github.com/CescoIV/nahuatl_api.
Currently our data is not curated! Check the github repository
read me for routes/ more information.
`);
})
//USER ROUTES
app.get('/users/:username', function(req,res){
let username = req.params.username;
console.log(username);
db.User.findOne({username: username}, function(err,data){
if(err){
console.log(err, 'could not find user');
}else{
res.send(data);
}
})
})
app.post('/users', function(req,res){
var newUser= db.User({
id: req.body.id,
knownWords: req.body.knownWords,
username: req.body.username
})
newUser.save(function(err,user){
if(err){
console.log(err)
}else{
res.status(201).json(user);
}
})
})
app.patch('/users/:username/:word', function(req,res){
//finds user,gets their words, if word exists,it lets you know,else it updates the user with the new word.
let username = req.params.username;
let word = req.params.word;
db.User.findOne({username: username}, function(err,data){
let response = '';
if(err){
console.log(err, "could not find user");
}else{
console.log('found user', data);
if(data.knownWords){
if(data.knownWords.includes(word)){
response = 'user already knows word' + word;
}else{
data.knownWords.push(word);
console.log('Your word is about to update', data)
response = 'Your word has been saved';
}
}else{
console.log('Could not find words array');
}
console.log(data.knownWords);
console.log(data);
res.send(response + " " + data);
}
data.save(function(err,savedUser){
if(err){
console.log('could not save user');
}else{
console.log('User saved!', savedUser)
}
});
});
})
//WORD ROUTES
app.get('/language/nahuatl/words', function(req,res){
//get all words in database
db.Word.find({},function(err,data){
if(err){
console.log('could not get words');
}else{
res.send(data);
}
})
})
app.get('/language/nahuatl/words/:word',(req,res)=>{
//search nahuatl word
db.Word.find({word_native: req.params.word}, (err,data)=>{
if(err){
console.log(err);
}else{
res.send('201',data);
}
})
})
app.get('/language/nahuatl/words/en/:word',(req,res)=>{
//search english word
db.Word.find({word_english: req.params.word}, (err,data)=>{
if(err){
console.log(err);
}else{
res.send('201',data);
}
})
})
app.post('/language/nahuatl/words',function(req,res){
var newWord = db.Word({
word_native: req.body.word_native,
word_english: req.body.word_english,
correct_responses: req.body.correct_responses,
})
newWord.save(function(err,word){
if(err){
console.log(err);
}else{
res.status(201).json(word);
}
})
})
app.patch('/language/nahuatl/words/:word', (req,res)=>{
db.Word.findOneAndUpdate({word_native: req.body.word_native}, {"$set": {
"word_native": req.body.word_native,
"word_english": req.body.word_english,
"correct_responses": req.body.correct_responses,
"source": req.body.source,
}}).exec((err,response)=>{
if(err){
console.log(err);
}else{
res.status(201).json(response);
}
})
});
app.listen(port,function(){
console.log(`Server listening on port ${port}`);
})