create support for releases, artists, tracks, and credits
Signed-off-by: ari melody <ari@arimelody.me>
This commit is contained in:
parent
442889340c
commit
9329aa9f60
19 changed files with 252 additions and 37 deletions
|
@ -24,6 +24,7 @@ func Handler() http.Handler {
|
|||
return
|
||||
}
|
||||
}))
|
||||
|
||||
mux.Handle("/v1/music/", http.StripPrefix("/v1/music", music.ServeRelease()))
|
||||
mux.Handle("/v1/music", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
|
@ -39,5 +40,8 @@ func Handler() http.Handler {
|
|||
}
|
||||
}))
|
||||
|
||||
mux.Handle("/v1/musiccredit", CreateCredit())
|
||||
mux.Handle("/v1/track", CreateTrack())
|
||||
|
||||
return mux
|
||||
}
|
||||
|
|
|
@ -98,10 +98,20 @@ func CreateArtist() http.Handler {
|
|||
var data model.Artist
|
||||
err := json.NewDecoder(r.Body).Decode(&data)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to create artist: %s\n", err)
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if data.ID == "" {
|
||||
http.Error(w, "Artist ID cannot be blank", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if data.Name == "" {
|
||||
http.Error(w, "Artist name cannot be blank", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if global.GetArtist(data.ID) != nil {
|
||||
http.Error(w, fmt.Sprintf("Artist %s already exists", data.ID), http.StatusBadRequest)
|
||||
return
|
||||
|
@ -114,8 +124,6 @@ func CreateArtist() http.Handler {
|
|||
Avatar: data.Avatar,
|
||||
}
|
||||
|
||||
global.Artists = append(global.Artists, artist)
|
||||
|
||||
err = controller.CreateArtistDB(global.DB, &artist)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to create artist %s: %s\n", artist.ID, err)
|
||||
|
@ -123,6 +131,8 @@ func CreateArtist() http.Handler {
|
|||
return
|
||||
}
|
||||
|
||||
global.Artists = append(global.Artists, &artist)
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
err = json.NewEncoder(w).Encode(artist)
|
||||
|
|
65
api/credit.go
Normal file
65
api/credit.go
Normal file
|
@ -0,0 +1,65 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"arimelody.me/arimelody.me/global"
|
||||
"arimelody.me/arimelody.me/music/model"
|
||||
controller "arimelody.me/arimelody.me/music/controller"
|
||||
)
|
||||
|
||||
func CreateCredit() http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
type creditJSON struct {
|
||||
Release string
|
||||
Artist string
|
||||
Role string
|
||||
Primary bool
|
||||
}
|
||||
|
||||
var data creditJSON
|
||||
err := json.NewDecoder(r.Body).Decode(&data)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var release = global.GetRelease(data.Release)
|
||||
if release == nil {
|
||||
http.Error(w, fmt.Sprintf("Release %s does not exist", data.Release), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var artist = global.GetArtist(data.Artist)
|
||||
if artist == nil {
|
||||
http.Error(w, fmt.Sprintf("Artist %s does not exist", data.Artist), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var credit = model.Credit{
|
||||
Artist: artist,
|
||||
Role: data.Role,
|
||||
Primary: data.Primary,
|
||||
}
|
||||
|
||||
err = controller.CreateCreditDB(global.DB, release.ID, artist.ID, &credit)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to create credit: %s\n", err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
release.Credits = append(release.Credits, &credit)
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
err = json.NewEncoder(w).Encode(credit)
|
||||
})
|
||||
}
|
|
@ -14,7 +14,7 @@ import (
|
|||
|
||||
func ServeCatalog() http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
releases := []model.Release{}
|
||||
releases := []*model.Release{}
|
||||
authorised := admin.GetSession(r) != nil
|
||||
for _, release := range global.Releases {
|
||||
if !release.IsReleased() && !authorised {
|
||||
|
@ -71,13 +71,11 @@ func CreateRelease() http.Handler {
|
|||
Artwork: data.Artwork,
|
||||
Buyname: data.Buyname,
|
||||
Buylink: data.Buylink,
|
||||
Links: []model.Link{},
|
||||
Credits: []model.Credit{},
|
||||
Tracks: []model.Track{},
|
||||
Links: []*model.Link{},
|
||||
Credits: []*model.Credit{},
|
||||
Tracks: []*model.Track{},
|
||||
}
|
||||
|
||||
global.Releases = append([]model.Release{release}, global.Releases...)
|
||||
|
||||
err = controller.CreateReleaseDB(global.DB, &release)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to create release %s: %s\n", release.ID, err)
|
||||
|
@ -85,6 +83,8 @@ func CreateRelease() http.Handler {
|
|||
return
|
||||
}
|
||||
|
||||
global.Releases = append([]*model.Release{&release}, global.Releases...)
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
err = json.NewEncoder(w).Encode(release)
|
46
api/track.go
Normal file
46
api/track.go
Normal file
|
@ -0,0 +1,46 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"arimelody.me/arimelody.me/global"
|
||||
"arimelody.me/arimelody.me/music/model"
|
||||
controller "arimelody.me/arimelody.me/music/controller"
|
||||
)
|
||||
|
||||
func CreateTrack() http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
var track model.Track
|
||||
err := json.NewDecoder(r.Body).Decode(&track)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if track.Title == "" {
|
||||
http.Error(w, "Track title cannot be empty", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
trackID, err := controller.CreateTrackDB(global.DB, &track)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to create credit: %s\n", err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
track.ID = trackID
|
||||
global.Tracks = append(global.Tracks, &track)
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
err = json.NewEncoder(w).Encode(track)
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue