vouchers.svelte 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <div class="columns">
  2. <div class="column is-two-thirds">
  3. {#each Object.entries(schueler) as [klasse, schueler]}
  4. <h3 class="title">{klasse}</h3>
  5. <table class="table is-striped">
  6. <thead>
  7. <tr>
  8. <th>Name</th>
  9. <th>Vorname</th>
  10. <th>Voucher</th>
  11. </tr>
  12. </thead>
  13. <tbody>
  14. {#each schueler as s}
  15. <tr>
  16. <td>{s.Name}</td>
  17. <td>{s.Vorname}</td>
  18. <td><span class={s.voucher && "tag is-info is-medium"}>{s.voucher || ''}</span></td>
  19. </tr>
  20. {/each}
  21. </tbody>
  22. </table>
  23. {/each}
  24. </div>
  25. <div class="column">
  26. <h3 class="title">Voucher austauschen</h3>
  27. <div class="field">
  28. <label class="label">
  29. Voucher als Liste zeilenweise in das Textfeld einfügen. Alte Voucher
  30. werden entfernt und durch die neuen ersetzt.
  31. </label>
  32. <div class="control">
  33. <textarea class="textarea" bind:value={vouchers} rows="10" cols="20"></textarea>
  34. </div>
  35. </div>
  36. <div class="field is-grouped">
  37. <div class="control">
  38. <button class="button is-link" on:click={()=>update_vouchers()}>Aktualisieren</button>
  39. </div>
  40. </div>
  41. </div>
  42. </div>
  43. <script>
  44. import Papa from '../papaparse.min.js'
  45. export let schueler, privat
  46. const { Pool } = R('pg')
  47. const pool = new Pool({ connectionString: privat.mein_bk_db})
  48. let vouchers = ''
  49. const update_vouchers = async () => {
  50. const res = await Papa.parse(vouchers, {comments: '#'})
  51. const values = res.data.map(e => '(\'' + e[0].trim() + '\')').join(',')
  52. const query = `INSERT INTO wlan (voucher)
  53. VALUES ${values}
  54. RETURNING *`
  55. console.log(values)
  56. try {
  57. await pool.query(`DELETE FROM wlan`)
  58. const res = await pool.query(query)
  59. console.log(res.rows)
  60. } catch(err) {
  61. console.log(err.stack)
  62. }
  63. }
  64. </script>