db.go 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. }