Skip to content

Commit af09e56

Browse files
authored
Fix/32 better structure endpoints (#40)
* fix killing each other * less bulky main * description, unused method * base database * clean database structure * Updated db structure * ignore...
1 parent db69abc commit af09e56

23 files changed

Lines changed: 626 additions & 570 deletions

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,5 @@ music/
7979
music_dev/
8080
*.db
8181
*.env
82-
database/data
82+
database/data
83+
*.pid

MyMusicBoxApi/configuration/util.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,14 @@ import (
88

99
var Config models.Config
1010

11-
func LoadConfig() {
12-
flag.StringVar(&Config.DevPort, "port", "", "Development port else use default port")
13-
flag.BoolVar(&Config.UseDevUrl, "devurl", false, "Have a dev prefix in the url")
14-
flag.StringVar(&Config.SourceFolder, "sourceFolder", "music", "Output folder for data")
15-
flag.StringVar(&Config.OutputExtension, "outputExtension", "opus", "Extension for ouput file")
11+
func LoadConfiguration() {
12+
flag.StringVar(&Config.DevPort, "port", "", "-port=8081")
13+
flag.BoolVar(&Config.UseDevUrl, "devurl", false, "-devurl")
14+
flag.StringVar(&Config.SourceFolder, "sourceFolder", "music", "-sourceFolder=/path to source folder/")
15+
flag.StringVar(&Config.OutputExtension, "outputExtension", "opus", "-outputExtension=opus,mp3,mp4 etc")
1616
flag.Parse()
1717
}
1818

19-
func GetApiGroupUrlV1() string {
20-
if Config.UseDevUrl {
21-
return "/dev/api/v1"
22-
} else {
23-
return "/api/v1"
24-
}
25-
}
26-
2719
func GetApiGroupUrl(version string) string {
2820
if Config.UseDevUrl {
2921
return fmt.Sprintf("/dev/api/%s", version)

MyMusicBoxApi/database/db.go

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package database
2+
3+
import (
4+
"database/sql"
5+
"errors"
6+
"fmt"
7+
"musicboxapi/configuration"
8+
"musicboxapi/logging"
9+
"os"
10+
"strings"
11+
"time"
12+
13+
_ "github.com/lib/pq"
14+
)
15+
16+
var DbInstance *sql.DB
17+
18+
type BaseTable struct {
19+
DB *sql.DB
20+
}
21+
22+
func NewBaseTableInstance() BaseTable {
23+
return BaseTable{
24+
DB: DbInstance,
25+
}
26+
}
27+
28+
func CreateDatabasConnectionPool() error {
29+
30+
// Will throw an error if its missing a method implementation from interface
31+
// will throw a compile time error
32+
var _ ISongTable = (*SongTable)(nil)
33+
var _ IPlaylistTable = (*PlaylistTable)(nil)
34+
var _ IPlaylistsongTable = (*PlaylistsongTable)(nil)
35+
var _ ITasklogTable = (*TasklogTable)(nil)
36+
37+
baseConnectionString := "user=postgres dbname=postgres password=%s %s sslmode=disable"
38+
password := os.Getenv("POSTGRES_PASSWORD")
39+
host := "host=127.0.0.1 port=5432"
40+
41+
if configuration.Config.UseDevUrl {
42+
host = "host=127.0.0.1 port=5433"
43+
}
44+
45+
connectionString := fmt.Sprintf(baseConnectionString, password, host)
46+
47+
DB, err := sql.Open("postgres", connectionString)
48+
49+
if err != nil {
50+
logging.Error(fmt.Sprintf("Failed to init database connection: %s", err.Error()))
51+
return err
52+
}
53+
54+
DB.SetMaxOpenConns(10)
55+
DB.SetMaxIdleConns(5)
56+
DB.SetConnMaxIdleTime(1 * time.Minute)
57+
DB.SetConnMaxLifetime(5 * time.Minute)
58+
59+
DbInstance = DB
60+
61+
return nil
62+
}
63+
64+
// Base methods
65+
func (base *BaseTable) InsertWithReturningId(query string, params ...any) (lastInsertedId int, err error) {
66+
67+
if !strings.Contains(query, "RETURNING") {
68+
logging.Error("Query does not contain RETURNING keyword")
69+
return -1, errors.New("Query does not contain RETURNING keyword")
70+
}
71+
72+
transaction, err := base.DB.Begin()
73+
74+
statement, err := transaction.Prepare(query)
75+
76+
if err != nil {
77+
transaction.Rollback()
78+
logging.Error(fmt.Sprintf("Prepared statement error: %s", err.Error()))
79+
return -1, err
80+
}
81+
defer statement.Close()
82+
83+
err = statement.QueryRow(params...).Scan(&lastInsertedId)
84+
85+
if err != nil {
86+
logging.Error(fmt.Sprintf("Queryrow error: %s", err.Error()))
87+
transaction.Rollback()
88+
return -1, err
89+
}
90+
91+
err = transaction.Commit()
92+
93+
if err != nil {
94+
logging.Error(fmt.Sprintf("Transaction commit error: %s", err.Error()))
95+
transaction.Rollback()
96+
return -1, err
97+
}
98+
99+
return lastInsertedId, nil
100+
}
101+
func (base *BaseTable) NonScalarQuery(query string, params ...any) (error error) {
102+
103+
transaction, err := base.DB.Begin()
104+
105+
if err != nil {
106+
logging.Error(fmt.Sprintf("Transaction error: %s", err.Error()))
107+
return err
108+
}
109+
110+
statement, err := transaction.Prepare(query)
111+
112+
if err != nil {
113+
logging.Error(fmt.Sprintf("Prepared statement error: %s", err.Error()))
114+
return err
115+
}
116+
117+
defer statement.Close()
118+
119+
_, err = statement.Exec(params...)
120+
121+
if err != nil {
122+
logging.Error(fmt.Sprintf("Exec error: %s", err.Error()))
123+
logging.Error(fmt.Sprintf("Query: %s", query))
124+
for index := range params {
125+
logging.Error(params[index])
126+
}
127+
return err
128+
}
129+
130+
err = transaction.Commit()
131+
132+
if err != nil {
133+
logging.Error(fmt.Sprintf("Transaction commit error: %s", err.Error()))
134+
return err
135+
}
136+
137+
return nil
138+
}

MyMusicBoxApi/database/playlist.go

Lines changed: 0 additions & 62 deletions
This file was deleted.

MyMusicBoxApi/database/playlistsong.go

Lines changed: 0 additions & 80 deletions
This file was deleted.

0 commit comments

Comments
 (0)