-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
75 lines (58 loc) · 1.58 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
const express = require('express')
const app = express()
const mongoose = require('mongoose')
const cors = require('cors')
const ItemModel = require('./models/Items')
require('dotenv').config()
app.use(cors())
app.use(express.json())
mongoose.connect(process.env.DB_STRING,
{ useNewUrlParser: true}
)
app.post('/addItem', async (req, res) => {
const name = req.body.name
const tier = req.body.tier
const item = new ItemModel({ name: name, tier: tier })
await item.save()
res.send(item)
})
app.get('/', async (req, res) => {
const result = await ItemModel.find().clone()
res.send(result)
})
app.put('/upgrade', async (req, res) => {
const newTier = req.body.newTier
const id = req.body.id
try{
await ItemModel.findById(id, (error, itemToUpgrade) => {
itemToUpgrade.tier = newTier
itemToUpgrade.save()
}).clone()
}
catch(err){
console.error(err)
}
res.send("Upgraded")
})
app.put('/downgrade', async (req, res) => {
const newTier = req.body.newTier
const id = req.body.id
try{
await ItemModel.findById(id, (error, itemToUpgrade) => {
itemToUpgrade.tier = newTier
itemToUpgrade.save()
}).clone()
}
catch(err){
console.error(err)
}
res.send("Downgraded")
})
app.delete('/delete/:id', async (req, res) => {
const id = req.params.id
await ItemModel.findByIdAndRemove(id).exec()
res.send("Deleted")
})
app.listen(process.env.PORT, () =>{
console.log(`Listening on ${process.env.PORT}`)
})