Add team model and basic team endpoints

This commit is contained in:
Garrett Mills 2020-11-01 13:55:05 -06:00
parent 9ffe5b3d46
commit 2d2f335b9c
Signed by: garrettmills
GPG Key ID: D2BF5FBA8298F246
2 changed files with 72 additions and 0 deletions

View File

@ -0,0 +1,47 @@
const { Controller } = require('libflitter')
/*
* Teams Controller
* -------------------------------------------------------------
*/
class Teams extends Controller {
static get services() {
return [...super.services, 'models']
}
async list_all_teams(req, res) {
const TeamModel = this.models.get('Team')
const teams = await TeamModel.find()
return res.api(teams)
}
async create_team(req, res) {
const TeamModel = this.models.get('Team')
if ( !req.body.team_name || !req.body.team_num ) {
return res.status(400)
.message('Missing team_name or team_num')
.api()
}
const duplicate_team = await TeamModel.findOne({
team_num: req.body.team_num,
})
if ( duplicate_team ) {
return res.status(400)
.message('That team number is already in use!')
.api()
}
const team = new TeamModel({
team_name: req.body.team_name,
team_num: req.body.team_num,
})
await team.save()
return res.api(team)
}
}
module.exports = Teams

25
app/models/Team.model.js Normal file
View File

@ -0,0 +1,25 @@
const { Model } = require('flitter-orm')
/*
* Team Model
* -------------------------------------------------------------
*/
class Team extends Model {
static get services() {
return [...super.services, 'output']
}
/*
* Define the flitter-orm schema of the model.
*/
static get schema() {
return {
user_id: String,
team_name: String,
team_num: Number,
player_ids: [String],
}
}
}
module.exports = exports = Team