Changed the structure of the whole application, should be no changes to functionality.
This commit is contained in:
61
pkg/db/dbHelper.go
Normal file
61
pkg/db/dbHelper.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/jackc/pgx/v4/pgxpool"
|
||||
"os"
|
||||
)
|
||||
|
||||
var pool *pgxpool.Pool
|
||||
|
||||
func InitDB(host string, port int, user string, password string, dbname string) {
|
||||
|
||||
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
|
||||
"password=%s dbname=%s sslmode=disable",
|
||||
host, port, user, password, dbname)
|
||||
|
||||
fmt.Println(psqlInfo)
|
||||
|
||||
var err error
|
||||
pool, err = pgxpool.Connect(context.Background(), psqlInfo)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
var success string
|
||||
err = pool.QueryRow(context.Background(), "select 'Successfully connected!'").Scan(&success)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Println(success)
|
||||
}
|
||||
|
||||
func CloseDb() {
|
||||
pool.Close()
|
||||
}
|
||||
|
||||
func Testf() {
|
||||
rows, dbErr := pool.Query(context.Background(), "select game_name from game")
|
||||
if dbErr != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", dbErr)
|
||||
os.Exit(1)
|
||||
}
|
||||
for rows.Next() {
|
||||
var gameName string
|
||||
dbErr = rows.Scan(&gameName)
|
||||
if dbErr != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", dbErr)
|
||||
}
|
||||
_, _ = fmt.Fprintf(os.Stderr, "%v\n", gameName)
|
||||
}
|
||||
}
|
||||
|
||||
func resetGameIdSeq() {
|
||||
_, err := pool.Query(context.Background(), "SELECT setval('game_id_seq', (SELECT MAX(id) FROM game)+1);")
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
|
||||
}
|
||||
}
|
||||
133
pkg/db/game.go
Normal file
133
pkg/db/game.go
Normal file
@@ -0,0 +1,133 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/jackc/pgtype"
|
||||
"music-server/pkg/models"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
func GetGameName(gameId int) string {
|
||||
var gameName = ""
|
||||
err := pool.QueryRow(context.Background(),
|
||||
"SELECT game_name FROM game WHERE id = $1", gameId).Scan(&gameName)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
return ""
|
||||
}
|
||||
return gameName
|
||||
}
|
||||
|
||||
func SetGameDeletionDate() {
|
||||
_, err := pool.Exec(context.Background(),
|
||||
"UPDATE game SET deleted=$1", time.Now())
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func ClearGames() {
|
||||
_, err := pool.Exec(context.Background(), "DELETE FROM game")
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func UpdateGameName(id int, name string, path string) {
|
||||
_, err := pool.Exec(context.Background(),
|
||||
"UPDATE game SET game_name=$1, path=$2, last_changed=$3 WHERE id=$4",
|
||||
name, path, time.Now(), id)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func RemoveDeletionDate(id int) {
|
||||
_, err := pool.Exec(context.Background(),
|
||||
"UPDATE game SET deleted=null WHERE id=$1", id)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func GetIdByGameName(name string) int {
|
||||
var gameId = -1
|
||||
err := pool.QueryRow(context.Background(),
|
||||
"SELECT id FROM game WHERE game_name = $1", name).Scan(&gameId)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
return -1
|
||||
}
|
||||
return gameId
|
||||
}
|
||||
|
||||
func InsertGame(name string, path string) int {
|
||||
gameId := -1
|
||||
err := pool.QueryRow(context.Background(),
|
||||
"INSERT INTO game(game_name, path, added) VALUES ($1, $2, $3) RETURNING id",
|
||||
name, path, time.Now()).Scan(&gameId)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
resetGameIdSeq()
|
||||
err2 := pool.QueryRow(context.Background(),
|
||||
"INSERT INTO game(game_name, path, added) VALUES ($1, $2, $3) RETURNING id",
|
||||
name, path, time.Now()).Scan(&gameId)
|
||||
if err2 != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
return -1
|
||||
}
|
||||
}
|
||||
return gameId
|
||||
}
|
||||
|
||||
func InsertGameWithExistingId(id int, name string, path string) {
|
||||
_, err := pool.Exec(context.Background(),
|
||||
"INSERT INTO game(id, game_name, path, added) VALUES ($1, $2, $3, $4)",
|
||||
id, name, path, time.Now())
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func FindAllGames() []models.GameData {
|
||||
rows, err := pool.Query(context.Background(),
|
||||
"SELECT id, game_name, added, deleted, last_changed, path, times_played, last_played, number_of_songs "+
|
||||
"FROM game "+
|
||||
"ORDER BY game_name")
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
}
|
||||
var gameList []models.GameData
|
||||
for rows.Next() {
|
||||
var id, timesPlayed int
|
||||
var numberOfSongs pgtype.Int4
|
||||
var gameName, path string
|
||||
var added, deleted, lastChanged, lastPlayed pgtype.Timestamp
|
||||
err := rows.Scan(&id, &gameName, &added, &deleted, &lastChanged, &path, ×Played, &lastPlayed, &numberOfSongs)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
}
|
||||
gameList = append(gameList, models.GameData{
|
||||
Id: id,
|
||||
GameName: gameName,
|
||||
Added: added.Time,
|
||||
Deleted: deleted.Time,
|
||||
LastChanged: lastChanged.Time,
|
||||
Path: path,
|
||||
TimesPlayed: timesPlayed,
|
||||
LastPlayed: lastPlayed.Time,
|
||||
NumberOfSongs: numberOfSongs.Int,
|
||||
})
|
||||
}
|
||||
return gameList
|
||||
}
|
||||
|
||||
func AddGamePlayed(id int) {
|
||||
_, err := pool.Exec(context.Background(),
|
||||
"UPDATE game SET times_played=times_played+1, last_played=now() WHERE id=$1", id)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
|
||||
}
|
||||
}
|
||||
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