Added concurrent sync, added search page. Other small changes

This commit is contained in:
2024-10-04 20:19:40 +02:00
parent fafa044c9b
commit 8fa93d580d
35 changed files with 1901 additions and 89 deletions

View File

@@ -1,21 +1,23 @@
package db
import (
"context"
"errors"
"fmt"
"music-server/pkg/models"
"os"
"strings"
)
var compareError = errors.New("no rows in result set")
func ClearSongs(gameId int) {
if gameId == -1 {
_, err := pool.Exec(context.Background(), "DELETE FROM song")
_, err := dbpool.Exec(ctx, "DELETE FROM song")
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
}
} else {
_, err := pool.Exec(context.Background(), "DELETE FROM song where game_id=$1", gameId)
_, err := dbpool.Exec(ctx, "DELETE FROM song where game_id=$1", gameId)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
}
@@ -23,7 +25,7 @@ func ClearSongs(gameId int) {
}
func AddSong(song models.SongData) {
_, err := pool.Exec(context.Background(),
_, err := dbpool.Exec(ctx,
"INSERT INTO song(game_id, song_name, path, file_name) VALUES ($1, $2, $3, $4)",
song.GameId, song.SongName, song.Path, song.FileName)
if err != nil {
@@ -33,17 +35,18 @@ func AddSong(song models.SongData) {
func CheckSong(songPath string) bool {
var path string
err := pool.QueryRow(context.Background(),
err := dbpool.QueryRow(ctx,
"SELECT path FROM song WHERE path = $1", songPath).Scan(&path)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
if compareError.Error() != err.Error() {
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
}
}
println("CheckSong path", path)
return path != ""
}
func UpdateSong(songName string, fileName string, path string) {
_, err := pool.Exec(context.Background(),
_, err := dbpool.Exec(ctx,
"UPDATE song SET song_name=$1, file_name=$2 WHERE path = $3",
songName, fileName, path)
if err != nil {
@@ -52,10 +55,12 @@ func UpdateSong(songName string, fileName string, path string) {
}
func FindSongsFromGame(id int) []models.SongData {
rows, err := pool.Query(context.Background(),
rows, err := dbpool.Query(ctx,
"SELECT song_name, path, file_name, times_played FROM song WHERE game_id = $1", id)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
if compareError.Error() != err.Error() {
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
}
return nil
}
@@ -68,7 +73,9 @@ func FindSongsFromGame(id int) []models.SongData {
err := rows.Scan(&songName, &path, &fileName, &timesPlayed)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
if compareError.Error() != err.Error() {
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
}
}
songDataList = append(songDataList, models.SongData{
@@ -83,7 +90,7 @@ func FindSongsFromGame(id int) []models.SongData {
}
func AddSongPlayed(id int, name string) {
_, err := pool.Exec(context.Background(),
_, err := dbpool.Exec(ctx,
"UPDATE song SET times_played=times_played+1 WHERE game_id=$1 AND song_name=$2", id, name)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
@@ -91,10 +98,12 @@ func AddSongPlayed(id int, name string) {
}
func FetchAllSongs() []models.SongData {
rows, err := pool.Query(context.Background(),
rows, err := dbpool.Query(ctx,
"SELECT song_name, path FROM song")
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
if compareError.Error() != err.Error() {
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
}
return nil
}
@@ -105,7 +114,9 @@ func FetchAllSongs() []models.SongData {
err := rows.Scan(&songName, &path)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
if compareError.Error() != err.Error() {
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
}
}
songDataList = append(songDataList, models.SongData{
@@ -117,7 +128,7 @@ func FetchAllSongs() []models.SongData {
}
func RemoveBrokenSong(song models.SongData) {
_, err := pool.Exec(context.Background(), "DELETE FROM song where path=$1", song.Path)
_, err := dbpool.Exec(ctx, "DELETE FROM song where path=$1", song.Path)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
}
@@ -130,7 +141,7 @@ func RemoveBrokenSongs(songs []models.SongData) {
}
joined = strings.TrimSuffix(joined, ",")
_, err := pool.Exec(context.Background(), "DELETE FROM song where path in ($1)", joined)
_, err := dbpool.Exec(ctx, "DELETE FROM song where path in ($1)", joined)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
}