Adde schema and route information for all entities.

This commit is contained in:
John Lyon-Smith
2018-03-26 12:41:53 -07:00
parent 3b16d7c417
commit 4c4f899c6a
12 changed files with 510 additions and 501 deletions

View File

@@ -0,0 +1,153 @@
import passport from 'passport'
import createError from 'http-errors'
import autobind from 'autobind-decorator'
@autobind
export class TeamRoutes {
constructor(container) {
const app = container.app
this.log = container.log
this.db = container.db
this.mq = container.mq
this.ws = container.ws
app.route('/teams')
.get(passport.authenticate('bearer', { session: false }), this.listTeams)
.post(passport.authenticate('bearer', { session: false }), this.createTeam)
.put(passport.authenticate('bearer', { session: false }), this.updateTeam)
app.route('/teams/:_id([a-f0-9]{24})')
.get(passport.authenticate('bearer', { session: false }), this.getTeam)
.delete(passport.authenticate('bearer', { session: false }), this.deleteTeam)
}
listTeams(req, res, next) {
const Team = this.db.Team
let limit = req.params.limit || 20
let skip = req.params.skip || 0
let partial = !!req.params.partial
let branch = req.params.branch
let query = {}
if (branch) {
query.branch = branch
}
Team.count({}).then((total) => {
let teams = []
let cursor = Team.find(query).limit(limit).skip(skip).cursor().map((doc) => {
return doc.toClient(partial)
})
cursor.on('data', (doc) => {
teams.push(doc)
})
cursor.on('end', () => {
res.json({
total: total,
offset: skip,
count: teams.length,
items: teams
})
})
cursor.on('error', (err) => {
next(createError.InternalServerError(err.message))
})
}).catch((err) => {
next(createError.InternalServerError(err.message))
})
}
createTeam(req, res, next) {
const role = req.user.role
// If user's role is not Executive or Administrator, return an error
if (role !== 'executive' && role !== 'administrator') {
return next(new createError.Forbidden())
}
// Create a new Team template then assign it to a value in the req.body
const Team = this.db.Team
let team = new Team(req.body)
// Save the team (with promise) - If it doesnt, catch and throw error
team.save().then((newTeam) => {
res.json(newTeam.toClient())
}).catch((err) => {
next(createError.InternalServerError(err.message))
})
}
updateTeam(req, res, next) {
const role = req.user.role
// If user's role is not Executive or Administrator, return an error
if (role !== 'executive' && role !== 'administrator') {
return new createError.Forbidden()
}
// Do this here because Mongoose will add it automatically otherwise
if (!req.body._id) {
return next(createError.BadRequest('No _id given in body'))
}
let Team = this.db.Team
let teamUpdates = null
try {
teamUpdates = new Team(req.body)
} catch (err) {
return next(createError.BadRequest('Invalid data'))
}
Team.findById(teamUpdates._id).then((foundTeam) => {
if (!foundTeam) {
return next(createError.NotFound(`Team with _id ${_id} was not found`))
}
foundTeam.merge(teamUpdates)
return foundTeam.save()
}).then((savedTeam) => {
res.json(savedTeam.toClient())
}).catch((err) => {
next(createError.InternalServerError(err.message))
})
}
getTeam(req, res, next) {
const Team = this.db.Team
const _id = req.params._id
Team.findById(_id).then((team) => {
if (!team) {
return next(createError.NotFound(`Team with _id ${_id} not found`))
}
res.json(team.toClient())
}).catch((err) => {
next(createError.InternalServerError(err.message))
})
}
deleteTeam(req, res, next) {
const role = req.user.role
// If user's role is not Executive or Administrator, return an error
if (role !== 'executive' && role !== 'administrator') {
return new createError.Forbidden()
}
const Team = this.db.Team
const _id = req.params._id
Team.remove({ _id }).then((team) => {
if (!project) {
return next(createError.NotFound(`Team with _id ${_id} not found`))
}
res.json({})
}).catch((err) => {
next(createError.InternalServerError(err.message))
})
}
}