db.go 5.4KB

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