infos.svelte 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <div class="box">
  2. <h4 class="title is-4">Neue Mitteilung erstellen</h4>
  3. <div class="field">
  4. <label class="label">Mitteilung</label>
  5. <div class="control">
  6. <textarea class="textarea" placeholder="Mitteilungstext" bind:value={neu.text}></textarea>
  7. </div>
  8. </div>
  9. <div class="field">
  10. <label class="label">Für Klassen – oder alle ('alle' eintippen!)</label>
  11. <div class="control">
  12. <input class="input" type="text" placeholder="z.B. C19A oder alle" bind:value={neu.klasse}>
  13. </div>
  14. </div>
  15. <div class="field">
  16. <label class="label">Datum</label>
  17. <div class="control">
  18. <input class="input" type="text" disabled value={neu.timestamp}>
  19. </div>
  20. </div>
  21. <div class="field is-grouped">
  22. <div class="control">
  23. <button class="button is-link" on:click={()=>save()}>Speichern</button>
  24. </div>
  25. </div>
  26. </div>
  27. <h4 class="title is-4">Bisherige Mitteilungen</h4>
  28. {#each info as i}
  29. <div class="box">
  30. <div class="field">
  31. <label class="label">Mitteilung</label>
  32. <div class="control">
  33. <textarea class="textarea" placeholder="Mitteilungstext" bind:value={i.text}></textarea>
  34. </div>
  35. </div>
  36. <div class="field">
  37. <label class="label">Für Klassen</label>
  38. <div class="control">
  39. <input class="input" type="text" placeholder="z.B. C19A, B19b, ..." bind:value={i.klasse}>
  40. </div>
  41. </div>
  42. <div class="field">
  43. <label class="label">Datum</label>
  44. <div class="control">
  45. <input class="input" type="text" disabled value={i.timestamp}>
  46. </div>
  47. </div>
  48. </div>
  49. <div class="field is-grouped">
  50. <div class="control">
  51. <button class="button is-link" on:click={()=>update(i)}>Änderung speichern</button>
  52. </div>
  53. <div class="control">
  54. <button class="button is-text" on:click={()=>remove(i.id)}>Löschen</button>
  55. </div>
  56. </div>
  57. {/each}
  58. <script>
  59. export let privat
  60. let neu = {}, info = []
  61. const { Pool } = R('pg')
  62. const pool = new Pool({ connectionString: privat.mein_bk_db})
  63. const update = async i => {
  64. const query = `UPDATE infos SET text = $1, klasse = $2, timestamp = $3 WHERE id = $4 RETURNING *`
  65. const values = [i.text, i.klasse, new Date(), i.id]
  66. try {
  67. const res = await pool.query(query, values)
  68. i = info.find(e=>e.id === i.id)
  69. i = res.rows[0]
  70. } catch(err) {
  71. console.log(err.stack)
  72. }
  73. }
  74. const remove = async id => {
  75. const query = `DELETE FROM infos WHERE id = $1 RETURNING *`
  76. const values = [id]
  77. try {
  78. const res = await pool.query(query, values)
  79. info = info.filter(e => e.id !== id)
  80. } catch(err) {
  81. console.log(err.stack)
  82. }
  83. }
  84. const save = async () => {
  85. const query = `INSERT INTO infos (text, klasse, timestamp) VALUES ($1, $2, $3) RETURNING *`
  86. const values = [neu.text, neu.klasse, new Date()]
  87. try {
  88. const res = await pool.query(query, values)
  89. info.unshift(res.rows[0])
  90. } catch(err) {
  91. console.log(err.stack)
  92. }
  93. }
  94. const get_infos = async () => {
  95. const query = `SELECT * FROM infos ORDER BY timestamp DESC`
  96. try {
  97. const res = await pool.query(query)
  98. info = res.rows
  99. } catch(err) {
  100. console.log(err.stack)
  101. }
  102. }
  103. get_infos()
  104. </script>