-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
115 lines (96 loc) · 3.39 KB
/
server.js
File metadata and controls
115 lines (96 loc) · 3.39 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
const express = require('express')
const mongoose = require('mongoose')
const cors = require('cors')
const logger = require('morgan')
const bodyParser = require('body-parser')
const PORT = process.env.PORT || 3001
const db = require('./db')
const { Legend } = require('./models')
const { Ability } = require('./models')
const app = express()
app.use(cors())
app.use(express.json())
app.use(logger('dev'))
app.use(express.static(`${__dirname}/client/build`))
app.get('/', (req, res) => {
res.send('This is pain')
})
//code here
app.get('/api/legends', async (req, res) => {
const allLegends = await Legend.find()
console.log(`All legends should be here!`)
res.json(allLegends);
})
app.get('/api/abilities', async (req, res) => {
const allAbilities = await Ability.find()
console.log(`All abilites should be present!`)
res.json(allAbilities);
})
app.get('/api/legends/:id', async (req, res) => {
try {
const { id } = req.params;
if (!mongoose.Types.ObjectId.isValid(id)) throw Error ("Not a valid MongoDB ObjectID!!!!!!!");
const thisLegend = await Legend.findById(id);
if (!thisLegend) throw Error ("OH NO! That Legend was not found.")
console.log(`specific legend should arrive!`)
res.json(thisLegend);
} catch (e) {
console.log(e)
res.send(`There was AN ERROR!!! ${e.message}`);
}
})
app.get('/api/abilities/:id', async (req, res) => {
try {
const { id } = req.params;
if (!mongoose.Types.ObjectId.isValid(id)) throw Error ("Not a valid MongoDB ObjectID!!!!!!!");
const thisAbility = await Ride.findById(id);
if (!thisAbility) throw Error ("OH NO! That Ability was not found.")
console.log(`specific Ability should arrive!`)
res.json(thisAbility);
} catch (e) {
console.log(e)
res.send(`There was AN ERROR!!! ${e.message}`);
}
})
app.delete('/api/legend/:legendId/abilities/:abilityId', async (req, res) => {
try {
const { params: { legendId, abilityId } } = req;
console.log(abilityId)
await Ability.findByIdAndDelete(abilityId)
const currentAbilities = await Ability.find({legend_id:legendId})
return res.status(200).json({abilities:currentAbilities});
} catch (error) {
return res.status(500).json({ error: error.message })
}
})
app.post('/api/legend/:legendId/abilities/', async (req, res) => {
try {
const { params: { legendId }, body } = req;
console.log('Data', legendId, body)
const abilityData = {
...body,
legend_id:legendId
}
const ability = await new Ability(abilityData);
await ability.save();
return res.status(200).send('Success');
} catch (error) {
return res.status(500).json({ error: error.message })
}
})
app.put('/api/legend/:legendId/abilities/:abilityId', async (req, res) => {
try {
const { legendId, abilityId } = req.params
const abilityData = req.body;
await Ability.updateOne({_id: abilityId }, abilityData)
return res.status(200).send('Success');
} catch (error) {
return res.status(500).json({ error: error.message })
}
})
app.get('/*', (req, res) => {
res.sendFile(`${__dirname}/client/build/index.html`)
})
app.listen(PORT, () => {
console.log(`Express server listening on port ${PORT}`)
})