Browse Source

ohne hashisds

burningTyger 5 years ago
parent
commit
02f43e9fea

+ 3 - 5
components/projektwoche-schueler.svelte

@@ -6,7 +6,6 @@
       <table class="table is-striped">
         <thead>
           <tr>
-            <th>ID</th>
             <th>Name</th>
             <th>Vorname</th>
             <th>Projektwahl</th>
@@ -15,7 +14,6 @@
         <tbody>
           {#each schuelers as s,i}
             <tr>
-              <td>{s.hashid}</td>
               <td>{s.Name}</td>
               <td>{s.Vorname}</td>
               <td class={'has-background-'+wahl(s)} on:click={() => modalset(s,i)} style="cursor: pointer">
@@ -188,12 +186,12 @@
     }
   }
   async function wahl_aendern () {
-    const text = `INSERT INTO wahlen(schueler_hashid, woche, mo_di, mi_do)
+    const text = `INSERT INTO wahlen(schueler_id, woche, mo_di, mi_do)
                     VALUES($1, $2, $3, $4)
-                  ON CONFLICT (schueler_hashid) DO
+                  ON CONFLICT (schueler_id) DO
                     UPDATE SET woche=$2, mo_di=$3, mi_do=$4
                   RETURNING *`
-    const values = [modal_s.hashid, selected_woche, selected_mo_di, selected_mi_do]
+    const values = [modal_s.id, selected_woche, selected_mo_di, selected_mi_do]
     try {
       const res = await pool.query(text, values)
       const data = res.rows[0]

+ 8 - 7
components/projektwoche-uebersicht.svelte

@@ -26,21 +26,22 @@
 </div>
 
 <script>
-  export let schueler, privat, einstellungen
+  export let schueler, privat, einstellungen = {}
   let check
   const { Pool } = R('pg')
   const pool = new Pool({ connectionString: privat.mein_bk_db})
-  $: wahlbeginn_prowo = wahlbeginn_prowo || setDateTime(einstellungen.wahlbeginn_prowo)
-  $: wahlende_prowo = wahlende_prowo || setDateTime(einstellungen.wahlende_prowo)
-  $: thema_prowo = thema_prowo || einstellungen.thema_prowo || ''
+  let wahlbeginn_prowo = setDateTime(einstellungen.wahlbeginn_prowo)
+  let wahlende_prowo = setDateTime(einstellungen.wahlende_prowo)
+  let thema_prowo = einstellungen.thema_prowo || ''
   const changeDate = async _ => {
     const query = `UPDATE einstellungen
-                    SET wahlbeginn_prowo=$1, wahlende_prowo=$2, thema_prowo=$3
-                  RETURNING wahlbeginn_prowo, wahlende_prowo, thema_prowo`
-    const values=[new Date(wahlbeginn_prowo), new Date(wahlende_prowo), thema_prowo]
+                   SET wahlbeginn_prowo=$1, wahlende_prowo=$2, thema_prowo=$3
+                   RETURNING wahlbeginn_prowo, wahlende_prowo, thema_prowo`
+    const values = [new Date(wahlbeginn_prowo), new Date(wahlende_prowo), thema_prowo]
     try {
       const res = await pool.query(query, values)
       const data = res.rows[0]
+      console.log(res)
       einstellungen.wahlbeginn_prowo = data.wahlbeginn_prowo
       einstellungen.wahlende_prowo = data.wahlende_prowo
       einstellungen.thema_prowo = data.thema_prowo

+ 4 - 5
components/svwahl.svelte

@@ -32,7 +32,6 @@
       <table class="table is-striped">
         <thead>
           <tr>
-            <th>ID</th>
             <th>Name</th>
             <th>Vorname</th>
             <th>Wahlstatus</th>
@@ -41,12 +40,12 @@
         <tbody>
           {#each schuelers as s,i}
             <tr>
-              <td>{s.hashid}</td>
               <td>{s.Name}</td>
               <td>{s.Vorname}</td>
               <td class:has-background-success={s.schuko||s.sprecher}>
                 Schuko: <input checked={s.schuko} type="checkbox" on:click={()=>check_aktiv(s, 'schuko')}>
                 Sprecher: <input checked={s.sprecher} type="checkbox" on:click={()=>check_aktiv(s, 'sprecher')}>
+                gewählt: <input checked={s.gewaehlt} type="checkbox" on:click={()=>check_aktiv(s, 'gewaehlt')}>
               </td>
             </tr>
           {/each}
@@ -56,7 +55,7 @@
   </div>
 </div>
 <script>
-  export let schueler, privat, einstellungen
+  export let schueler, privat, einstellungen = {}
   let check
   const { Pool } = R('pg')
   const pool = new Pool({ connectionString: privat.mein_bk_db})
@@ -82,9 +81,9 @@
   }
   const check_aktiv = async (s, art) => {
     const query = `UPDATE schueler SET ${art}=$1
-                   WHERE hashid=$2
+                   WHERE id=$2
                    RETURNING ${art}`
-    const values=[!s.schuko, s.hashid]
+    const values=[!s[art], s.id]
     try {
       const res = await pool.query(query, values)
       console.log(res)

+ 53 - 24
components/vouchers.svelte

@@ -1,27 +1,56 @@
-{#each Object.entries(schueler) as [klasse, schueler]}
-  <h3 class="title">{klasse}</h3>
-  <table class="table is-striped">
-    <thead>
-      <tr>
-        <th>ID></th>
-        <th>Name</th>
-        <th>Vorname</th>
-        <th>Voucher</th>
-      </tr>
-    </thead>
-    <tbody>
-      {#each schueler as s}
-        <tr>
-          <td>{s.hashid}</td>
-          <td>{s.Name}</td>
-          <td>{s.Vorname}</td>
-          <td><span class={s.voucher && "tag is-info is-medium"}>{s.voucher || ''}</span></td>
-        </tr>
-      {/each}
-    </tbody>
-  </table>
-{/each}
+<div class="columns">
+  <div class="column is-two-thirds">
+    {#each Object.entries(schueler) as [klasse, schueler]}
+      <h3 class="title">{klasse}</h3>
+      <table class="table is-striped">
+        <thead>
+          <tr>
+            <th>Name</th>
+            <th>Vorname</th>
+            <th>Voucher</th>
+          </tr>
+        </thead>
+        <tbody>
+          {#each schueler as s}
+            <tr>
+              <td>{s.Name}</td>
+              <td>{s.Vorname}</td>
+              <td><span class={s.voucher && "tag is-info is-medium"}>{s.voucher || ''}</span></td>
+            </tr>
+          {/each}
+        </tbody>
+      </table>
+    {/each}
+  </div>
+  <div class="column">
+    <h3 class="title">Voucher austauschen</h3>
+    Voucher als Liste zeilenweise in das Textfeld einfügen. Alte Voucher
+    werden entfernt und durch die neuen ersetzt.
+    <textarea bind:value={vouchers} rows="20" cols="20"></textarea>
+    <button on:click={()=>update_vouchers()}>Aktualisieren</button>
+  </div>
+</div>
 
 <script>
-  export let schueler, klasse
+  export let schueler, klasse, privat
+  const { Pool } = R('pg')
+  const pool = new Pool({ connectionString: privat.mein_bk_db})
+  let vouchers = ''
+  function vouchers_value () {
+    const arr = vouchers.split(/\r\n|\r|\n/g)
+    return arr.filter(e=>!e.startsWith('#')).map(e=>'(\'' + e.substring(2,13) + '\')').join(',')
+  }
+  const update_vouchers = async () => {
+    const query = `INSERT INTO wlan (voucher)
+                   VALUES ${vouchers_value()}
+                   RETURNING *`
+    // const values = [vouchers_value()]
+    try {
+      await pool.query(`DELETE FROM wlan`)
+      const res = await pool.query(query)
+      console.log(res.rows)
+    } catch(err) {
+      console.log(err.stack)
+    }
+  }
 </script>

+ 51 - 61
Übersicht.svelte

@@ -1,92 +1,82 @@
-  <section class="section">
-    <div class="container">
-      <h1 class="title">Mein BK</h1>
-      <div>
-        <span class="tag is-success" class:is-danger={!diff(schueler_lokal, schueler_entfernt)} on:click={query} style="cursor: pointer;">
-        {diff(schueler_lokal, schueler_entfernt) ? `${schueler_lokal.length} Schüler in der Datenbank` : 'Schülerdaten müssen aktualisiert werden'}
-      </span>
-      </div>
-      <br>
-      <div class="field">
-        <div class="control">
-          <input class="input" type="text" placeholder="Filter" bind:value={suche}>
-        </div>
-      </div>
+<section class="section">
+  <div class="container">
+    <h1 class="title">Mein BK</h1>
+    <div>
+      <span class="tag is-success" class:is-danger={!diff(schueler_lokal, schueler_entfernt)} style="cursor: pointer;">
+      {diff(schueler_lokal, schueler_entfernt) ? `${schueler_lokal.length} Schüler in der Datenbank` : 'Schülerdaten müssen aktualisiert werden'}
+    </span>
     </div>
-    <div class="container">
-      <div class="tabs">
-        <ul>
-          <li class:is-active={active === Vouchers}><a on:click={() => active=Vouchers}>WLAN</a></li>
-          <li class:is-active={active === ProjektwocheSchueler}><a on:click={() => active=ProjektwocheSchueler}>Projektwoche Schüler</a></li>
-          <li class:is-active={active === ProjektwocheLehrer}><a on:click={() => active=ProjektwocheLehrer}>Projektwoche Lehrer</a></li>
-          <li class:is-active={active === ProjektwocheUebersicht}><a on:click={() => active=ProjektwocheUebersicht}>Projektwoche Übersicht</a></li>
-          <li class:is-active={active === SVWahl}><a on:click={() => active=SVWahl}>SV-Wahl</a></li>
-        </ul>
+    <br>
+    <div class="field">
+      <div class="control">
+        <input class="input" type="text" placeholder="Filter" bind:value={suche}>
       </div>
-      <svelte:component this={active} schueler={schueler_filter} {privat} {einstellungen}/>
     </div>
-  </section>
+  </div>
+  <div class="container">
+    <div class="tabs">
+      <ul>
+        <li class:is-active={active === Vouchers}><a on:click={() => active=Vouchers}>WLAN</a></li>
+        <li class:is-active={active === ProjektwocheSchueler}><a on:click={() => active=ProjektwocheSchueler}>Projektwoche Schüler</a></li>
+        <li class:is-active={active === ProjektwocheLehrer}><a on:click={() => active=ProjektwocheLehrer}>Projektwoche Lehrer</a></li>
+        <li class:is-active={active === ProjektwocheUebersicht}><a on:click={() => active=ProjektwocheUebersicht}>Projektwoche Übersicht</a></li>
+        <li class:is-active={active === SVWahl}><a on:click={() => active=SVWahl}>SV-Wahl</a></li>
+      </ul>
+    </div>
+    <svelte:component this={active} schueler={schueler_filter} {privat} {einstellungen}/>
+  </div>
+</section>
 
 <style>
   @import 'node_modules/bulma/css/bulma.css';
 </style>
 
 <script>
-  import Hashids from 'hashids'
   import Vouchers from './components/vouchers.svelte'
   import ProjektwocheSchueler from './components/projektwoche-schueler.svelte'
   import ProjektwocheLehrer from './components/projektwoche-lehrer.svelte'
   import ProjektwocheUebersicht from './components/projektwoche-uebersicht.svelte'
   import SVWahl from './components/svwahl.svelte'
   export let schueler, knexConfig, privat
-  let active = SVWahl
+  let active
   let suche = ''
 
   const { Pool } = R('pg')
   const _ = R('lodash')
   const mysql = R('mysql')
-  const hashids = new Hashids(privat.salt, 5, 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789')
-  const h = (id) => hashids.encode(id)
   let schueler_entfernt = [], schueler_lokal = []
-  let merged =[]
-  let einstellungen = {}
+  let einstellungen
   const pool = new Pool({ connectionString: privat.mein_bk_db})
   pool.query(`SELECT * FROM einstellungen`).then(res => einstellungen = res.rows[0])
-  async function query () {
-    const resp = await pool.query(`SELECT schueler.*,
-                                          wlan.voucher,
-                                          wahlen.*
-                                   FROM schueler
-                                   LEFT JOIN wlan ON schueler.hashid = wlan.schueler_hashid
-                                   LEFT JOIN wahlen ON schueler.hashid = wahlen.schueler_hashid`)
-    schueler_entfernt = resp.rows
-    merged = mergeById(schueler_lokal, schueler_entfernt)
-  }
-  function lokal () {
-    const connection = mysql.createConnection(knexConfig.connection)
-    connection.connect()
-    connection.query(`SELECT ID, Name, Vorname, Klasse
-                      FROM schueler
-                      WHERE Status = 2 AND Geloescht = "-" AND Gesperrt = "-"
-                      ORDER BY Klasse, Name ASC`,
-      (e, res) => {
-        schueler_lokal = res
-      })
-    connection.end()
-  }
-  query() && lokal()
+  if (!einstellungen) pool.query(`INSERT INTO einstellungen DEFAULT VALUES RETURNING *`).then(res => einstellungen = res.rows[0])
+  pool.query(`SELECT schueler.*,
+                    wlan.voucher,
+                    wahlen.*
+              FROM schueler
+              LEFT JOIN wlan ON schueler.id = wlan.schueler_id
+              LEFT JOIN wahlen ON schueler.id = wahlen.schueler_id`)
+        .then(resp => {
+          schueler_entfernt = resp.rows
+        })
+  const connection = mysql.createConnection(knexConfig.connection)
+  connection.connect()
+  connection.query(`SELECT ID AS id, Name, Vorname, Klasse
+                    FROM schueler
+                    WHERE Status = 2 AND Geloescht = "-" AND Gesperrt = "-"
+                    ORDER BY Klasse, Name ASC`,
+    (e, res) => {
+      schueler_lokal = res
+    })
+  connection.end()
   function diff (eins, zwei) {
     let a = new Set(eins);
     let b = new Set(zwei);
     return new Set(
       [...a].filter(x => !b.has(x)));
   }
-  const mergeById = (a1, a2) =>
-    a1.map(itm => ({
-      ...a2.find((item) => (item.hashid === h(itm.ID)) && item),
-        ...itm
-    }));
-  function update_db () {}
-  // let schueler_filter
+  $: merged = schueler_lokal.map(itm => ({
+      ...schueler_entfernt.find((item) => (item.id === itm.id) && item),
+      ...itm}))
   $: schueler_filter = _.groupBy(merged.filter(s => s.Vorname.startsWith(suche) || s.Name.startsWith(suche)), 'Klasse')
+  function update_db () {}
 </script>