Add API endpoint to fetch team
This commit is contained in:
parent
d1d12663a7
commit
3befe69f30
@ -9,6 +9,18 @@ class Teams extends Controller {
|
|||||||
return [...super.services, 'models']
|
return [...super.services, 'models']
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the API data for the current user's team.
|
||||||
|
* Requires an authenticated user.
|
||||||
|
* @param req
|
||||||
|
* @param res
|
||||||
|
* @param next
|
||||||
|
* @return {Promise<void>}
|
||||||
|
*/
|
||||||
|
async get_my_team(req, res, next) {
|
||||||
|
return res.api(await req.user_team.to_api())
|
||||||
|
}
|
||||||
|
|
||||||
async list_all_teams(req, res) {
|
async list_all_teams(req, res) {
|
||||||
const TeamModel = this.models.get('Team')
|
const TeamModel = this.models.get('Team')
|
||||||
const teams = await TeamModel.find()
|
const teams = await TeamModel.find()
|
||||||
|
36
app/models/Player.model.js
Normal file
36
app/models/Player.model.js
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
const { Model } = require('flitter-orm')
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Player Model
|
||||||
|
* -------------------------------------------------------------
|
||||||
|
*/
|
||||||
|
class Player extends Model {
|
||||||
|
static get services() {
|
||||||
|
return [...super.services, 'output', 'models']
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Define the flitter-orm schema of the model.
|
||||||
|
*/
|
||||||
|
static get schema() {
|
||||||
|
return {
|
||||||
|
player_number: String,
|
||||||
|
player_name: String,
|
||||||
|
player_position: String,
|
||||||
|
team_name: String,
|
||||||
|
image_url: String,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async to_api() {
|
||||||
|
return {
|
||||||
|
player_number: this.player_number,
|
||||||
|
player_name: this.player_name,
|
||||||
|
player_position: this.player_position,
|
||||||
|
team_name: this.team_name,
|
||||||
|
image_url: this.image_url,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = exports = Player
|
@ -6,7 +6,7 @@ const { Model } = require('flitter-orm')
|
|||||||
*/
|
*/
|
||||||
class Team extends Model {
|
class Team extends Model {
|
||||||
static get services() {
|
static get services() {
|
||||||
return [...super.services, 'output']
|
return [...super.services, 'output', 'models']
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -20,6 +20,46 @@ class Team extends Model {
|
|||||||
player_ids: [String],
|
player_ids: [String],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Look up or create a team for the given user.
|
||||||
|
* @param {User} user
|
||||||
|
* @return {Promise<Team>}
|
||||||
|
*/
|
||||||
|
static async getForUser(user) {
|
||||||
|
// try to find an existing team first
|
||||||
|
const existing_team = await this.findOne({ user_id: user.id })
|
||||||
|
if ( existing_team ) return existing_team
|
||||||
|
|
||||||
|
// otherwise create a team for the user
|
||||||
|
const new_team = new this({
|
||||||
|
user_id: user.id,
|
||||||
|
team_name: `${user.uid}'s team`,
|
||||||
|
player_ids: [],
|
||||||
|
})
|
||||||
|
|
||||||
|
// Generate the next team number
|
||||||
|
const highest_num_team = await this.sort('-team_num').findOne()
|
||||||
|
if ( highest_num_team ) {
|
||||||
|
new_team.team_num = highest_num_team.team_num + 1
|
||||||
|
} else {
|
||||||
|
new_team.team_num = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
await new_team.save()
|
||||||
|
return new_team
|
||||||
|
}
|
||||||
|
|
||||||
|
async to_api() {
|
||||||
|
const User = this.models.get('auth:User')
|
||||||
|
|
||||||
|
return {
|
||||||
|
user_id: this.user_id,
|
||||||
|
user_display: (await User.findById(this.user_id))?.uid || 'Unknown User',
|
||||||
|
team_name: this.team_name,
|
||||||
|
team_num: this.team_num,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = exports = Team
|
module.exports = exports = Team
|
||||||
|
@ -1,32 +0,0 @@
|
|||||||
const { Middleware } = require('libflitter')
|
|
||||||
|
|
||||||
/*
|
|
||||||
* HomeLogger Middleware
|
|
||||||
* -------------------------------------------------------------
|
|
||||||
* This is a sample middleware. It simply prints a console message when
|
|
||||||
* the route that it is tied to is accessed. By default, it is called if
|
|
||||||
* the '/' route is accessed. It can be injected in routes globally using
|
|
||||||
* the middlewares service.
|
|
||||||
*/
|
|
||||||
class HomeLogger extends Middleware {
|
|
||||||
static get services() {
|
|
||||||
return [...super.services, 'output']
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Run the middleware test.
|
|
||||||
* This method is required by all Flitter middleware.
|
|
||||||
* It should either call the next function in the stack,
|
|
||||||
* or it should handle the response accordingly.
|
|
||||||
*/
|
|
||||||
test(req, res, next, args) {
|
|
||||||
this.output.debug('Home was accessed!')
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Call the next function in the stack.
|
|
||||||
*/
|
|
||||||
next()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = HomeLogger
|
|
27
app/routing/middleware/InjectUserTeam.middleware.js
Normal file
27
app/routing/middleware/InjectUserTeam.middleware.js
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
const { Middleware } = require('libflitter')
|
||||||
|
|
||||||
|
/*
|
||||||
|
* InjectUserTeam Middleware
|
||||||
|
* -------------------------------------------------------------
|
||||||
|
* For the authenticated user, looks up the associated Team instance
|
||||||
|
* and injects it as request.team.
|
||||||
|
*/
|
||||||
|
class InjectUserTeam extends Middleware {
|
||||||
|
static get services() {
|
||||||
|
return [...super.services, 'models']
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Run the middleware test.
|
||||||
|
*/
|
||||||
|
async test(req, res, next, args = {}){
|
||||||
|
if ( !req.user ) return res.redirect('/auth/login')
|
||||||
|
|
||||||
|
const Team = this.models.get('Team')
|
||||||
|
req.user_team = await Team.getForUser(req.user)
|
||||||
|
|
||||||
|
return next()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = InjectUserTeam
|
68
app/routing/routers/api.routes.js
Normal file
68
app/routing/routers/api.routes.js
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
/*
|
||||||
|
* API Routes
|
||||||
|
* -------------------------------------------------------------
|
||||||
|
* These routes are related to the AJAX API used by the front-end.
|
||||||
|
*/
|
||||||
|
const index = {
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Define the prefix applied to each of these routes.
|
||||||
|
* For example, if prefix is '/auth':
|
||||||
|
* '/' becomes '/auth'
|
||||||
|
* '/login' becomes '/auth/login'
|
||||||
|
*/
|
||||||
|
prefix: '/api/v1',
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Define middleware that should be applied to all
|
||||||
|
* routes defined in this file. Middleware should be
|
||||||
|
* included using its non-prefixed canonical name.
|
||||||
|
*
|
||||||
|
* You can pass arguments along to a middleware by
|
||||||
|
* specifying it as an array where the first element
|
||||||
|
* is the canonical name of the middleware and the
|
||||||
|
* second element is the argument passed to the
|
||||||
|
* handler's test() method.
|
||||||
|
*/
|
||||||
|
middleware: [
|
||||||
|
// Require an authenticated user
|
||||||
|
'auth:UserOnly',
|
||||||
|
|
||||||
|
// Inject the user's team
|
||||||
|
'InjectUserTeam',
|
||||||
|
],
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Define GET routes.
|
||||||
|
* These routes are registered as GET methods.
|
||||||
|
* Handlers for these routes should be specified as
|
||||||
|
* an array of canonical references to controller methods
|
||||||
|
* or middleware that are applied in order.
|
||||||
|
*/
|
||||||
|
get: {
|
||||||
|
// handlers should be a list of either controller:: or middleware:: references
|
||||||
|
// e.g. middleware::HomeLogger
|
||||||
|
// e.g. controller::Home.welcome
|
||||||
|
'/': [
|
||||||
|
'controller::Home.welcome'
|
||||||
|
],
|
||||||
|
|
||||||
|
'/my-team': ['controller::Teams.get_my_team'],
|
||||||
|
},
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Define POST routes.
|
||||||
|
* These routes are registered as POST methods.
|
||||||
|
* Handlers for these routes should be specified as
|
||||||
|
* an array of canonical references to controller methods
|
||||||
|
* or middleware that are applied in order.
|
||||||
|
*/
|
||||||
|
post: {
|
||||||
|
|
||||||
|
},
|
||||||
|
|
||||||
|
// You can include other HTTP verbs here.
|
||||||
|
// Supported ones are: get, post, put, delete, copy, patch
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = exports = index
|
@ -28,9 +28,6 @@ const index = {
|
|||||||
middleware: [
|
middleware: [
|
||||||
// Sets the locale scope
|
// Sets the locale scope
|
||||||
['i18n:Scope', {scope: 'common'}],
|
['i18n:Scope', {scope: 'common'}],
|
||||||
|
|
||||||
['HomeLogger', {note: 'arguments can be specified as the second element in this array'}],
|
|
||||||
// 'MiddlewareName', // Or without arguments
|
|
||||||
],
|
],
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
Loading…
Reference in New Issue
Block a user