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
|
@ -3,7 +3,7 @@
|
|||
|
||||
body {
|
||||
width: 100%;
|
||||
height: 100vh;
|
||||
height: calc(100vh - 1em);
|
||||
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
|
@ -76,6 +76,31 @@ a:hover {
|
|||
margin: 0;
|
||||
}
|
||||
|
||||
.card-title {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
}
|
||||
|
||||
.create-btn {
|
||||
background: #c4ff6a;
|
||||
padding: .5em .8em;
|
||||
border-radius: .5em;
|
||||
border: 1px solid #84b141;
|
||||
text-decoration: none;
|
||||
}
|
||||
.create-btn:hover {
|
||||
background: #fff;
|
||||
border-color: #d0d0d0;
|
||||
text-decoration: inherit;
|
||||
}
|
||||
.create-btn:active {
|
||||
background: #d0d0d0;
|
||||
border-color: #808080;
|
||||
text-decoration: inherit;
|
||||
}
|
||||
|
||||
.release {
|
||||
margin-bottom: 1em;
|
||||
padding: 1em;
|
||||
|
|
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
|
||||
}
|
||||
|
||||
|
|
|
@ -51,11 +51,11 @@ func UpdateArtistDB(db *sqlx.DB, artist *model.Artist) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func DeleteArtistDB(db *sqlx.DB, artistID string) error {
|
||||
func DeleteArtistDB(db *sqlx.DB, artist *model.Artist) error {
|
||||
_, err := db.Exec(
|
||||
"DELETE FROM artist "+
|
||||
"WHERE id=$1",
|
||||
artistID,
|
||||
artist.ID,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -79,7 +79,7 @@ func UpdateReleaseDB(db *sqlx.DB, release *model.Release) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func DeleteReleaseDB(db *sqlx.DB, release model.Release) error {
|
||||
func DeleteReleaseDB(db *sqlx.DB, release *model.Release) error {
|
||||
_, err := db.Exec(
|
||||
"DELETE FROM musicrelease "+
|
||||
"WHERE id=$1",
|
||||
|
|
|
@ -55,11 +55,11 @@ func UpdateTrackDB(db *sqlx.DB, track *model.Track) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func DeleteTrackDB(db *sqlx.DB, trackID string) error {
|
||||
func DeleteTrackDB(db *sqlx.DB, track *model.Track) error {
|
||||
_, err := db.Exec(
|
||||
"DELETE FROM musictrack "+
|
||||
"WHERE id=$1",
|
||||
trackID,
|
||||
track.ID,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -13,7 +13,10 @@
|
|||
|
||||
<main>
|
||||
|
||||
<div class="card-title">
|
||||
<h1>Releases</h1>
|
||||
<a href="/admin/createrelease" class="create-btn">Create New</a>
|
||||
</div>
|
||||
<div class="card releases">
|
||||
{{range $Release := .Releases}}
|
||||
<div class="release">
|
||||
|
@ -36,7 +39,10 @@
|
|||
{{end}}
|
||||
</div>
|
||||
|
||||
<div class="card-title">
|
||||
<h1>Artists</h1>
|
||||
<a href="/admin/createartist" class="create-btn">Create New</a>
|
||||
</div>
|
||||
<div class="card artists">
|
||||
{{range $Artist := .Artists}}
|
||||
<div class="artist">
|
||||
|
@ -49,7 +55,10 @@
|
|||
{{end}}
|
||||
</div>
|
||||
|
||||
<div class="card-title">
|
||||
<h1>Tracks</h1>
|
||||
<a href="/admin/createtrack" class="create-btn">Create New</a>
|
||||
</div>
|
||||
<div class="card tracks">
|
||||
{{range $Track := .Tracks}}
|
||||
<div class="track">
|
||||
|
|
Loading…
Reference in a new issue