Changed the structure of the whole application, should be no changes to functionality.
This commit is contained in:
68
pkg/db/song.go
Normal file
68
pkg/db/song.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"music-server/pkg/models"
|
||||
"os"
|
||||
)
|
||||
|
||||
func ClearSongs(gameId int) {
|
||||
if gameId == -1 {
|
||||
_, err := pool.Exec(context.Background(), "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)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func AddSong(song models.SongData) {
|
||||
_, err := pool.Exec(context.Background(),
|
||||
"INSERT INTO song(game_id, song_name, path) VALUES ($1, $2, $3)",
|
||||
song.GameId, song.SongName, song.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, times_played FROM song WHERE game_id = $1", id)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
var songDataList []models.SongData
|
||||
for rows.Next() {
|
||||
var songName string
|
||||
var path string
|
||||
var timesPlayed int
|
||||
|
||||
err := rows.Scan(&songName, &path, ×Played)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
}
|
||||
|
||||
songDataList = append(songDataList, models.SongData{
|
||||
GameId: id,
|
||||
SongName: songName,
|
||||
Path: path,
|
||||
TimesPlayed: timesPlayed,
|
||||
})
|
||||
}
|
||||
return songDataList
|
||||
}
|
||||
|
||||
func AddSongPlayed(id int, name string) {
|
||||
_, err := pool.Exec(context.Background(),
|
||||
"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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user