f73a541423f0523071ead480e98d382f661484ac — Martin Angers 4 months ago d1636d0
finalize league new command
4 files changed, 44 insertions(+), 12 deletions(-)

M src/cmds/league_cmds/new.js
A src/handlers/league_cmds/new.js
A src/league_teams.js
A src/leagues.js
M src/cmds/league_cmds/new.js => src/cmds/league_cmds/new.js +15 -12
@@ 3,15 3,16 @@
 import { DB } from 'mymigrate'
 import { Constants, TeamName } from '../../constants'
 import { teamsByKind, randomTeam } from '../../team_names'
+import exec from '../../handlers/league_cmds/new'
 
 export const command = 'new'
 export const desc = 'Create new league'
 export function builder () {}
 
-function leagueNameQuestion () {
+function leagueNameQuestion (pres) {
   return inquirer.prompt ([ {
     type: 'input',
-    name: 'league_name',
+    name: 'leagueName',
     message: 'League name:',
     validate (input) {
       return input.trim ().length > 0


@@ 20,8 21,8 @@
       return input.trim ().toLowerCase ()
     },
     when (res) {
-      // used as a hack to initialize the answers hash
-      res.teams = []
+      // used as a hack to collect previous results in new one
+      Object.assign (res, pres)
       return true
     },
   } ])


@@ 32,7 33,7 @@
     return inquirer.prompt ([
       {
         type: 'list',
-        name: 'team_selection',
+        name: 'teamSelection',
         message: `Team #${index + 1} type:`,
         choices: [
           { name: 'NHL', value: TeamName.currentNHL },


@@ 51,10 52,10 @@
         name: `team`,
         message: `Team #${index + 1} selection:`,
         choices (res) {
-          return teamsByKind (conn, res.team_selection, res.teams)
+          return teamsByKind (conn, res.teamSelection, res.teams)
         },
         when (res) {
-          return res.team_selection >= 0
+          return res.teamSelection >= 0
         },
         pageSize: 10,
       },


@@ 66,7 67,7 @@
           return randomTeam (conn, res.teams)
         },
         when (res) {
-          return res.team_selection === -1
+          return res.teamSelection === -1
         },
         validate (input, res) {
           const name = input.trim ()


@@ 98,7 99,7 @@
   return inquirer.prompt ([
     {
       type: 'confirm',
-      name: 'confirm_create',
+      name: 'confirmCreate',
       message: 'Create this league?',
       default: true,
       when (res) {


@@ 124,10 125,12 @@
     }
     promises.push (confirmationQuestion)
 
-    const prompt = () => promises.reduce ((p, f) => p.then (f), Promise.resolve ())
+    const prompt = (init) => promises.reduce ((p, f) => p.then (f), Promise.resolve (init))
 
-    const res = await prompt ()
-    console.log (res)
+    const res = await prompt ({ teams: [] })
+    if (res.confirmCreate) {
+      await exec (conn, res)
+    }
   } finally {
     await conn.end ()
   }

A src/handlers/league_cmds/new.js => src/handlers/league_cmds/new.js +16 -0
@@ 0,0 1,16 @@
+import { createLeague } from '../../leagues'
+import { createLeagueTeam } from '../../league_teams'
+
+export default async function (conn, { leagueName, teams } = {}) {
+  await conn.beginTransaction ()
+  try {
+    const lid = await createLeague (conn, leagueName)
+    for (const team of teams) {
+      await createLeagueTeam (conn, lid, team)
+    }
+    await conn.commit ()
+  } catch (e) {
+    await conn.rollback ()
+    throw e
+  }
+}

A src/league_teams.js => src/league_teams.js +6 -0
@@ 0,0 1,6 @@
+// creates a team for the specified league, and returns the league team's
+// id.
+export async function createLeagueTeam (conn, leagueId, teamName) {
+  const [res] = await conn.query ('insert into league_teams (league_id, name) values (?, ?)', [leagueId, teamName])
+  return res.insertId
+}

A src/leagues.js => src/leagues.js +7 -0
@@ 0,0 1,7 @@
+import { LeagueState } from './constants'
+
+// creates a new league and returns the ID
+export async function createLeague (conn, name, state = LeagueState.active) {
+  const [res] = await conn.query ('insert into leagues (name, state) values (?, ?)', [name, state])
+  return res.insertId
+}