Add team model and basic team endpoints
This commit is contained in:
parent
9ffe5b3d46
commit
2d2f335b9c
47
app/controllers/Teams.controller.js
Normal file
47
app/controllers/Teams.controller.js
Normal 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
25
app/models/Team.model.js
Normal 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
|
Loading…
Reference in New Issue
Block a user