SectionList on home screen with API

This commit is contained in:
John Lyon-Smith
2018-04-08 18:33:21 -07:00
parent 5634acb967
commit 7891bb71c9
19 changed files with 1278 additions and 1201 deletions

View File

@@ -1,6 +1,7 @@
import passport from 'passport'
import createError from 'http-errors'
import autobind from 'autobind-decorator'
import passport from "passport"
import createError from "http-errors"
import autobind from "autobind-decorator"
import { catchAll } from "."
@autobind
export class TeamRoutes {
@@ -12,14 +13,31 @@ export class TeamRoutes {
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")
.get(
passport.authenticate("bearer", { session: false }),
catchAll(this.listTeams)
)
.post(
passport.authenticate("bearer", { session: false }),
catchAll(this.createTeam)
)
.put(
passport.authenticate("bearer", { session: false }),
catchAll(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)
app
.route("/teams/:_id([a-f0-9]{24})")
.get(
passport.authenticate("bearer", { session: false }),
catchAll(this.getTeam)
)
.delete(
passport.authenticate("bearer", { session: false }),
catchAll(this.deleteTeam)
)
}
async listTeams(req, res, next) {
@@ -29,139 +47,103 @@ export class TeamRoutes {
let partial = !!req.query.partial
let query = {}
try {
const total = await Team.count({})
let teams = []
let cursor = Team.find(query).limit(limit).skip(skip).cursor().map((doc) => {
const total = await Team.count({})
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("data", (doc) => {
teams.push(doc)
})
cursor.on("end", () => {
res.json({
total: total,
offset: skip,
count: teams.length,
items: teams,
})
cursor.on('end', () => {
res.json({
total: total,
offset: skip,
count: teams.length,
items: teams
})
})
cursor.on('error', (err) => {
throw err
})
} catch(err) {
if (err instanceof createError.HttpError) {
next(err)
} else {
next(createError.InternalServerError(err.message))
}
}
})
cursor.on("error", (err) => {
throw err
})
}
async createTeam(req, res, next) {
try {
if (!req.user.administrator) {
throw 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)
const newTeam = await team.save()
res.json(newTeam.toClient())
} catch(err) {
if (err instanceof createError.HttpError) {
next(err)
} else {
next(createError.InternalServerError(err.message))
}
if (!req.user.administrator) {
throw 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)
const newTeam = await team.save()
res.json(newTeam.toClient())
}
async updateTeam(req, res, next) {
try {
if (!req.user.administrator) {
throw createError.Forbidden()
}
// Do this here because Mongoose will add it automatically otherwise
if (!req.body._id) {
throw createError.BadRequest('No _id given in body')
}
let Team = this.db.Team
let teamUpdates = null
try {
teamUpdates = new Team(req.body)
} catch (err) {
throw createError.BadRequest('Invalid data')
}
const foundTeam = await Team.findById(teamUpdates._id)
if (!foundTeam) {
throw createError.NotFound(`Team with _id ${_id} was not found`)
}
foundTeam.merge(teamUpdates)
const savedTeam = await foundTeam.save()
res.json(savedTeam.toClient())
} catch (err) {
if (err instanceof createError.HttpError) {
next(err)
} else {
next(createError.InternalServerError(err.message))
}
if (!req.user.administrator) {
throw createError.Forbidden()
}
// Do this here because Mongoose will add it automatically otherwise
if (!req.body._id) {
throw createError.BadRequest("No _id given in body")
}
let Team = this.db.Team
let teamUpdates = null
try {
teamUpdates = new Team(req.body)
} catch (err) {
throw createError.BadRequest("Invalid data")
}
const foundTeam = await Team.findById(teamUpdates._id)
if (!foundTeam) {
throw createError.NotFound(`Team with _id ${_id} was not found`)
}
foundTeam.merge(teamUpdates)
const savedTeam = await foundTeam.save()
res.json(savedTeam.toClient())
}
async getTeam(req, res, next) {
const Team = this.db.Team
const _id = req.params._id
try {
const team = await Team.findById(_id)
const team = await Team.findById(_id)
if (!team) {
throw createError.NotFound(`Team with _id ${_id} not found`)
}
res.json(team.toClient())
} catch (err) {
if (err instanceof createError.HttpError) {
next(err)
} else {
next(createError.InternalServerError(err.message))
}
if (!team) {
throw createError.NotFound(`Team with _id ${_id} not found`)
}
res.json(team.toClient())
}
async deleteTeam(req, res, next) {
try {
if (!req.user.administrator) {
throw createError.Forbidden()
}
const Team = this.db.Team
const _id = req.params._id
const removedTeam = await Team.remove({ _id })
if (!removedTeam) {
throw createError.NotFound(`Team with _id ${_id} not found`)
}
res.json({})
} catch(err) {
if (err instanceof createError.HttpError) {
next(err)
} else {
next(createError.InternalServerError(err.message))
}
if (!req.user.administrator) {
throw createError.Forbidden()
}
const Team = this.db.Team
const _id = req.params._id
const removedTeam = await Team.remove({ _id })
if (!removedTeam) {
throw createError.NotFound(`Team with _id ${_id} not found`)
}
res.json({})
}
}