db.go 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. package main
  2. import (
  3. "database/sql"
  4. "errors"
  5. _ "github.com/lib/pq"
  6. "log"
  7. )
  8. type DB struct {
  9. database *sql.DB
  10. }
  11. func InitDatabase() *DB {
  12. connStr := "user=levyraati dbname=levyraati sslmode=disable"
  13. var err error
  14. database, err := sql.Open("postgres", connStr)
  15. if err != nil {
  16. log.Fatal(err)
  17. }
  18. _, err = database.Query("SELECT 1")
  19. if err != nil {
  20. log.Fatal(err)
  21. }
  22. return &DB{database}
  23. }
  24. func (db *DB) FindHashForUser(username string) (string, error) {
  25. var hash string
  26. err := db.database.QueryRow("SELECT u.password FROM public.user u WHERE lower(u.username) = lower($1)", username).Scan(&hash)
  27. return hash, err
  28. }
  29. func (db *DB) EntrySynced(userId, roundId int) (bool, error) {
  30. query := `UPDATE public.entry SET synced = true WHERE user_id = $1 AND round_id = $2`
  31. res, err := db.database.Exec(query, userId, roundId)
  32. if err != nil {
  33. return false, err
  34. }
  35. affected, err := res.RowsAffected()
  36. if err != nil {
  37. return false, err
  38. }
  39. if affected != 1 {
  40. return false, errors.New("Unknown entry ID")
  41. }
  42. return true, nil
  43. }
  44. func (db *DB) FindEntriesToSync() ([]*EntryToSync, error) {
  45. query := `
  46. SELECT e.user_id, e.round_id, e.artist, e.title, e.spotify_url, p.article, u.username, r.section
  47. FROM public.entry e
  48. JOIN public."user" u ON u.id = e.user_id
  49. JOIN public.round r ON r.id = e.round_id
  50. JOIN public.panel p ON p.id = r.panel_id
  51. WHERE r.start < current_timestamp AND e.synced = false`
  52. rows, err := db.database.Query(query)
  53. if err != nil {
  54. log.Println("Error while reading songs from database:", err)
  55. return nil, err
  56. }
  57. defer rows.Close()
  58. var entries []*EntryToSync
  59. for rows.Next() {
  60. var (
  61. userId, roundId int
  62. artist, title, spotifyURL, article, username, section string
  63. )
  64. err := rows.Scan(&userId, &roundId, &artist, &title, &spotifyURL, &article, &username, &section)
  65. if err != nil {
  66. log.Println("Error while scanning row:", err)
  67. return nil, err
  68. }
  69. entries = append(entries, &EntryToSync{userId, roundId, artist, title, spotifyURL, article, username, section})
  70. }
  71. err = rows.Err()
  72. if err != nil {
  73. log.Println("Error after reading cursor:", err)
  74. return nil, err
  75. }
  76. return entries, nil
  77. }
  78. type EntryToSync struct {
  79. userId, roundId int
  80. artist, title, spotifyURL, article, username, section string
  81. }
  82. func (db *DB) FindAllEntries(username string) ([]*Song, error) {
  83. var songs []*Song
  84. query := `
  85. SELECT r.id, r.section, e.artist, e.title, e.spotify_url, e.synced
  86. FROM public.round r
  87. LEFT JOIN public.entry e ON r.id = e.round_id
  88. LEFT JOIN public."user" u ON u.id = e.user_id AND lower(u.username) = lower($1)
  89. ORDER BY r.start ASC`
  90. rows, err := db.database.Query(query, username)
  91. if err != nil {
  92. return nil, err
  93. }
  94. defer rows.Close()
  95. for i := 0; rows.Next(); i++ {
  96. song := &Song{}
  97. songs = append(songs, song)
  98. var (
  99. artist, title, url *string
  100. sync *bool
  101. )
  102. err = rows.Scan(&songs[i].RoundID, &songs[i].RoundName, &artist, &title, &url, &sync)
  103. if err != nil {
  104. return nil, err
  105. }
  106. if artist != nil {
  107. song.Artist = *artist
  108. }
  109. if title != nil {
  110. song.Title = *title
  111. }
  112. if url != nil {
  113. song.URL = *url
  114. }
  115. if sync != nil {
  116. song.Sync = *sync
  117. }
  118. }
  119. return songs, nil
  120. }
  121. type Song struct {
  122. RoundID int
  123. RoundName string
  124. Title string
  125. Artist string
  126. URL string
  127. Sync bool
  128. }
  129. func (db *DB) UpdateEntry(username, round, artist, title, url string) (bool, error) {
  130. query := `
  131. INSERT INTO public.entry
  132. SELECT id, $2, $3, $4, $5, false
  133. FROM public."user" u
  134. WHERE lower(u.username) = lower($1)
  135. ON CONFLICT (user_id, round_id) DO UPDATE SET artist = EXCLUDED.artist, title = EXCLUDED.title, spotify_url = EXCLUDED.spotify_url, synced = EXCLUDED.synced`
  136. res, err := db.database.Exec(query, username, round, artist, title, url)
  137. if err != nil {
  138. return false, err
  139. }
  140. affected, err := res.RowsAffected()
  141. if err != nil {
  142. return false, err
  143. }
  144. if affected != 1 {
  145. return false, nil
  146. }
  147. return true, nil
  148. }
  149. func (db *DB) FindAllPanels() ([]string, error) {
  150. query := `
  151. SELECT name FROM public.panel`
  152. rows, err := db.database.Query(query)
  153. if err != nil {
  154. return nil, err
  155. }
  156. defer rows.Close()
  157. names := make([]string, 0)
  158. for i := 0; rows.Next(); i++ {
  159. var name string
  160. err := rows.Scan(&name)
  161. if err != nil {
  162. return nil, err
  163. }
  164. names = append(names, name)
  165. }
  166. return names, nil
  167. }
  168. func (db *DB) FindPlaylistBySection(sectionName string) ([]string, error) {
  169. query := `
  170. SELECT pl.tracks FROM public.round_playlist pl
  171. JOIN public.round r ON pl.round_id = r.id
  172. WHERE r.section = $1`
  173. row := db.database.QueryRow(query, sectionName)
  174. var tracks []string
  175. err := row.Scan(&tracks)
  176. if err != nil {
  177. return nil, err
  178. }
  179. return tracks, nil
  180. }
  181. func (db *DB) UpdatePlaylistBySection(sectionName string, tracks []string) (bool, error) {
  182. query := `
  183. INSERT INTO public.round_playlist pl
  184. SELECT r.id, $2
  185. FROM public.round r
  186. WHERE r.section = $1
  187. ON CONFLICT (pl.round_id) DO UPDATE SET tracks = EXCLUDED.tracks`
  188. res, err := db.database.Exec(query, sectionName, tracks)
  189. if err != nil {
  190. return false, err
  191. }
  192. affected, err := res.RowsAffected()
  193. if err != nil {
  194. return false, err
  195. }
  196. if affected != 1 {
  197. return false, nil
  198. }
  199. return true, nil
  200. }