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
20
api/api.go
20
api/api.go
|
@ -25,7 +25,19 @@ func Handler() http.Handler {
|
|||
}
|
||||
}))
|
||||
|
||||
mux.Handle("/v1/music/", http.StripPrefix("/v1/music", music.ServeRelease()))
|
||||
mux.Handle("/v1/music/", http.StripPrefix("/v1/music", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
music.ServeRelease().ServeHTTP(w, r)
|
||||
return
|
||||
case http.MethodDelete:
|
||||
admin.MustAuthorise(DeleteRelease()).ServeHTTP(w, r)
|
||||
return
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
})))
|
||||
mux.Handle("/v1/music", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
|
@ -34,13 +46,17 @@ func Handler() http.Handler {
|
|||
case http.MethodPost:
|
||||
admin.MustAuthorise(CreateRelease()).ServeHTTP(w, r)
|
||||
return
|
||||
case http.MethodDelete:
|
||||
admin.MustAuthorise(DeleteRelease()).ServeHTTP(w, r)
|
||||
return
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
}))
|
||||
|
||||
mux.Handle("/v1/musiccredit", CreateCredit())
|
||||
mux.Handle("/v1/musiccredit", CreateMusicCredit())
|
||||
mux.Handle("/v1/musiclink", CreateMusicLink())
|
||||
mux.Handle("/v1/track", CreateTrack())
|
||||
|
||||
return mux
|
||||
|
|
|
@ -104,16 +104,16 @@ func CreateArtist() http.Handler {
|
|||
}
|
||||
|
||||
if data.ID == "" {
|
||||
http.Error(w, "Artist ID cannot be blank", http.StatusBadRequest)
|
||||
http.Error(w, "Artist ID cannot be blank\n", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if data.Name == "" {
|
||||
http.Error(w, "Artist name cannot be blank", http.StatusBadRequest)
|
||||
http.Error(w, "Artist name cannot be blank\n", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if global.GetArtist(data.ID) != nil {
|
||||
http.Error(w, fmt.Sprintf("Artist %s already exists", data.ID), http.StatusBadRequest)
|
||||
http.Error(w, fmt.Sprintf("Artist %s already exists\n", data.ID), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@ import (
|
|||
controller "arimelody.me/arimelody.me/music/controller"
|
||||
)
|
||||
|
||||
func CreateCredit() http.Handler {
|
||||
func CreateMusicCredit() http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.NotFound(w, r)
|
||||
|
@ -33,13 +33,13 @@ func CreateCredit() http.Handler {
|
|||
|
||||
var release = global.GetRelease(data.Release)
|
||||
if release == nil {
|
||||
http.Error(w, fmt.Sprintf("Release %s does not exist", data.Release), http.StatusBadRequest)
|
||||
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", data.Artist), http.StatusBadRequest)
|
||||
http.Error(w, fmt.Sprintf("Artist %s does not exist\n", data.Artist), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
66
api/musiclink.go
Normal file
66
api/musiclink.go
Normal file
|
@ -0,0 +1,66 @@
|
|||
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 CreateMusicLink() http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
type linkJSON struct {
|
||||
Release string
|
||||
Name string
|
||||
URL string
|
||||
}
|
||||
|
||||
var data linkJSON
|
||||
err := json.NewDecoder(r.Body).Decode(&data)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if data.Release == "" {
|
||||
http.Error(w, "Release cannot be empty\n", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if data.Name == "" {
|
||||
http.Error(w, "Link name cannot be empty\n", 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 link = model.Link{
|
||||
Name: data.Name,
|
||||
URL: data.URL,
|
||||
}
|
||||
|
||||
err = controller.CreateLinkDB(global.DB, release.ID, &link)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to create link: %s\n", err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
release.Links = append(release.Links, &link)
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
err = json.NewEncoder(w).Encode(release.Links)
|
||||
})
|
||||
}
|
|
@ -41,6 +41,7 @@ func CreateRelease() http.Handler {
|
|||
|
||||
type PostReleaseBody struct {
|
||||
ID string `json:"id"`
|
||||
Visible bool `json:"visible"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
ReleaseType model.ReleaseType `json:"type"`
|
||||
|
@ -57,13 +58,27 @@ func CreateRelease() http.Handler {
|
|||
return
|
||||
}
|
||||
|
||||
if data.ID == "" {
|
||||
http.Error(w, "Release ID cannot be empty\n", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if data.Title == "" {
|
||||
http.Error(w, "Release title cannot be empty\n", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if data.ReleaseDate.Unix() == 0 {
|
||||
http.Error(w, "Release date cannot be empty or 0\n", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if global.GetRelease(data.ID) != nil {
|
||||
http.Error(w, fmt.Sprintf("Release %s already exists", data.ID), http.StatusBadRequest)
|
||||
http.Error(w, fmt.Sprintf("Release %s already exists\n", data.ID), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var release = model.Release{
|
||||
ID: data.ID,
|
||||
Visible: data.Visible,
|
||||
Title: data.Title,
|
||||
Description: data.Description,
|
||||
ReleaseType: data.ReleaseType,
|
||||
|
@ -88,5 +103,48 @@ func CreateRelease() http.Handler {
|
|||
w.Header().Add("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
err = json.NewEncoder(w).Encode(release)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func DeleteRelease() http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodDelete {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
if r.URL.Path == "/" {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var releaseID = r.URL.Path[1:]
|
||||
var release = global.GetRelease(releaseID)
|
||||
if release == nil {
|
||||
http.Error(w, fmt.Sprintf("Release %s does not exist\n", releaseID), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err := controller.DeleteReleaseDB(global.DB, release)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
global.Releases = func () []*model.Release {
|
||||
var releases = []*model.Release{}
|
||||
for _, r := range global.Releases {
|
||||
if r.ID == releaseID { continue }
|
||||
releases = append(releases, r)
|
||||
}
|
||||
return releases
|
||||
}()
|
||||
|
||||
w.WriteHeader(200)
|
||||
w.Write([]byte(fmt.Sprintf("Release %s has been deleted\n", release.ID)))
|
||||
})
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ func CreateTrack() http.Handler {
|
|||
}
|
||||
|
||||
if track.Title == "" {
|
||||
http.Error(w, "Track title cannot be empty", http.StatusBadRequest)
|
||||
http.Error(w, "Track title cannot be empty\n", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue