Haris Razis
4 years ago
10 changed files with 299 additions and 110 deletions
@ -1,41 +1,61 @@ |
|||
const express = require('express') |
|||
const router = express.Router(); |
|||
const mongoose = require('mongoose'); |
|||
const {requireAuth} = require('../middlewares/middleware'); |
|||
const {celebrate} = require('celebrate'); |
|||
|
|||
const {requireAuth} = require('../middlewares/middleware'); |
|||
const {athleteUpdateSchema, guid} = require('../schemas/joi'); |
|||
const Athlete = mongoose.model('Athlete'); |
|||
|
|||
router.get('/api/athletes', requireAuth, async (req, res) => { |
|||
const athletes = await Athlete.find(); |
|||
res.send(athletes); |
|||
}); |
|||
|
|||
router.get('/api/athletes/:id', requireAuth, async (req, res) => { |
|||
const athlete = await Athlete.findById(req.params.id); |
|||
res.send(athlete) |
|||
}); |
|||
|
|||
router.get('/api/athletes/:id/edit', requireAuth, async (req, res) => { |
|||
const athlete = await Athlete.findById(req.params.id) |
|||
res.send(athlete) |
|||
}); |
|||
|
|||
router.put('/api/athletes/:id', requireAuth, async (req, res) => { |
|||
const {name, _trainer} = req.body |
|||
const updateAthlete = {name, _trainer} |
|||
|
|||
if (name || _trainer) |
|||
await Athlete.findByIdAndUpdate(req.params.id, updateAthlete, {}, (err, athlete) => { |
|||
if (err) |
|||
return res.status(400).json({errors: 'Something went wrong!0'}); |
|||
|
|||
res.send(athlete) |
|||
}) |
|||
|
|||
}); |
|||
|
|||
router.delete('/api/athlete/:id', requireAuth, async (req, res) => { |
|||
await Athlete.findByIdAndDelete(req.params.id) |
|||
}); |
|||
router.get('/api/athletes', |
|||
requireAuth, |
|||
async (req, res) => { |
|||
const athletes = await Athlete.find(); |
|||
res.send(athletes); |
|||
}); |
|||
|
|||
router.get('/api/athletes/:id', |
|||
requireAuth, |
|||
celebrate(guid), |
|||
async (req, res) => { |
|||
const athlete = await Athlete.findById(req.params.id); |
|||
res.send(athlete) |
|||
}); |
|||
|
|||
router.get('/api/athletes/:id/edit', |
|||
requireAuth, |
|||
celebrate(guid), |
|||
async (req, res) => { |
|||
const athlete = await Athlete.findById(req.params.id) |
|||
res.send(athlete) |
|||
} |
|||
) |
|||
; |
|||
|
|||
router.put('/api/athletes/:id', |
|||
requireAuth, |
|||
celebrate(athleteUpdateSchema, guid), |
|||
async (req, res) => { |
|||
const {name, _trainer} = req.body |
|||
const updateAthlete = {name, _trainer} |
|||
|
|||
if (name || _trainer) |
|||
await Athlete.findByIdAndUpdate(req.params.id, updateAthlete, {}, (err, athlete) => { |
|||
if (err) |
|||
return res.status(400).json({errors: 'Something went wrong!'}); |
|||
|
|||
res.send(athlete) |
|||
}) |
|||
|
|||
}); |
|||
|
|||
router.delete('/api/athlete/:id', |
|||
requireAuth, |
|||
celebrate(guid), |
|||
async (req, res) => { |
|||
await Athlete.findByIdAndDelete(req.params.id) |
|||
} |
|||
) |
|||
; |
|||
|
|||
module.exports = router; |
|||
|
@ -0,0 +1,37 @@ |
|||
const {Joi} = require('celebrate'); |
|||
|
|||
const guid = { |
|||
params:{ |
|||
userId: Joi.string().guid().required() |
|||
} |
|||
} |
|||
|
|||
const userAuthSchema = { |
|||
body: { |
|||
username: Joi.string().required(), |
|||
password: Joi.string().required(), |
|||
} |
|||
}; |
|||
|
|||
const userUpdateSchema = { |
|||
body: { |
|||
_id: Joi.string().required(), |
|||
username: Joi.string().required(), |
|||
email: Joi.any(), |
|||
password: Joi.string().allow(''), |
|||
newPassword: Joi.string().allow(''), |
|||
} |
|||
}; |
|||
|
|||
const athleteUpdateSchema = { |
|||
body: { |
|||
_id: Joi.string().required(), |
|||
id: Joi.string().required(), |
|||
socketID: Joi.string().required(), |
|||
name: Joi.string().required(), |
|||
__v: Joi.number().integer(), |
|||
_trainer: Joi.string().allow(''), |
|||
} |
|||
} |
|||
|
|||
module.exports = {guid, userAuthSchema, userUpdateSchema, athleteUpdateSchema} |
Loading…
Reference in new issue