lots of post-DB cleanup

This commit is contained in:
ari melody 2024-09-02 00:15:23 +01:00
parent 965d6f5c3e
commit c9d950d2b2
Signed by: ari
GPG key ID: CF99829C92678188
23 changed files with 412 additions and 550 deletions

View file

@ -1,7 +1,7 @@
{{define "release"}} {{define "release"}}
<div class="release"> <div class="release">
<div class="release-artwork"> <div class="release-artwork">
<img src="{{.Artwork}}" alt="" width="128" loading="lazy"> <img src="{{.GetArtwork}}" alt="" width="128" loading="lazy">
</div> </div>
<div class="release-info"> <div class="release-info">
<h3 class="release-title"> <h3 class="release-title">

View file

@ -57,7 +57,7 @@ func Handler() http.Handler {
} }
releases := []musicModel.FullRelease{} releases := []musicModel.FullRelease{}
for _, release := range dbReleases { for _, release := range dbReleases {
fullRelease, err := musicDB.GetFullRelease(global.DB, release) fullRelease, err := musicDB.GetFullRelease(global.DB, release.ID)
if err != nil { if err != nil {
fmt.Printf("FATAL: Failed to pull full release data for %s: %s\n", release.ID, err) fmt.Printf("FATAL: Failed to pull full release data for %s: %s\n", release.ID, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)

View file

@ -6,22 +6,23 @@ import (
"strings" "strings"
"arimelody.me/arimelody.me/global" "arimelody.me/arimelody.me/global"
db "arimelody.me/arimelody.me/music/controller"
"arimelody.me/arimelody.me/music/model" "arimelody.me/arimelody.me/music/model"
controller "arimelody.me/arimelody.me/music/controller"
) )
func serveRelease() http.Handler { func serveRelease() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
slices := strings.Split(r.URL.Path[1:], "/") slices := strings.Split(r.URL.Path[1:], "/")
releaseID := slices[0] releaseID := slices[0]
release, err := controller.GetRelease(global.DB, releaseID)
release, err := db.GetFullRelease(global.DB, releaseID)
if err != nil { if err != nil {
fmt.Printf("FATAL: Failed to pull release %s: %s\n", releaseID, err) if strings.Contains(err.Error(), "no rows") {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) http.NotFound(w, r)
return return
} }
if release == nil { fmt.Printf("FATAL: Failed to pull full release data for %s: %s\n", release.ID, err)
http.NotFound(w, r) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return return
} }
@ -31,32 +32,25 @@ func serveRelease() http.Handler {
return return
} }
fullRelease, err := controller.GetFullRelease(global.DB, release)
if err != nil {
fmt.Printf("FATAL: Failed to pull full release data for %s: %s\n", release.ID, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
if len(slices) > 1 { if len(slices) > 1 {
switch slices[1] { switch slices[1] {
case "editcredits": case "editcredits":
serveEditCredits(fullRelease).ServeHTTP(w, r) serveEditCredits(release).ServeHTTP(w, r)
return return
case "addcredit": case "addcredit":
serveAddCredit(fullRelease).ServeHTTP(w, r) serveAddCredit(release).ServeHTTP(w, r)
return return
case "newcredit": case "newcredit":
serveNewCredit().ServeHTTP(w, r) serveNewCredit().ServeHTTP(w, r)
return return
case "editlinks": case "editlinks":
serveEditLinks(fullRelease).ServeHTTP(w, r) serveEditLinks(release).ServeHTTP(w, r)
return return
case "edittracks": case "edittracks":
serveEditTracks(fullRelease).ServeHTTP(w, r) serveEditTracks(release).ServeHTTP(w, r)
return return
case "addtrack": case "addtrack":
serveAddTrack(fullRelease).ServeHTTP(w, r) serveAddTrack(release).ServeHTTP(w, r)
return return
case "newtrack": case "newtrack":
serveNewTrack().ServeHTTP(w, r) serveNewTrack().ServeHTTP(w, r)
@ -66,7 +60,7 @@ func serveRelease() http.Handler {
return return
} }
err = pages["release"].Execute(w, fullRelease) err = pages["release"].Execute(w, release)
if err != nil { if err != nil {
fmt.Printf("Error rendering admin release page for %s: %s\n", release.ID, err) fmt.Printf("Error rendering admin release page for %s: %s\n", release.ID, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
@ -87,7 +81,7 @@ func serveEditCredits(release *model.FullRelease) http.Handler {
func serveAddCredit(release *model.FullRelease) http.Handler { func serveAddCredit(release *model.FullRelease) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
artists, err := controller.GetArtistsNotOnRelease(global.DB, release.Release) artists, err := db.GetArtistsNotOnRelease(global.DB, release.Release.ID)
if err != nil { if err != nil {
fmt.Printf("FATAL: Failed to pull artists not on %s: %s\n", release.ID, err) fmt.Printf("FATAL: Failed to pull artists not on %s: %s\n", release.ID, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
@ -114,7 +108,7 @@ func serveAddCredit(release *model.FullRelease) http.Handler {
func serveNewCredit() http.Handler { func serveNewCredit() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
artistID := strings.Split(r.URL.Path, "/")[3] artistID := strings.Split(r.URL.Path, "/")[3]
artist, err := controller.GetArtist(global.DB, artistID) artist, err := db.GetArtist(global.DB, artistID)
if err != nil { if err != nil {
fmt.Printf("FATAL: Failed to pull artists %s: %s\n", artistID, err) fmt.Printf("FATAL: Failed to pull artists %s: %s\n", artistID, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
@ -158,7 +152,7 @@ func serveEditTracks(release *model.FullRelease) http.Handler {
func serveAddTrack(release *model.FullRelease) http.Handler { func serveAddTrack(release *model.FullRelease) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
tracks, err := controller.GetTracksNotOnRelease(global.DB, release.Release) tracks, err := db.GetTracksNotOnRelease(global.DB, release.Release.ID)
if err != nil { if err != nil {
fmt.Printf("FATAL: Failed to pull tracks not on %s: %s\n", release.ID, err) fmt.Printf("FATAL: Failed to pull tracks not on %s: %s\n", release.ID, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
@ -186,7 +180,7 @@ func serveAddTrack(release *model.FullRelease) http.Handler {
func serveNewTrack() http.Handler { func serveNewTrack() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
trackID := strings.Split(r.URL.Path, "/")[3] trackID := strings.Split(r.URL.Path, "/")[3]
track, err := controller.GetTrack(global.DB, trackID) track, err := db.GetTrack(global.DB, trackID)
if err != nil { if err != nil {
fmt.Printf("Error rendering new track component for %s: %s\n", trackID, err) fmt.Printf("Error rendering new track component for %s: %s\n", trackID, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)

View file

@ -18,8 +18,8 @@ input[type="text"] {
.release-artwork { .release-artwork {
width: 200px; width: 200px;
text-align: center;
} }
.release-artwork img { .release-artwork img {
width: 100%; width: 100%;
aspect-ratio: 1; aspect-ratio: 1;
@ -28,6 +28,9 @@ input[type="text"] {
outline: 1px solid #808080; outline: 1px solid #808080;
cursor: pointer; cursor: pointer;
} }
.release-artwork #remove-artwork {
padding: .3em .4em;
}
.release-info { .release-info {
width: 0; width: 0;
@ -342,7 +345,7 @@ dialog div.dialog-actions {
background-color: #8cff83 background-color: #8cff83
} }
.card.links a.button[data-name="applemusic"] { .card.links a.button[data-name="apple music"] {
background-color: #8cd9ff background-color: #8cd9ff
} }

View file

@ -3,45 +3,36 @@ import Stateful from "/script/silver.min.js"
const releaseID = document.getElementById("release").dataset.id; const releaseID = document.getElementById("release").dataset.id;
const titleInput = document.getElementById("title"); const titleInput = document.getElementById("title");
const artworkImg = document.getElementById("artwork"); const artworkImg = document.getElementById("artwork");
const removeArtworkBtn = document.getElementById("remove-artwork");
const artworkInput = document.getElementById("artwork-file"); const artworkInput = document.getElementById("artwork-file");
const typeInput = document.getElementById("type"); const typeInput = document.getElementById("type");
const descInput = document.getElementById("description"); const descInput = document.getElementById("description");
const dateInput = document.getElementById("release-date"); const dateInput = document.getElementById("release-date");
const buynameInput = document.getElementById("buyname"); const buynameInput = document.getElementById("buyname");
const buylinkInput = document.getElementById("buylink"); const buylinkInput = document.getElementById("buylink");
const copyrightInput = document.getElementById("copyright");
const copyrightURLInput = document.getElementById("copyright-url");
const visInput = document.getElementById("visibility"); const visInput = document.getElementById("visibility");
const saveBtn = document.getElementById("save"); const saveBtn = document.getElementById("save");
const deleteBtn = document.getElementById("delete"); const deleteBtn = document.getElementById("delete");
var artworkData = artworkImg.attributes.src.value; var artworkData = artworkImg.attributes.src.value;
var edited = new Stateful(false);
var releaseData = updateData(undefined);
function updateData(old) { saveBtn.addEventListener("click", () => {
var releaseData = { fetch("/api/v1/music/" + releaseID, {
method: "PUT",
body: JSON.stringify({
visible: visInput.value === "true", visible: visInput.value === "true",
title: titleInput.value, title: titleInput.value,
description: descInput.value, description: descInput.value,
type: typeInput.value, type: typeInput.value,
releaseDate: dateInput.value, releaseDate: dateInput.value + ":00Z",
artwork: artworkData, artwork: artworkData,
buyname: buynameInput.value, buyname: buynameInput.value,
buylink: buylinkInput.value, buylink: buylinkInput.value,
}; copyright: copyrightInput.value,
copyrightURL: copyrightURLInput.value,
if (releaseData && releaseData != old) { }),
edited.set(true);
}
return releaseData;
}
function saveRelease() {
console.table(releaseData);
fetch("/api/v1/music/" + releaseID, {
method: "PUT",
body: JSON.stringify(releaseData),
headers: { "Content-Type": "application/json" } headers: { "Content-Type": "application/json" }
}).then(res => { }).then(res => {
if (!res.ok) { if (!res.ok) {
@ -54,9 +45,13 @@ function saveRelease() {
location = location; location = location;
}); });
} });
function deleteRelease() { deleteBtn.addEventListener("click", () => {
if (releaseID != prompt(
"You are about to permanently delete " + releaseID + ". " +
"This action is irreversible. " +
"Please enter \"" + releaseID + "\" to continue.")) return;
fetch("/api/v1/music/" + releaseID, { fetch("/api/v1/music/" + releaseID, {
method: "DELETE", method: "DELETE",
}).then(res => { }).then(res => {
@ -70,15 +65,17 @@ function deleteRelease() {
location = "/admin"; location = "/admin";
}); });
}
edited.onUpdate(edited => {
saveBtn.disabled = !edited;
})
titleInput.addEventListener("change", () => {
releaseData = updateData(releaseData);
}); });
[titleInput, typeInput, descInput, dateInput, buynameInput, buylinkInput, copyrightInput, copyrightURLInput, visInput].forEach(input => {
input.addEventListener("change", () => {
saveBtn.disabled = false;
});
input.addEventListener("keypress", () => {
saveBtn.disabled = false;
});
});
artworkImg.addEventListener("click", () => { artworkImg.addEventListener("click", () => {
artworkInput.addEventListener("change", () => { artworkInput.addEventListener("change", () => {
if (artworkInput.files.length > 0) { if (artworkInput.files.length > 0) {
@ -87,41 +84,17 @@ artworkImg.addEventListener("click", () => {
const data = e.target.result; const data = e.target.result;
artworkImg.src = data; artworkImg.src = data;
artworkData = data; artworkData = data;
releaseData = updateData(releaseData); saveBtn.disabled = false;
}; };
reader.readAsDataURL(artworkInput.files[0]); reader.readAsDataURL(artworkInput.files[0]);
} }
}); });
artworkInput.click(); artworkInput.click();
}); });
typeInput.addEventListener("change", () => {
releaseData = updateData(releaseData);
});
descInput.addEventListener("change", () => {
releaseData = updateData(releaseData);
});
dateInput.addEventListener("change", () => {
releaseData = updateData(releaseData);
});
buynameInput.addEventListener("change", () => {
releaseData = updateData(releaseData);
});
buylinkInput.addEventListener("change", () => {
releaseData = updateData(releaseData);
});
visInput.addEventListener("change", () => {
releaseData = updateData(releaseData);
});
saveBtn.addEventListener("click", () => {
if (!edited.get()) return;
saveRelease();
});
deleteBtn.addEventListener("click", () => { removeArtworkBtn.addEventListener("click", () => {
if (releaseID != prompt( artworkImg.src = "/img/default-cover-art.png"
"You are about to permanently delete " + releaseID + ". " + artworkData = "";
"This action is irreversible. " + saveBtn.disabled = false;
"Please enter \"" + releaseID + "\" to continue.")) return;
deleteRelease();
}); });

View file

@ -25,7 +25,7 @@ func serveTrack() http.Handler {
return return
} }
dbReleases, err := music.GetTrackReleases(global.DB, track) dbReleases, err := music.GetTrackReleases(global.DB, track.ID)
if err != nil { if err != nil {
fmt.Printf("Error rendering admin track page for %s: %s\n", id, err) fmt.Printf("Error rendering admin track page for %s: %s\n", id, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
@ -33,7 +33,7 @@ func serveTrack() http.Handler {
} }
releases := []model.FullRelease{} releases := []model.FullRelease{}
for _, release := range dbReleases { for _, release := range dbReleases {
fullRelease, err := music.GetFullRelease(global.DB, release) fullRelease, err := music.GetFullRelease(global.DB, release.ID)
if err != nil { if err != nil {
fmt.Printf("FATAL: Failed to pull full release data for %s: %s\n", release.ID, err) fmt.Printf("FATAL: Failed to pull full release data for %s: %s\n", release.ID, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)

View file

@ -12,10 +12,11 @@
<div class="release-artwork"> <div class="release-artwork">
<img src="{{.Artwork}}" alt="" width="256" loading="lazy" id="artwork"> <img src="{{.Artwork}}" alt="" width="256" loading="lazy" id="artwork">
<input type="file" id="artwork-file" name="Artwork" accept=".png,.jpg,.jpeg" hidden> <input type="file" id="artwork-file" name="Artwork" accept=".png,.jpg,.jpeg" hidden>
<button id="remove-artwork">Remove</button>
</div> </div>
<div class="release-info"> <div class="release-info">
<h1 class="release-title"> <h1 class="release-title">
<input type="text" id="title" name="Title" value="{{.Title}}"> <input type="text" id="title" name="Title" value="{{.Title}}" autocomplete="true">
</h1> </h1>
<table> <table>
<tr> <tr>
@ -53,19 +54,31 @@
<tr> <tr>
<td>Release Date</td> <td>Release Date</td>
<td> <td>
<input type="datetime-local" name="Release Date" id="release-date" value="{{.TextReleaseDate}}"> <input type="datetime-local" name="release-date" id="release-date" value="{{.TextReleaseDate}}">
</td> </td>
</tr> </tr>
<tr> <tr>
<td>Buy Name</td> <td>Buy Name</td>
<td> <td>
<input type="text" name="Buy Name" id="buyname" value="{{.Buyname}}"> <input type="text" name="buyname" id="buyname" value="{{.Buyname}}" autocomplete="true">
</td> </td>
</tr> </tr>
<tr> <tr>
<td>Buy Link</td> <td>Buy Link</td>
<td> <td>
<input type="text" name="Buy Link" id="buylink" value="{{.Buylink}}"> <input type="text" name="buylink" id="buylink" value="{{.Buylink}}" autocomplete="true">
</td>
</tr>
<tr>
<td>Copyright</td>
<td>
<input type="text" name="copyright" id="copyright" value="{{.Copyright}}" autocomplete="true">
</td>
</tr>
<tr>
<td>Copyright URL</td>
<td>
<input type="text" name="copyright-url" id="copyright-url" value="{{.CopyrightURL}}" autocomplete="true">
</td> </td>
</tr> </tr>
<tr> <tr>

View file

@ -21,10 +21,14 @@ func Handler() http.Handler {
var artist model.Artist var artist model.Artist
err := global.DB.Get(&artist, "SELECT * FROM artist WHERE id=$1", artistID) err := global.DB.Get(&artist, "SELECT * FROM artist WHERE id=$1", artistID)
if err != nil { if err != nil {
fmt.Printf("FATAL: Error while retrieving artist %s: %s\n", artistID, err) if strings.Contains(err.Error(), "no rows") {
http.NotFound(w, r) http.NotFound(w, r)
return return
} }
fmt.Printf("FATAL: Error while retrieving artist %s: %s\n", artistID, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
switch r.Method { switch r.Method {
case http.MethodGet: case http.MethodGet:
@ -60,10 +64,14 @@ func Handler() http.Handler {
var release model.Release var release model.Release
err := global.DB.Get(&release, "SELECT * FROM musicrelease WHERE id=$1", releaseID) err := global.DB.Get(&release, "SELECT * FROM musicrelease WHERE id=$1", releaseID)
if err != nil { if err != nil {
fmt.Printf("FATAL: Error while retrieving release %s: %s\n", releaseID, err) if strings.Contains(err.Error(), "no rows") {
http.NotFound(w, r) http.NotFound(w, r)
return return
} }
fmt.Printf("FATAL: Error while retrieving release %s: %s\n", releaseID, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
switch r.Method { switch r.Method {
case http.MethodGet: case http.MethodGet:
@ -99,10 +107,14 @@ func Handler() http.Handler {
var track model.Track var track model.Track
err := global.DB.Get(&track, "SELECT * FROM musictrack WHERE id=$1", trackID) err := global.DB.Get(&track, "SELECT * FROM musictrack WHERE id=$1", trackID)
if err != nil { if err != nil {
fmt.Printf("FATAL: Error while retrieving track %s: %s\n", trackID, err) if strings.Contains(err.Error(), "no rows") {
http.NotFound(w, r) http.NotFound(w, r)
return return
} }
fmt.Printf("FATAL: Error while retrieving track %s: %s\n", trackID, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
switch r.Method { switch r.Method {
case http.MethodGet: case http.MethodGet:

View file

@ -8,6 +8,7 @@ import (
"arimelody.me/arimelody.me/global" "arimelody.me/arimelody.me/global"
"arimelody.me/arimelody.me/music/model" "arimelody.me/arimelody.me/music/model"
db "arimelody.me/arimelody.me/music/controller"
) )
type artistJSON struct { type artistJSON struct {
@ -20,7 +21,7 @@ type artistJSON struct {
func ServeAllArtists() http.Handler { func ServeAllArtists() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var artists = []*model.Artist{} var artists = []*model.Artist{}
err := global.DB.Select(&artists, "SELECT * FROM artist") artists, err := db.GetAllArtists(global.DB)
if err != nil { if err != nil {
fmt.Printf("FATAL: Failed to serve all artists: %s\n", err) fmt.Printf("FATAL: Failed to serve all artists: %s\n", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
@ -39,7 +40,6 @@ func ServeArtist(artist model.Artist) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
type ( type (
creditJSON struct { creditJSON struct {
Release string `json:"release"`
Role string `json:"role"` Role string `json:"role"`
Primary bool `json:"primary"` Primary bool `json:"primary"`
} }
@ -49,14 +49,22 @@ func ServeArtist(artist model.Artist) http.Handler {
} }
) )
var credits = map[string]creditJSON{} var dbCredits []*model.Credit
err := global.DB.Select(&credits, "SELECT release,role,is_primary FROM musiccredit WHERE id=$1", artist.ID) dbCredits, err := db.GetArtistCredits(global.DB, artist.ID)
if err != nil { if err != nil {
fmt.Printf("FATAL: Failed to retrieve artist credits for %s: %s\n", artist.ID, err) fmt.Printf("FATAL: Failed to retrieve artist credits for %s: %s\n", artist.ID, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return return
} }
var credits = map[string]creditJSON{}
for _, credit := range dbCredits {
credits[credit.Release.ID] = creditJSON{
Role: credit.Role,
Primary: credit.Primary,
}
}
w.Header().Add("Content-Type", "application/json") w.Header().Add("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(artistJSON{ err = json.NewEncoder(w).Encode(artistJSON{
Artist: artist, Artist: artist,

View file

@ -12,31 +12,12 @@ import (
"arimelody.me/arimelody.me/admin" "arimelody.me/arimelody.me/admin"
"arimelody.me/arimelody.me/global" "arimelody.me/arimelody.me/global"
music "arimelody.me/arimelody.me/music/controller"
"arimelody.me/arimelody.me/music/model" "arimelody.me/arimelody.me/music/model"
) )
type releaseBodyJSON struct {
ID string `json:"id"`
Visible *bool `json:"visible"`
Title *string `json:"title"`
Description *string `json:"description"`
ReleaseType *model.ReleaseType `json:"type"`
ReleaseDate *string `json:"releaseDate"`
Artwork *string `json:"artwork"`
Buyname *string `json:"buyname"`
Buylink *string `json:"buylink"`
}
func ServeCatalog() http.Handler { func ServeCatalog() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
type catalogItem struct {
ID string `json:"id"`
Title string `json:"title"`
ReleaseType model.ReleaseType `json:"type"`
ReleaseDate time.Time `json:"releaseDate"`
Artwork string `json:"artwork"`
Buylink string `json:"buylink"`
}
releases := []*model.Release{} releases := []*model.Release{}
err := global.DB.Select(&releases, "SELECT * FROM musicrelease ORDER BY release_date DESC") err := global.DB.Select(&releases, "SELECT * FROM musicrelease ORDER BY release_date DESC")
@ -45,13 +26,13 @@ func ServeCatalog() http.Handler {
return return
} }
catalog := []catalogItem{} catalog := []model.ReleaseShorthand{}
authorised := admin.GetSession(r) != nil authorised := admin.GetSession(r) != nil
for _, release := range releases { for _, release := range releases {
if !release.Visible && !authorised { if !release.Visible && !authorised {
continue continue
} }
catalog = append(catalog, catalogItem{ catalog = append(catalog, model.ReleaseShorthand{
ID: release.ID, ID: release.ID,
Title: release.Title, Title: release.Title,
ReleaseType: release.ReleaseType, ReleaseType: release.ReleaseType,
@ -77,77 +58,31 @@ func CreateRelease() http.Handler {
return return
} }
var data releaseBodyJSON var release model.Release
err := json.NewDecoder(r.Body).Decode(&data) err := json.NewDecoder(r.Body).Decode(&release)
if err != nil { if err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return return
} }
if data.ID == "" { if release.ID == "" {
http.Error(w, "Release ID cannot be empty\n", http.StatusBadRequest) http.Error(w, "Release ID cannot be empty\n", http.StatusBadRequest)
return return
} }
title := data.ID if release.Title == "" { release.Title = release.ID }
if data.Title != nil && *data.Title != "" { if release.ReleaseType == "" { release.ReleaseType = model.Single }
title = *data.Title
if release.ReleaseDate != time.Unix(0, 0) {
release.ReleaseDate = time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 0, 0, 0, 0, time.UTC)
} }
description := "" if release.Artwork == "" { release.Artwork = "/img/default-cover-art.png" }
if data.Description != nil && *data.Description != "" { description = *data.Description }
releaseType := model.Single err = music.CreateRelease(global.DB, &release)
if data.ReleaseType != nil && *data.ReleaseType != "" { releaseType = *data.ReleaseType }
releaseDate := time.Time{}
if data.ReleaseDate != nil && *data.ReleaseDate != "" {
releaseDate, err = time.Parse("2006-01-02T15:04", *data.ReleaseDate)
if err != nil {
http.Error(w, "Invalid release date", http.StatusBadRequest)
return
}
} else {
releaseDate = time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 0, 0, 0, 0, time.UTC)
}
artwork := "/img/default-cover-art.png"
if data.Artwork != nil && *data.Artwork != "" { artwork = *data.Artwork }
buyname := ""
if data.Buyname != nil && *data.Buyname != "" { buyname = *data.Buyname }
buylink := ""
if data.Buylink != nil && *data.Buylink != "" { buylink = *data.Buylink }
var release = model.Release{
ID: data.ID,
Visible: false,
Title: title,
Description: description,
ReleaseType: releaseType,
ReleaseDate: releaseDate,
Artwork: artwork,
Buyname: buyname,
Buylink: buylink,
}
_, err = global.DB.Exec(
"INSERT INTO musicrelease "+
"(id, visible, title, description, type, release_date, artwork, buyname, buylink) "+
"VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)",
release.ID,
release.Visible,
release.Title,
release.Description,
release.ReleaseType,
release.ReleaseDate.Format("2006-01-02 15:04:05"),
release.Artwork,
release.Buyname,
release.Buylink)
if err != nil { if err != nil {
if strings.Contains(err.Error(), "duplicate key") { if strings.Contains(err.Error(), "duplicate key") {
http.Error(w, fmt.Sprintf("Release %s already exists\n", data.ID), http.StatusBadRequest) http.Error(w, fmt.Sprintf("Release %s already exists\n", release.ID), http.StatusBadRequest)
return return
} }
fmt.Printf("Failed to create release %s: %s\n", release.ID, err) fmt.Printf("Failed to create release %s: %s\n", release.ID, err)
@ -173,14 +108,6 @@ func UpdateRelease(release model.Release) http.Handler {
} }
segments := strings.Split(r.URL.Path[1:], "/") segments := strings.Split(r.URL.Path[1:], "/")
var releaseID = segments[0]
var exists int
err := global.DB.Get(&exists, "SELECT count(*) FROM musicrelease WHERE id=$1", releaseID)
if err != nil {
fmt.Printf("Failed to update release: %s\n", err)
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
if len(segments) == 2 { if len(segments) == 2 {
switch segments[1] { switch segments[1] {
@ -199,30 +126,19 @@ func UpdateRelease(release model.Release) http.Handler {
return return
} }
var data releaseBodyJSON err := json.NewDecoder(r.Body).Decode(&release)
err = json.NewDecoder(r.Body).Decode(&data)
if err != nil { if err != nil {
fmt.Printf("WARN: Failed to update release %s: %s\n", release.ID, err)
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return return
} }
if data.ID != "" { release.ID = data.ID } if release.Artwork == "" {
if data.Visible != nil { release.Visible = *data.Visible } release.Artwork = "/img/default-cover-art.png"
if data.Title != nil { release.Title = *data.Title } } else {
if data.Description != nil { release.Description = *data.Description } if strings.Contains(release.Artwork, ";base64,") {
if data.ReleaseType != nil { release.ReleaseType = *data.ReleaseType }
if data.ReleaseDate != nil {
newDate, err := time.Parse("2006-01-02T15:04", *data.ReleaseDate)
if err != nil {
http.Error(w, "Invalid release date", http.StatusBadRequest)
return
}
release.ReleaseDate = newDate
}
if data.Artwork != nil {
if strings.Contains(*data.Artwork, ";base64,") {
var artworkDirectory = filepath.Join("uploads", "musicart") var artworkDirectory = filepath.Join("uploads", "musicart")
filename, err := HandleImageUpload(data.Artwork, artworkDirectory, data.ID) filename, err := HandleImageUpload(&release.Artwork, artworkDirectory, release.ID)
// clean up files with this ID and different extensions // clean up files with this ID and different extensions
err = filepath.Walk(artworkDirectory, func(path string, info fs.FileInfo, err error) error { err = filepath.Walk(artworkDirectory, func(path string, info fs.FileInfo, err error) error {
@ -238,27 +154,10 @@ func UpdateRelease(release model.Release) http.Handler {
} }
release.Artwork = fmt.Sprintf("/uploads/musicart/%s", filename) release.Artwork = fmt.Sprintf("/uploads/musicart/%s", filename)
} else {
release.Artwork = *data.Artwork
} }
} }
if data.Buyname != nil { release.Buyname = *data.Buyname } err = music.UpdateRelease(global.DB, &release)
if data.Buylink != nil { release.Buylink = *data.Buylink }
_, err = global.DB.Exec(
"UPDATE musicrelease SET "+
"visible=$2, title=$3, description=$4, type=$5, release_date=$6, artwork=$7, buyname=$8, buylink=$9 "+
"WHERE id=$1",
release.ID,
release.Visible,
release.Title,
release.Description,
release.ReleaseType,
release.ReleaseDate.Format("2006-01-02 15:04:05"),
release.Artwork,
release.Buyname,
release.Buylink)
if err != nil { if err != nil {
fmt.Printf("FATAL: Failed to update release %s: %s\n", release.ID, err) fmt.Printf("FATAL: Failed to update release %s: %s\n", release.ID, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
@ -275,18 +174,7 @@ func UpdateReleaseTracks(release model.Release) http.Handler {
return return
} }
tx := global.DB.MustBegin() err = music.UpdateReleaseTracks(global.DB, &release, trackIDs)
tx.MustExec("DELETE FROM musicreleasetrack WHERE release=$1", release.ID)
for i, trackID := range trackIDs {
tx.MustExec(
"INSERT INTO musicreleasetrack "+
"(release, track, number) "+
"VALUES ($1, $2, $3)",
release.ID,
trackID,
i)
}
err = tx.Commit()
if err != nil { if err != nil {
fmt.Printf("Failed to update tracks for %s: %s\n", release.ID, err) fmt.Printf("Failed to update tracks for %s: %s\n", release.ID, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
@ -308,45 +196,23 @@ func UpdateReleaseCredits(release model.Release) http.Handler {
return return
} }
// clear duplicates var credits []model.Credit
type Credit struct {
Role string
Primary bool
}
var credits = map[string]Credit{}
for _, credit := range data { for _, credit := range data {
credits[credit.Artist] = Credit{ credits = append(credits, model.Credit{
Artist: model.Artist{
ID: credit.Artist,
},
Role: credit.Role, Role: credit.Role,
Primary: credit.Primary, Primary: credit.Primary,
} })
} }
tx := global.DB.MustBegin() err = music.UpdateReleaseCredits(global.DB, &release, credits)
tx.MustExec("DELETE FROM musiccredit WHERE release=$1", release.ID)
for artistID := range credits {
if credits[artistID].Role == "" {
http.Error(w, fmt.Sprintf("Artist role cannot be blank (%s)", artistID), http.StatusBadRequest)
return
}
var exists int
_ = global.DB.Get(&exists, "SELECT count(*) FROM artist WHERE id=$1", artistID)
if exists == 0 {
http.Error(w, fmt.Sprintf("Artist %s does not exist\n", artistID), http.StatusBadRequest)
return
}
tx.MustExec(
"INSERT INTO musiccredit "+
"(release, artist, role, is_primary) "+
"VALUES ($1, $2, $3, $4)",
release.ID,
artistID,
credits[artistID].Role,
credits[artistID].Primary)
}
err = tx.Commit()
if err != nil { if err != nil {
if strings.Contains(err.Error(), "duplicate key") {
http.Error(w, "Artists may only be credited once\n", http.StatusBadRequest)
return
}
fmt.Printf("Failed to update links for %s: %s\n", release.ID, err) fmt.Printf("Failed to update links for %s: %s\n", release.ID, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
} }

View file

@ -6,23 +6,39 @@ import (
"net/http" "net/http"
"arimelody.me/arimelody.me/global" "arimelody.me/arimelody.me/global"
music "arimelody.me/arimelody.me/music/controller"
"arimelody.me/arimelody.me/music/model" "arimelody.me/arimelody.me/music/model"
) )
type (
Track struct {
model.Track
Releases []model.ReleaseShorthand
}
)
func ServeAllTracks() http.Handler { func ServeAllTracks() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
type track struct { type Track struct {
ID string `json:"id"` ID string `json:"id"`
Title string `json:"title"` Title string `json:"title"`
} }
var tracks = []track{} var tracks = []Track{}
err := global.DB.Select(&tracks, "SELECT id, title FROM musictrack") var dbTracks = []*model.Track{}
dbTracks, err := music.GetAllTracks(global.DB)
if err != nil { if err != nil {
fmt.Printf("FATAL: Failed to pull tracks from DB: %s\n", err) fmt.Printf("FATAL: Failed to pull tracks from DB: %s\n", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
} }
for _, track := range dbTracks {
tracks = append(tracks, Track{
ID: track.ID,
Title: track.Title,
})
}
w.Header().Add("Content-Type", "application/json") w.Header().Add("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(tracks) err = json.NewEncoder(w).Encode(tracks)
if err != nil { if err != nil {
@ -34,40 +50,16 @@ func ServeAllTracks() http.Handler {
func ServeTrack(track model.Track) http.Handler { func ServeTrack(track model.Track) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" { releases, err := music.GetTrackReleases(global.DB, track.ID)
ServeAllTracks().ServeHTTP(w, r)
return
}
var trackID = r.URL.Path[1:]
var track = model.Track{}
err := global.DB.Get(&track, "SELECT * from musictrack WHERE id=$1", trackID)
if err != nil { if err != nil {
http.NotFound(w, r) fmt.Printf("FATAL: Failed to pull track releases for %s from DB: %s\n", track.ID, err)
return
}
var releases = []*model.Release{}
err = global.DB.Select(&releases,
"SELECT * FROM musicrelease JOIN musicreleasetrack AS mrt "+
"WHERE mrt.track=$1 "+
"ORDER BY release_date",
track.ID,
)
if err != nil {
fmt.Printf("FATAL: Failed to pull track releases for %s from DB: %s\n", trackID, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
} }
type response struct {
model.Track
Releases []*model.Release
}
w.Header().Add("Content-Type", "application/json") w.Header().Add("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(response{ track, releases }) err = json.NewEncoder(w).Encode(Track{ track, releases })
if err != nil { if err != nil {
fmt.Printf("FATAL: Failed to serve track %s: %s\n", trackID, err) fmt.Printf("FATAL: Failed to serve track %s: %s\n", track.ID, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
} }
}) })
@ -92,15 +84,7 @@ func CreateTrack() http.Handler {
return return
} }
var trackID string id, err := music.CreateTrack(global.DB, &track)
err = global.DB.Get(&trackID,
"INSERT INTO musictrack (title, description, lyrics, preview_url) "+
"VALUES ($1, $2, $3, $4) "+
"RETURNING id",
track.Title,
track.Description,
track.Lyrics,
track.PreviewURL)
if err != nil { if err != nil {
fmt.Printf("FATAL: Failed to create track: %s\n", err) fmt.Printf("FATAL: Failed to create track: %s\n", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
@ -109,7 +93,7 @@ func CreateTrack() http.Handler {
w.Header().Add("Content-Type", "text/plain") w.Header().Add("Content-Type", "text/plain")
w.WriteHeader(http.StatusCreated) w.WriteHeader(http.StatusCreated)
w.Write([]byte(trackID)) w.Write([]byte(id))
}) })
} }
@ -120,35 +104,18 @@ func UpdateTrack(track model.Track) http.Handler {
return return
} }
var update model.Track err := json.NewDecoder(r.Body).Decode(&track)
err := json.NewDecoder(r.Body).Decode(&update)
if err != nil { if err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return return
} }
if update.Title == "" { if track.Title == "" {
http.Error(w, "Track title cannot be empty\n", http.StatusBadRequest) http.Error(w, "Track title cannot be empty\n", http.StatusBadRequest)
return return
} }
var trackID = r.URL.Path[1:] err = music.UpdateTrack(global.DB, &track)
var track = model.Track{}
err = global.DB.Get(&track, "SELECT * from musictrack WHERE id=$1", trackID)
if err != nil {
http.NotFound(w, r)
return
}
_, err = global.DB.Exec(
"UPDATE musictrack "+
"SET title=$2, description=$3, lyrics=$4, preview_url=$5 "+
"WHERE id=$1",
track.ID,
track.Title,
track.Description,
track.Lyrics,
track.PreviewURL)
if err != nil { if err != nil {
fmt.Printf("Failed to update track %s: %s\n", track.ID, err) fmt.Printf("Failed to update track %s: %s\n", track.ID, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
@ -171,10 +138,7 @@ func DeleteTrack(track model.Track) http.Handler {
} }
var trackID = r.URL.Path[1:] var trackID = r.URL.Path[1:]
_, err := global.DB.Exec( err := music.DeleteTrack(global.DB, trackID)
"DELETE FROM musictrack "+
"WHERE id=$1",
trackID)
if err != nil { if err != nil {
fmt.Printf("Failed to delete track %s: %s\n", trackID, err) fmt.Printf("Failed to delete track %s: %s\n", trackID, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)

View file

@ -29,14 +29,14 @@ func GetAllArtists(db *sqlx.DB) ([]*model.Artist, error) {
return artists, nil return artists, nil
} }
func GetArtistsNotOnRelease(db *sqlx.DB, release *model.Release) ([]*model.Artist, error) { func GetArtistsNotOnRelease(db *sqlx.DB, releaseID string) ([]*model.Artist, error) {
var artists = []*model.Artist{} var artists = []*model.Artist{}
err := db.Select(&artists, err := db.Select(&artists,
"SELECT * FROM artist "+ "SELECT * FROM artist "+
"WHERE id NOT IN "+ "WHERE id NOT IN "+
"(SELECT artist FROM musiccredit WHERE release=$1)", "(SELECT artist FROM musiccredit WHERE release=$1)",
release.ID) releaseID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -44,6 +44,33 @@ func GetArtistsNotOnRelease(db *sqlx.DB, release *model.Release) ([]*model.Artis
return artists, nil return artists, nil
} }
func GetArtistCredits(db *sqlx.DB, artistID string) ([]*model.Credit, error) {
type DBCredit struct {
Release string
Artist string
Role string
Primary bool `db:"is_primary"`
}
var dbCredits []DBCredit
err := db.Select(&dbCredits, "SELECT * FROM musiccredit WHERE artist=$1", artistID)
if err != nil {
return nil, err
}
var credits []*model.Credit
for _, credit := range dbCredits {
credits = append(credits, &model.Credit{
Release: model.Release{ ID: credit.Release },
Artist: model.Artist{ ID: credit.Artist },
Role: credit.Role,
Primary: credit.Primary,
})
}
return credits, nil
}
func CreateArtist(db *sqlx.DB, artist *model.Artist) error { func CreateArtist(db *sqlx.DB, artist *model.Artist) error {
_, err := db.Exec( _, err := db.Exec(
"INSERT INTO artist (id, name, website, avatar) "+ "INSERT INTO artist (id, name, website, avatar) "+

View file

@ -1,71 +0,0 @@
package music
import (
"arimelody.me/arimelody.me/music/model"
"github.com/jmoiron/sqlx"
)
// DATABASE
func GetReleaseCredits(db *sqlx.DB, release *model.Release) ([]model.Credit, error) {
var credits = []model.Credit{}
err := db.Select(&credits,
"SELECT artist.*,role,is_primary FROM musiccredit "+
"JOIN artist ON artist=id "+
"WHERE release=$1",
release.ID,
)
if err != nil {
return nil, err
}
return credits, nil
}
func CreateCredit(db *sqlx.DB, releaseID string, artistID string, credit *model.Credit) (error) {
_, err := db.Exec(
"INSERT INTO musiccredit (release, artist, role, is_primary) "+
"VALUES ($1, $2, $3, $4)",
releaseID,
artistID,
credit.Role,
credit.Primary,
)
if err != nil {
return err
}
return nil
}
func UpdateCredit(db *sqlx.DB, releaseID string, artistID string, credit *model.Credit) (error) {
_, err := db.Exec(
"UPDATE musiccredit SET "+
"role=$3, is_primary=$4 "+
"WHERE release=$1, artist=$2",
releaseID,
artistID,
credit.Role,
credit.Primary,
)
if err != nil {
return err
}
return nil
}
func DeleteCredit(db *sqlx.DB, releaseID string, artistID string) (error) {
_, err := db.Exec(
"DELETE FROM musiccredit "+
"WHERE release=$1, artist=$2",
releaseID,
artistID,
)
if err != nil {
return err
}
return nil
}

View file

@ -1,64 +0,0 @@
package music
import (
"arimelody.me/arimelody.me/music/model"
"github.com/jmoiron/sqlx"
)
// DATABASE
func GetReleaseLinks(db *sqlx.DB, release *model.Release) ([]model.Link, error) {
var links = []model.Link{}
err := db.Select(&links, "SELECT name,url FROM musiclink WHERE release=$1", release.ID)
if err != nil {
return nil, err
}
return links, nil
}
func CreateLink(db *sqlx.DB, releaseID string, link *model.Link) (error) {
_, err := db.Exec(
"INSERT INTO musiclink (release, name, url) "+
"VALUES ($1, $2, $3)",
releaseID,
link.Name,
link.URL,
)
if err != nil {
return err
}
return nil
}
func UpdateLink(db *sqlx.DB, releaseID string, link *model.Link) (error) {
_, err := db.Exec(
"UPDATE musiclink SET "+
"name=$2, url=$3 "+
"WHERE release=$1",
releaseID,
link.Name,
link.URL,
)
if err != nil {
return err
}
return nil
}
func DeleteLink(db *sqlx.DB, releaseID string, link *model.Link) (error) {
_, err := db.Exec(
"DELETE FROM musiclink "+
"WHERE release=$1, name=$2",
releaseID,
link.Name,
)
if err != nil {
return err
}
return nil
}

View file

@ -1,6 +1,9 @@
package music package music
import ( import (
"errors"
"fmt"
"arimelody.me/arimelody.me/music/model" "arimelody.me/arimelody.me/music/model"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
) )
@ -27,28 +30,11 @@ func GetAllReleases(db *sqlx.DB) ([]*model.Release, error) {
return releases, nil return releases, nil
} }
func GetReleaseTracks(db *sqlx.DB, release *model.Release) ([]*model.Track, error) {
var tracks = []*model.Track{}
err := db.Select(&tracks,
"SELECT musictrack.* FROM musictrack "+
"JOIN musicreleasetrack ON track=id "+
"WHERE release=$1 "+
"ORDER BY number ASC",
release.ID,
)
if err != nil {
return nil, err
}
return tracks, nil
}
func CreateRelease(db *sqlx.DB, release *model.Release) error { func CreateRelease(db *sqlx.DB, release *model.Release) error {
_, err := db.Exec( _, err := db.Exec(
"INSERT INTO musicrelease "+ "INSERT INTO musicrelease "+
"(id, visible, title, description, type, release_date, artwork, buyname, buylink) "+ "(id, visible, title, description, type, release_date, artwork, buyname, buylink, copyright, copyrighturl) "+
"VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)", "VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)",
release.ID, release.ID,
release.Visible, release.Visible,
release.Title, release.Title,
@ -58,6 +44,8 @@ func CreateRelease(db *sqlx.DB, release *model.Release) error {
release.Artwork, release.Artwork,
release.Buyname, release.Buyname,
release.Buylink, release.Buylink,
release.Copyright,
release.CopyrightURL,
) )
if err != nil { if err != nil {
return err return err
@ -69,7 +57,7 @@ func CreateRelease(db *sqlx.DB, release *model.Release) error {
func UpdateRelease(db *sqlx.DB, release *model.Release) error { func UpdateRelease(db *sqlx.DB, release *model.Release) error {
_, err := db.Exec( _, err := db.Exec(
"UPDATE musicrelease SET "+ "UPDATE musicrelease SET "+
"visible=$2, title=$3, description=$4, type=$5, release_date=$6, artwork=$7, buyname=$8, buylink=$9 "+ "visible=$2, title=$3, description=$4, type=$5, release_date=$6, artwork=$7, buyname=$8, buylink=$9, copyright=$10, copyrighturl=$11 "+
"WHERE id=$1", "WHERE id=$1",
release.ID, release.ID,
release.Visible, release.Visible,
@ -80,6 +68,8 @@ func UpdateRelease(db *sqlx.DB, release *model.Release) error {
release.Artwork, release.Artwork,
release.Buyname, release.Buyname,
release.Buylink, release.Buylink,
release.Copyright,
release.CopyrightURL,
) )
if err != nil { if err != nil {
return err return err
@ -88,7 +78,7 @@ func UpdateRelease(db *sqlx.DB, release *model.Release) error {
return nil return nil
} }
func UpdateReleaseTracks(db *sqlx.DB, release *model.Release, new_tracks []*model.Track) error { func UpdateReleaseTracks(db *sqlx.DB, release *model.Release, new_tracks []string) error {
_, err := db.Exec( _, err := db.Exec(
"DELETE FROM musicreleasetrack "+ "DELETE FROM musicreleasetrack "+
"WHERE release=$1", "WHERE release=$1",
@ -98,13 +88,13 @@ func UpdateReleaseTracks(db *sqlx.DB, release *model.Release, new_tracks []*mode
return err return err
} }
for i, track := range new_tracks { for i, trackID := range new_tracks {
_, err = db.Exec( _, err = db.Exec(
"INSERT INTO musicreleasetrack "+ "INSERT INTO musicreleasetrack "+
"(release, track, number) "+ "(release, track, number) "+
"VALUES ($1, $2, $3)", "VALUES ($1, $2, $3)",
release.ID, release.ID,
track.ID, trackID,
i, i,
) )
if err != nil { if err != nil {
@ -115,7 +105,7 @@ func UpdateReleaseTracks(db *sqlx.DB, release *model.Release, new_tracks []*mode
return nil return nil
} }
func UpdateReleaseCredits(db *sqlx.DB, release *model.Release, new_credits []*model.Credit) error { func UpdateReleaseCredits(db *sqlx.DB, release *model.Release, new_credits []model.Credit) error {
_, err := db.Exec( _, err := db.Exec(
"DELETE FROM musiccredit "+ "DELETE FROM musiccredit "+
"WHERE release=$1", "WHERE release=$1",
@ -183,17 +173,32 @@ func DeleteRelease(db *sqlx.DB, release *model.Release) error {
return nil return nil
} }
func GetFullRelease(db *sqlx.DB, release *model.Release) (*model.FullRelease, error) { func GetFullRelease(db *sqlx.DB, releaseID string) (*model.FullRelease, error) {
// get credits // get release
credits, err := GetReleaseCredits(db, release) release, err := GetRelease(db, releaseID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// get tracks // get credits
dbTracks, err := GetReleaseTracks(db, release) credits, err := GetReleaseCredits(db, releaseID)
if err != nil { if err != nil {
return nil, err return nil, errors.New(fmt.Sprintf("Credits: %s", err))
}
// get artists
for i, credit := range credits {
artist, err := GetArtist(db, credit.Artist.ID)
if err != nil {
return nil, errors.New(fmt.Sprintf("FATAL: Failed to serve release %s: Artists: %s\n", release.ID, err))
}
credits[i].Artist = *artist
}
// get tracks
dbTracks, err := GetReleaseTracks(db, releaseID)
if err != nil {
return nil, errors.New(fmt.Sprintf("Tracks: %s", err))
} }
tracks := []model.DisplayTrack{} tracks := []model.DisplayTrack{}
for i, track := range dbTracks { for i, track := range dbTracks {
@ -201,9 +206,9 @@ func GetFullRelease(db *sqlx.DB, release *model.Release) (*model.FullRelease, er
} }
// get links // get links
links, err := GetReleaseLinks(db, release) links, err := GetReleaseLinks(db, releaseID)
if err != nil { if err != nil {
return nil, err return nil, errors.New(fmt.Sprintf("Links: %s", err))
} }
return &model.FullRelease{ return &model.FullRelease{
@ -213,3 +218,63 @@ func GetFullRelease(db *sqlx.DB, release *model.Release) (*model.FullRelease, er
Links: links, Links: links,
}, nil }, nil
} }
func GetReleaseTracks(db *sqlx.DB, releaseID string) ([]model.Track, error) {
var tracks = []model.Track{}
err := db.Select(&tracks,
"SELECT musictrack.* FROM musictrack "+
"JOIN musicreleasetrack ON track=id "+
"WHERE release=$1 "+
"ORDER BY number ASC",
releaseID,
)
if err != nil {
return nil, err
}
return tracks, nil
}
func GetReleaseCredits(db *sqlx.DB, releaseID string) ([]model.Credit, error) {
type DBCredit struct {
Release string
Artist string
Role string
Primary bool `db:"is_primary"`
}
var dbCredits []DBCredit
err := db.Select(&dbCredits,
"SELECT musiccredit.* FROM musiccredit "+
"JOIN artist ON artist=id "+
"WHERE release=$1",
releaseID,
)
if err != nil {
return nil, err
}
var credits []model.Credit
for _, credit := range dbCredits {
credits = append(credits, model.Credit{
Release: model.Release{ ID: credit.Release },
Artist: model.Artist{ ID: credit.Artist },
Role: credit.Role,
Primary: credit.Primary,
})
}
return credits, nil
}
func GetReleaseLinks(db *sqlx.DB, releaseID string) ([]model.Link, error) {
var links = []model.Link{}
err := db.Select(&links, "SELECT name,url FROM musiclink WHERE release=$1", releaseID)
if err != nil {
return nil, err
}
return links, nil
}

View file

@ -40,14 +40,14 @@ func GetOrphanTracks(db *sqlx.DB) ([]*model.Track, error) {
return tracks, nil return tracks, nil
} }
func GetTracksNotOnRelease(db *sqlx.DB, release *model.Release) ([]*model.Track, error) { func GetTracksNotOnRelease(db *sqlx.DB, releaseID string) ([]*model.Track, error) {
var tracks = []*model.Track{} var tracks = []*model.Track{}
err := db.Select(&tracks, err := db.Select(&tracks,
"SELECT * FROM musictrack "+ "SELECT * FROM musictrack "+
"WHERE id NOT IN "+ "WHERE id NOT IN "+
"(SELECT track FROM musicreleasetrack WHERE release=$1)", "(SELECT track FROM musicreleasetrack WHERE release=$1)",
release.ID) releaseID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -55,15 +55,15 @@ func GetTracksNotOnRelease(db *sqlx.DB, release *model.Release) ([]*model.Track,
return tracks, nil return tracks, nil
} }
func GetTrackReleases(db *sqlx.DB, track *model.Track) ([]*model.Release, error) { func GetTrackReleases(db *sqlx.DB, trackID string) ([]model.ReleaseShorthand, error) {
var releases = []*model.Release{} var releases = []model.ReleaseShorthand{}
err := db.Select(&releases, err := db.Select(&releases,
"SELECT musicrelease.* FROM musicrelease "+ "SELECT id,title,type,release_date,artwork,buylink FROM musicrelease "+
"JOIN musicreleasetrack ON release=id "+ "JOIN musicreleasetrack ON release=id "+
"WHERE track=$1 "+ "WHERE track=$1 "+
"ORDER BY release_date", "ORDER BY release_date",
track.ID, trackID,
) )
if err != nil { if err != nil {
return nil, err return nil, err
@ -123,11 +123,11 @@ func UpdateTrack(db *sqlx.DB, track *model.Track) error {
return nil return nil
} }
func DeleteTrack(db *sqlx.DB, track *model.Track) error { func DeleteTrack(db *sqlx.DB, trackID string) error {
_, err := db.Exec( _, err := db.Exec(
"DELETE FROM musictrack "+ "DELETE FROM musictrack "+
"WHERE id=$1", "WHERE id=$1",
track.ID, trackID,
) )
if err != nil { if err != nil {
return err return err

View file

@ -1,7 +1,8 @@
package model package model
type Credit struct { type Credit struct {
Artist `json:"artist"` Release Release `json:"release"`
Artist Artist `json:"artist"`
Role string `json:"role"` Role string `json:"role"`
Primary bool `json:"primary" db:"is_primary"` Primary bool `json:"primary" db:"is_primary"`
} }

View file

@ -23,9 +23,18 @@ type (
FullRelease struct { FullRelease struct {
*Release *Release
Tracks []DisplayTrack Tracks []DisplayTrack `json:"tracks"`
Credits []Credit Credits []Credit `json:"credits"`
Links []Link Links []Link `json:"links"`
}
ReleaseShorthand struct {
ID string `json:"id"`
Title string `json:"title"`
ReleaseType ReleaseType `json:"type" db:"type"`
ReleaseDate time.Time `json:"releaseDate" db:"release_date"`
Artwork string `json:"artwork"`
Buylink string `json:"buylink"`
} }
) )

View file

@ -10,14 +10,14 @@ type (
ID string `json:"id"` ID string `json:"id"`
Title string `json:"title"` Title string `json:"title"`
Description string `json:"description"` Description string `json:"description"`
Lyrics string `json:"lyrics"` Lyrics string `json:"lyrics" db:"lyrics"`
PreviewURL string `json:"previewURL" db:"preview_url"` PreviewURL string `json:"previewURL" db:"preview_url"`
} }
DisplayTrack struct { DisplayTrack struct {
*Track *Track
Lyrics template.HTML Lyrics template.HTML `json:"lyrics"`
Number int Number int `json:"-"`
} }
) )

View file

@ -48,7 +48,7 @@ func ServeCatalog() http.Handler {
if !dbRelease.IsReleased() { if !dbRelease.IsReleased() {
dbRelease.ReleaseType = model.Upcoming dbRelease.ReleaseType = model.Upcoming
} }
release, err := music.GetFullRelease(global.DB, dbRelease) release, err := music.GetFullRelease(global.DB, dbRelease.ID)
if err != nil { if err != nil {
fmt.Printf("FATAL: Failed to pull full release for %s: %s\n", dbRelease.ID, err) fmt.Printf("FATAL: Failed to pull full release for %s: %s\n", dbRelease.ID, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)

View file

@ -12,7 +12,26 @@ import (
"arimelody.me/arimelody.me/templates" "arimelody.me/arimelody.me/templates"
) )
// HTTP HANDLERS type (
Track struct {
Title string `json:"title"`
Description string `json:"description"`
Lyrics string `json:"lyrics"`
}
Credit struct {
*model.Artist
Role string `json:"role"`
Primary bool `json:"primary"`
}
Release struct {
model.Release
Tracks []Track `json:"tracks"`
Credits []Credit `json:"credits"`
Links map[string]string `json:"links"`
}
)
func ServeRelease(release model.Release) http.Handler { func ServeRelease(release model.Release) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@ -23,22 +42,65 @@ func ServeRelease(release model.Release) http.Handler {
return return
} }
fullRelease := &model.FullRelease{ response := Release{
Release: &release, Release: release,
Tracks: []Track{},
Credits: []Credit{},
Links: make(map[string]string),
} }
if authorised || release.IsReleased() { if authorised || release.IsReleased() {
fullerRelease, err := db.GetFullRelease(global.DB, &release) // get credits
credits, err := db.GetReleaseCredits(global.DB, release.ID)
if err != nil { if err != nil {
fmt.Printf("FATAL: Failed to pull full release data for %s: %s\n", release.ID, err) fmt.Printf("FATAL: Failed to serve release %s: Credits: %s\n", release.ID, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return return
} }
fullRelease = fullerRelease for _, credit := range credits {
artist, err := db.GetArtist(global.DB, credit.Artist.ID)
if err != nil {
fmt.Printf("FATAL: Failed to serve release %s: Artists: %s\n", release.ID, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
response.Credits = append(response.Credits, Credit{
Artist: artist,
Role: credit.Role,
Primary: credit.Primary,
})
}
// get tracks
tracks, err := db.GetReleaseTracks(global.DB, release.ID)
if err != nil {
fmt.Printf("FATAL: Failed to serve release %s: Tracks: %s\n", release.ID, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
for _, track := range tracks {
response.Tracks = append(response.Tracks, Track{
Title: track.Title,
Description: track.Description,
Lyrics: track.Lyrics,
})
}
// get links
links, err := db.GetReleaseLinks(global.DB, release.ID)
if err != nil {
fmt.Printf("FATAL: Failed to serve release %s: Links: %s\n", release.ID, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
for _, link := range links {
response.Links[link.Name] = link.URL
}
} }
w.Header().Add("Content-Type", "application/json") w.Header().Add("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(fullRelease) err := json.NewEncoder(w).Encode(response)
if err != nil { if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return return
@ -60,7 +122,7 @@ func ServeGateway(release model.Release) http.Handler {
} }
if authorised || release.IsReleased() { if authorised || release.IsReleased() {
fullerRelease, err := db.GetFullRelease(global.DB, &release) fullerRelease, err := db.GetFullRelease(global.DB, release.ID)
if err != nil { if err != nil {
fmt.Printf("FATAL: Failed to pull full release data for %s: %s\n", release.ID, err) fmt.Printf("FATAL: Failed to pull full release data for %s: %s\n", release.ID, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)

View file

@ -311,6 +311,7 @@ ul#links li {
flex-grow: 1; flex-grow: 1;
} }
#buylink,
ul#links a { ul#links a {
width: calc(100% - 1.6em); width: calc(100% - 1.6em);
padding: .5em .8em; padding: .5em .8em;
@ -324,7 +325,9 @@ ul#links a {
transition: filter .1s,-webkit-filter .1s transition: filter .1s,-webkit-filter .1s
} }
ul#links a.buy { #buylink {
margin-top: 1em;
margin-bottom: -.5em;
background-color: #ff94e9 background-color: #ff94e9
} }
@ -390,7 +393,7 @@ ul#links a:hover {
animation: share-after 2s cubic-bezier(.5,0,1,.5) forwards animation: share-after 2s cubic-bezier(.5,0,1,.5) forwards
} }
h2 { #info h2 {
width: fit-content; width: fit-content;
padding: .3em 1em; padding: .3em 1em;
font-size: 1em; font-size: 1em;

View file

@ -29,7 +29,7 @@
{{end}} {{end}}
{{define "content"}} {{define "content"}}
<main> <main >
<script type="module" src="/script/music-gateway.js"></script> <script type="module" src="/script/music-gateway.js"></script>
<div id="background" style="background-image: url({{.GetArtwork}})"></div> <div id="background" style="background-image: url({{.GetArtwork}})"></div>
@ -65,13 +65,10 @@
{{end}} {{end}}
{{if .IsReleased}} {{if .IsReleased}}
<ul id="links">
{{if .Buylink}} {{if .Buylink}}
<li> <a href="{{.Buylink}}" id="buylink">{{or .Buyname "buy"}}</a>
<a href="{{.Buylink}}" class="buy">{{or .Buyname "buy"}}</a>
</li>
{{end}} {{end}}
<ul id="links">
{{range .Links}} {{range .Links}}
<li> <li>
<a class="{{.NormaliseName}}" href="{{.URL}}">{{.Name}}</a> <a class="{{.NormaliseName}}" href="{{.URL}}">{{.Name}}</a>