Ver código fonte

Fix playlist not updating properly

Toni Fadjukoff 6 anos atrás
pai
commit
efacc315b0
2 arquivos alterados com 15 adições e 3 exclusões
  1. 3 3
      db.go
  2. 12 0
      db_test.go

+ 3 - 3
db.go Ver arquivo

@@ -212,18 +212,18 @@ func (db *DB) FindPlaylistBySection(sectionName string) ([]string, error) {
212 212
 }
213 213
 
214 214
 func (db *DB) UpdatePlaylistBySection(sectionName string, tracks []string) (bool, error) {
215
-	tracksJson, err := json.Marshal(tracks)
215
+	tracksJSON, err := json.Marshal(tracks)
216 216
 	if err != nil {
217 217
 		return false, err
218 218
 	}
219 219
 	query := `
220 220
 	INSERT INTO public.round_playlist
221
-	SELECT r.id, ARRAY(SELECT e::text FROM json_array_elements($2::json) e)
221
+	SELECT r.id, ARRAY(SELECT e::text FROM json_array_elements_text($2::json) e)
222 222
 	FROM public.round r 
223 223
 	WHERE r.section = $1
224 224
 	ON CONFLICT (round_id) DO UPDATE SET tracks = EXCLUDED.tracks`
225 225
 
226
-	res, err := db.database.Exec(query, sectionName, tracksJson)
226
+	res, err := db.database.Exec(query, sectionName, tracksJSON)
227 227
 
228 228
 	if err != nil {
229 229
 		return false, err

+ 12 - 0
db_test.go Ver arquivo

@@ -90,6 +90,18 @@ func TestUpdatePlaylistBySection(t *testing.T) {
90 90
 	if len(playlistEntries) != 4 {
91 91
 		t.Error("Should have found 4 playlistEntries, got", len(playlistEntries))
92 92
 	}
93
+	if playlistEntries[0] != "ID1" {
94
+		t.Error("Should have been ID1, got ", playlistEntries[0])
95
+	}
96
+	if playlistEntries[1] != "ID2" {
97
+		t.Error("Should have been ID2, got ", playlistEntries[1])
98
+	}
99
+	if playlistEntries[2] != "ID3" {
100
+		t.Error("Should have been ID3, got ", playlistEntries[2])
101
+	}
102
+	if playlistEntries[3] != "ID4" {
103
+		t.Error("Should have been ID4, got ", playlistEntries[3])
104
+	}
93 105
 }
94 106
 func TestUpdatePlaylistBySectionUnknownSection(t *testing.T) {
95 107
 	initTestCase(t)