c6e9faf6034790556c1a2c2e8814c827476a2d16 — Martin Angers 4 months ago 7c0e94d
implement league remove command
4 files changed, 81 insertions(+), 16 deletions(-)

M src/cli.js
M src/cmds/league_cmds/remove.js
A src/handlers/league_cmds/remove.js
M src/leagues.js
M src/cli.js => src/cli.js +0 -13
@@ 19,23 19,10 @@
   .help ()
   .argv
 
-// Commands (see https://github.com/yargs/yargs/blob/master/docs/advanced.md#example-command-hierarchy-using-commanddir)
-//
-// $ hockeysim league [list] [--all]
-//    list existing leagues with status (maybe number of seasons,
-//    active season, "archived" if --all is passed, etc.)
-//
-// $ hockeysim league new
-//    starts an interactive dialog to name the new league and its teams.
-//
 // $ hockeysim league rm | remove
 //    lists the leagues available for removal - only leagues with no
 //    started season can be removed.
 //
-// $ hockeysim league archive
-//    lists the active leagues available for archiving - only leagues
-//    with no active season can be archived.
-//
 // $ hockeysim season new --league NAME
 //    create a new season for the specified league. If no league is
 //    provided, the configured default league is used, or a list of

M src/cmds/league_cmds/remove.js => src/cmds/league_cmds/remove.js +45 -3
@@ 1,9 1,51 @@
+import CFonts from 'cfonts'
+import inquirer from 'inquirer'
+import { DB } from 'mymigrate'
+import { removableLeagues } from '../../leagues'
+import exec from '../../handlers/league_cmds/remove'
+
 export const command = ['remove', 'rm']
 export const desc = 'Remove an existing league'
 
 export function builder () {}
 
-export function handler (argv) {
-  console.log ('remove league')
-  console.log (argv)
+function leagueSelectionQuestion (conn) {
+  return () => {
+    return inquirer.prompt ([
+      {
+        type: 'list',
+        name: 'leagueId',
+        message: 'League to remove:',
+        choices () {
+          return removableLeagues (conn)
+            .then (ls => ls.map (l => { l.value = l.id; return l }))
+        },
+        pageSize: 10,
+      },
+      {
+        type: 'confirm',
+        name: 'confirm',
+        message: 'Remove this league?',
+        default: true,
+      },
+    ])
+  }
+}
+
+export async function handler (argv) {
+  CFonts.say ('Remove League', {
+    font: 'chrome',
+    colors: ['#0ff', 'green', '#ff0'],
+  })
+
+  const conn = await new DB ().connect ()
+  try {
+    const prompt = await leagueSelectionQuestion (conn)
+    const res = await prompt ()
+    if (res.confirm) {
+      await exec (conn, res)
+    }
+  } finally {
+    await conn.end ()
+  }
 }

A src/handlers/league_cmds/remove.js => src/handlers/league_cmds/remove.js +5 -0
@@ 0,0 1,5 @@
+import { removeLeague } from '../../leagues'
+
+export default async function (conn, { leagueId }) {
+  await removeLeague (conn, leagueId)
+}

M src/leagues.js => src/leagues.js +31 -0
@@ 6,6 6,8 @@
   return res.insertId
 }
 
+// archives the specified league if it has no season in a state
+// other than completed.
 export async function archiveLeague (conn, leagueId) {
   await conn.query (`
     update leagues l


@@ 20,6 22,21 @@
   `, [LeagueState.archived, leagueId, SeasonState.completed])
 }
 
+// deletes the specified league if it has no season in a state
+// other than unstarted.
+export async function removeLeague (conn, leagueId) {
+  await conn.query (`
+    delete from leagues l
+    where  id = ?
+    and    not exists (
+      select 1
+      from   seasons s
+      where  s.league_id = l.id
+      and    s.state != ?
+    )
+  `, [leagueId, SeasonState.unstarted])
+}
+
 // returns the list of leagues corresponding to the criteria
 export async function listLeagues (conn, all) {
   let args = []


@@ 48,3 65,17 @@
     )`, [LeagueState.archived, SeasonState.completed])
   return res
 }
+
+// returns an array of [id, name] of leagues that can be removed.
+export async function removableLeagues (conn) {
+  const [res] = await conn.query (`
+    select id, name
+    from   leagues l
+    where  not exists (
+      select 1
+      from   seasons s
+      where  s.league_id = l.id
+      and    s.state != ?
+    )`, [SeasonState.unstarted])
+  return res
+}