vouchers.svelte 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. function vouchers_value () {
  50. const arr = Papa.parse(vouchers, {comments: '#'})
  51. return arr.filter(e=>!e.startsWith('#')).map(e=>'(\'' + e.substring(2,13) + '\')').join(',')
  52. }
  53. const update_vouchers = async () => {
  54. const res = await Papa.parse(vouchers, {comments: '#'})
  55. const values = res.data.map(e => '(\'' + e[0].trim() + '\')').join(',')
  56. const query = `INSERT INTO wlan (voucher)
  57. VALUES ${values}
  58. RETURNING *`
  59. console.log(values)
  60. try {
  61. await pool.query(`DELETE FROM wlan`)
  62. const res = await pool.query(query)
  63. console.log(res.rows)
  64. } catch(err) {
  65. console.log(err.stack)
  66. }
  67. }
  68. </script>