that's all the API create routes! + some admin UI
Signed-off-by: ari melody <ari@arimelody.me>
This commit is contained in:
parent
9329aa9f60
commit
494b29def3
11 changed files with 193 additions and 19 deletions
65
api/musiccredit.go
Normal file
65
api/musiccredit.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 CreateMusicCredit() 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\n", data.Release), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var artist = global.GetArtist(data.Artist)
|
||||
if artist == nil {
|
||||
http.Error(w, fmt.Sprintf("Artist %s does not exist\n", 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)
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue