-
Notifications
You must be signed in to change notification settings - Fork 0
/
songupdate.go
76 lines (62 loc) · 1.59 KB
/
songupdate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"database/sql"
"encoding/json"
"fmt"
"log"
"net/http"
)
func songUpdate(w http.ResponseWriter, r *http.Request) {
type httpResponse struct {
Response string `json:"response"`
Reason string `json:"reason"`
}
var h httpResponse
var err error
db, err := dbConnection()
if err != nil {
log.Println("Connection to database failed: ", err)
}
image, err := sendToCDN(r.FormValue("image"))
if err != nil {
log.Printf("Failed to send image to CDN: %s", err)
h.Response = "fail"
h.Reason = fmt.Sprintf("Failed to update db: %s", err)
json.NewEncoder(w).Encode(h)
return
}
err = songUpdateDbCall(db, r.FormValue("filename"), r.FormValue("artist"), r.FormValue("title"), r.FormValue("url"), image)
if err != nil {
log.Printf("Update failed: %s", err)
h.Response = "fail"
h.Reason = fmt.Sprintf("Failed to update db: %s", err)
json.NewEncoder(w).Encode(h)
return
}
h.Response = "success"
h.Reason = fmt.Sprintf("Updated field %s", r.FormValue("filename"))
err = generatePlaylist(db)
if err != nil {
log.Println("Error updating the playlist ", err)
}
json.NewEncoder(w).Encode(h)
return
}
func songUpdateDbCall(db *sql.DB, filename string, artist string, title string, share string, image string) error {
tx, err := db.Begin()
if err != nil {
return err
}
defer func() {
switch err {
case nil:
err = tx.Commit()
default:
tx.Rollback()
}
}()
if _, err = tx.Exec("UPDATE details SET artist = ?, title = ?, share = ?, image = ? WHERE filename = ?", artist, title, share, image, filename); err != nil {
return err
}
return nil
}