HEAVY: migrate accounts and logs to service/repo architecture
This commit is contained in:
parent
5c255fb34b
commit
49e14b5bc5
37 changed files with 1019 additions and 687 deletions
21
api/api.go
21
api/api.go
|
|
@ -1,17 +1,18 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"arimelody-web/controller"
|
||||
"arimelody-web/model"
|
||||
"arimelody-web/controller"
|
||||
"arimelody-web/model"
|
||||
"arimelody-web/model/app"
|
||||
)
|
||||
|
||||
func Handler(app *model.AppState) http.Handler {
|
||||
func Handler(app *app.AppState) http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
// TODO: generate API keys on the frontend
|
||||
|
|
@ -166,7 +167,7 @@ func requireAccount(next http.Handler) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func getSession(app *model.AppState, r *http.Request) (*model.Session, error) {
|
||||
func getSession(app *app.AppState, r *http.Request) (*model.Session, error) {
|
||||
var token string
|
||||
|
||||
// check cookies first
|
||||
|
|
@ -184,7 +185,7 @@ func getSession(app *model.AppState, r *http.Request) (*model.Session, error) {
|
|||
if token == "" { return nil, nil }
|
||||
|
||||
// fetch existing session
|
||||
session, err := controller.GetSession(app.DB, token)
|
||||
session, err := controller.GetSession(app, token)
|
||||
|
||||
if err != nil && !strings.Contains(err.Error(), "no rows") {
|
||||
return nil, fmt.Errorf("Failed to retrieve session: %v\n", err)
|
||||
|
|
|
|||
|
|
@ -1,21 +1,21 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"arimelody-web/controller"
|
||||
"arimelody-web/log"
|
||||
"arimelody-web/model"
|
||||
"arimelody-web/controller"
|
||||
"arimelody-web/model"
|
||||
"arimelody-web/model/app"
|
||||
)
|
||||
|
||||
func ServeAllArtists(app *model.AppState) http.Handler {
|
||||
func ServeAllArtists(app *app.AppState) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var artists = []*model.Artist{}
|
||||
artists, err := controller.GetAllArtists(app.DB)
|
||||
|
|
@ -35,7 +35,7 @@ func ServeAllArtists(app *model.AppState) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func ServeArtist(app *model.AppState, artist *model.Artist) http.Handler {
|
||||
func ServeArtist(app *app.AppState, artist *model.Artist) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
type (
|
||||
creditJSON struct {
|
||||
|
|
@ -87,7 +87,7 @@ func ServeArtist(app *model.AppState, artist *model.Artist) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func CreateArtist(app *model.AppState) http.Handler {
|
||||
func CreateArtist(app *app.AppState) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
session := r.Context().Value("session").(*model.Session)
|
||||
|
||||
|
|
@ -115,13 +115,13 @@ func CreateArtist(app *model.AppState) http.Handler {
|
|||
return
|
||||
}
|
||||
|
||||
app.Log.Info(log.TYPE_ARTIST, "Artist \"%s\" created by \"%s\".", artist.Name, session.Account.Username)
|
||||
app.Log.Info(model.LOG_ARTIST, "Artist \"%s\" created by \"%s\".", artist.Name, session.Account.Username)
|
||||
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
})
|
||||
}
|
||||
|
||||
func UpdateArtist(app *model.AppState, artist *model.Artist) http.Handler {
|
||||
func UpdateArtist(app *app.AppState, artist *model.Artist) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
session := r.Context().Value("session").(*model.Session)
|
||||
|
||||
|
|
@ -166,11 +166,11 @@ func UpdateArtist(app *model.AppState, artist *model.Artist) http.Handler {
|
|||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
app.Log.Info(log.TYPE_ARTIST, "Artist \"%s\" updated by \"%s\".", artist.Name, session.Account.Username)
|
||||
app.Log.Info(model.LOG_ARTIST, "Artist \"%s\" updated by \"%s\".", artist.Name, session.Account.Username)
|
||||
})
|
||||
}
|
||||
|
||||
func DeleteArtist(app *model.AppState, artist *model.Artist) http.Handler {
|
||||
func DeleteArtist(app *app.AppState, artist *model.Artist) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
session := r.Context().Value("session").(*model.Session)
|
||||
|
||||
|
|
@ -184,6 +184,6 @@ func DeleteArtist(app *model.AppState, artist *model.Artist) http.Handler {
|
|||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
app.Log.Info(log.TYPE_ARTIST, "Artist \"%s\" deleted by \"%s\".", artist.Name, session.Account.Username)
|
||||
app.Log.Info(model.LOG_ARTIST, "Artist \"%s\" deleted by \"%s\".", artist.Name, session.Account.Username)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,21 +1,21 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"arimelody-web/controller"
|
||||
"arimelody-web/log"
|
||||
"arimelody-web/model"
|
||||
"arimelody-web/controller"
|
||||
"arimelody-web/model"
|
||||
"arimelody-web/model/app"
|
||||
)
|
||||
|
||||
func ServeRelease(app *model.AppState, release *model.Release) http.Handler {
|
||||
func ServeRelease(app *app.AppState, release *model.Release) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// only allow authorised users to view hidden releases
|
||||
privileged := false
|
||||
|
|
@ -127,7 +127,7 @@ func ServeRelease(app *model.AppState, release *model.Release) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func ServeCatalog(app *model.AppState) http.Handler {
|
||||
func ServeCatalog(app *app.AppState) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
releases, err := controller.GetAllReleases(app.DB, false, 0, true)
|
||||
if err != nil {
|
||||
|
|
@ -188,7 +188,7 @@ func ServeCatalog(app *model.AppState) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func CreateRelease(app *model.AppState) http.Handler {
|
||||
func CreateRelease(app *app.AppState) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
session := r.Context().Value("session").(*model.Session)
|
||||
|
||||
|
|
@ -224,7 +224,7 @@ func CreateRelease(app *model.AppState) http.Handler {
|
|||
return
|
||||
}
|
||||
|
||||
app.Log.Info(log.TYPE_MUSIC, "Release \"%s\" created by \"%s\".", release.ID, session.Account.Username)
|
||||
app.Log.Info(model.LOG_MUSIC, "Release \"%s\" created by \"%s\".", release.ID, session.Account.Username)
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
|
|
@ -238,7 +238,7 @@ func CreateRelease(app *model.AppState) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func UpdateRelease(app *model.AppState, release *model.Release) http.Handler {
|
||||
func UpdateRelease(app *app.AppState, release *model.Release) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
session := r.Context().Value("session").(*model.Session)
|
||||
|
||||
|
|
@ -307,11 +307,11 @@ func UpdateRelease(app *model.AppState, release *model.Release) http.Handler {
|
|||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
app.Log.Info(log.TYPE_MUSIC, "Release \"%s\" updated by \"%s\".", release.ID, session.Account.Username)
|
||||
app.Log.Info(model.LOG_MUSIC, "Release \"%s\" updated by \"%s\".", release.ID, session.Account.Username)
|
||||
})
|
||||
}
|
||||
|
||||
func UpdateReleaseTracks(app *model.AppState, release *model.Release) http.Handler {
|
||||
func UpdateReleaseTracks(app *app.AppState, release *model.Release) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
session := r.Context().Value("session").(*model.Session)
|
||||
|
||||
|
|
@ -336,11 +336,11 @@ func UpdateReleaseTracks(app *model.AppState, release *model.Release) http.Handl
|
|||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
app.Log.Info(log.TYPE_MUSIC, "Tracklist for release \"%s\" updated by \"%s\".", release.ID, session.Account.Username)
|
||||
app.Log.Info(model.LOG_MUSIC, "Tracklist for release \"%s\" updated by \"%s\".", release.ID, session.Account.Username)
|
||||
})
|
||||
}
|
||||
|
||||
func UpdateReleaseCredits(app *model.AppState, release *model.Release) http.Handler {
|
||||
func UpdateReleaseCredits(app *app.AppState, release *model.Release) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
session := r.Context().Value("session").(*model.Session)
|
||||
|
||||
|
|
@ -381,11 +381,11 @@ func UpdateReleaseCredits(app *model.AppState, release *model.Release) http.Hand
|
|||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
app.Log.Info(log.TYPE_MUSIC, "Credits for release \"%s\" updated by \"%s\".", release.ID, session.Account.Username)
|
||||
app.Log.Info(model.LOG_MUSIC, "Credits for release \"%s\" updated by \"%s\".", release.ID, session.Account.Username)
|
||||
})
|
||||
}
|
||||
|
||||
func UpdateReleaseLinks(app *model.AppState, release *model.Release) http.Handler {
|
||||
func UpdateReleaseLinks(app *app.AppState, release *model.Release) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
session := r.Context().Value("session").(*model.Session)
|
||||
|
||||
|
|
@ -410,11 +410,11 @@ func UpdateReleaseLinks(app *model.AppState, release *model.Release) http.Handle
|
|||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
app.Log.Info(log.TYPE_MUSIC, "Links for release \"%s\" updated by \"%s\".", release.ID, session.Account.Username)
|
||||
app.Log.Info(model.LOG_MUSIC, "Links for release \"%s\" updated by \"%s\".", release.ID, session.Account.Username)
|
||||
})
|
||||
}
|
||||
|
||||
func DeleteRelease(app *model.AppState, release *model.Release) http.Handler {
|
||||
func DeleteRelease(app *app.AppState, release *model.Release) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
session := r.Context().Value("session").(*model.Session)
|
||||
|
||||
|
|
@ -428,6 +428,6 @@ func DeleteRelease(app *model.AppState, release *model.Release) http.Handler {
|
|||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
app.Log.Info(log.TYPE_MUSIC, "Release \"%s\" deleted by \"%s\".", release.ID, session.Account.Username)
|
||||
app.Log.Info(model.LOG_MUSIC, "Release \"%s\" deleted by \"%s\".", release.ID, session.Account.Username)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
28
api/track.go
28
api/track.go
|
|
@ -1,13 +1,13 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"arimelody-web/controller"
|
||||
"arimelody-web/log"
|
||||
"arimelody-web/model"
|
||||
"arimelody-web/controller"
|
||||
"arimelody-web/model"
|
||||
"arimelody-web/model/app"
|
||||
)
|
||||
|
||||
type (
|
||||
|
|
@ -17,7 +17,7 @@ type (
|
|||
}
|
||||
)
|
||||
|
||||
func ServeAllTracks(app *model.AppState) http.Handler {
|
||||
func ServeAllTracks(app *app.AppState) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
type Track struct {
|
||||
ID string `json:"id"`
|
||||
|
|
@ -50,7 +50,7 @@ func ServeAllTracks(app *model.AppState) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func ServeTrack(app *model.AppState, track *model.Track) http.Handler {
|
||||
func ServeTrack(app *app.AppState, track *model.Track) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
dbReleases, err := controller.GetTrackReleases(app.DB, track.ID, false)
|
||||
if err != nil {
|
||||
|
|
@ -74,7 +74,7 @@ func ServeTrack(app *model.AppState, track *model.Track) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func CreateTrack(app *model.AppState) http.Handler {
|
||||
func CreateTrack(app *app.AppState) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
session := r.Context().Value("session").(*model.Session)
|
||||
|
||||
|
|
@ -97,7 +97,7 @@ func CreateTrack(app *model.AppState) http.Handler {
|
|||
return
|
||||
}
|
||||
|
||||
app.Log.Info(log.TYPE_MUSIC, "Track \"%s\" (%s) created by \"%s\".", track.Title, track.ID, session.Account.Username)
|
||||
app.Log.Info(model.LOG_MUSIC, "Track \"%s\" (%s) created by \"%s\".", track.Title, track.ID, session.Account.Username)
|
||||
|
||||
w.Header().Add("Content-Type", "text/plain")
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
|
|
@ -105,7 +105,7 @@ func CreateTrack(app *model.AppState) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func UpdateTrack(app *model.AppState, track *model.Track) http.Handler {
|
||||
func UpdateTrack(app *app.AppState, track *model.Track) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/" {
|
||||
http.NotFound(w, r)
|
||||
|
|
@ -132,7 +132,7 @@ func UpdateTrack(app *model.AppState, track *model.Track) http.Handler {
|
|||
return
|
||||
}
|
||||
|
||||
app.Log.Info(log.TYPE_MUSIC, "Track \"%s\" (%s) updated by \"%s\".", track.Title, track.ID, session.Account.Username)
|
||||
app.Log.Info(model.LOG_MUSIC, "Track \"%s\" (%s) updated by \"%s\".", track.Title, track.ID, session.Account.Username)
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
encoder := json.NewEncoder(w)
|
||||
|
|
@ -144,7 +144,7 @@ func UpdateTrack(app *model.AppState, track *model.Track) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func DeleteTrack(app *model.AppState, track *model.Track) http.Handler {
|
||||
func DeleteTrack(app *app.AppState, track *model.Track) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/" {
|
||||
http.NotFound(w, r)
|
||||
|
|
@ -160,6 +160,6 @@ func DeleteTrack(app *model.AppState, track *model.Track) http.Handler {
|
|||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
app.Log.Info(log.TYPE_MUSIC, "Track \"%s\" (%s) deleted by \"%s\".", track.Title, track.ID, session.Account.Username)
|
||||
app.Log.Info(model.LOG_MUSIC, "Track \"%s\" (%s) deleted by \"%s\".", track.Title, track.ID, session.Account.Username)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,18 +1,18 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"arimelody-web/log"
|
||||
"arimelody-web/model"
|
||||
"bufio"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"arimelody-web/model"
|
||||
"arimelody-web/model/app"
|
||||
"bufio"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func HandleImageUpload(app *model.AppState, data *string, directory string, filename string) (string, error) {
|
||||
func HandleImageUpload(app *app.AppState, data *string, directory string, filename string) (string, error) {
|
||||
split := strings.Split(*data, ";base64,")
|
||||
header := split[0]
|
||||
imageData, err := base64.StdEncoding.DecodeString(split[1])
|
||||
|
|
@ -50,7 +50,7 @@ func HandleImageUpload(app *model.AppState, data *string, directory string, file
|
|||
return "", nil
|
||||
}
|
||||
|
||||
app.Log.Info(log.TYPE_FILES, "\"%s\" created.", imagePath)
|
||||
app.Log.Info(model.LOG_FILES, "\"%s\" created.", imagePath)
|
||||
|
||||
return filename, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue