Fixed bug with inserting songs

This commit is contained in:
2024-08-05 20:35:16 +02:00
parent 892af71e19
commit 9d2c6895ec
2 changed files with 26 additions and 4 deletions

View File

@@ -31,6 +31,24 @@ func AddSong(song models.SongData) {
}
}
func CheckSong(path string) bool {
query, err := pool.Query(context.Background(),
"SELECT path FROM song WHERE path = $1", path)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
}
return query.Next()
}
func UpdateSong(songName string, fileName string, path string) {
_, err := pool.Exec(context.Background(),
"UPDATE song SET song_name=$1, file_name=$2 WHERE path = $3",
songName, fileName, path)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
}
}
func FindSongsFromGame(id int) []models.SongData {
rows, err := pool.Query(context.Background(),
"SELECT song_name, path, file_name, times_played FROM song WHERE game_id = $1", id)

View File

@@ -123,11 +123,15 @@ func checkSongs(gameDir string, gameId int) {
if err != nil {
log.Println(err)
}
path := gameDir + entry.Name()
fileName := entry.Name()
songName, _ := strings.CutSuffix(fileName, ".mp3")
if isSong(entry) {
db.AddSong(models.SongData{GameId: gameId, SongName: songName, Path: path, FileName: fileName})
path := gameDir + entry.Name()
fileName := entry.Name()
songName, _ := strings.CutSuffix(fileName, ".mp3")
if db.CheckSong(path) {
db.UpdateSong(songName, fileName, path)
} else {
db.AddSong(models.SongData{GameId: gameId, SongName: songName, Path: path, FileName: fileName})
}
} else if isCoverImage(entry) {
//TODO: Later add cover art image here in db
}