diff --git a/admin/http.go b/admin/http.go index 4bbc9b6..55265cb 100644 --- a/admin/http.go +++ b/admin/http.go @@ -62,39 +62,69 @@ func adminIndexHandler(app *model.AppState) http.Handler { releases, err := controller.GetAllReleases(app.DB, false, 3, true) if err != nil { - fmt.Fprintf(os.Stderr, "WARN: Failed to pull releases: %s\n", err) + fmt.Fprintf(os.Stderr, "WARN: Failed to pull releases: %v\n", err) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } releaseCount, err := controller.GetReleaseCount(app.DB, false) if err != nil { - fmt.Fprintf(os.Stderr, "WARN: Failed to pull releases count: %s\n", err) + fmt.Fprintf(os.Stderr, "WARN: Failed to pull releases count: %v\n", err) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } artists, err := controller.GetAllArtists(app.DB) if err != nil { - fmt.Fprintf(os.Stderr, "WARN: Failed to pull artists: %s\n", err) + fmt.Fprintf(os.Stderr, "WARN: Failed to pull artists: %v\n", err) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } artistCount, err := controller.GetArtistCount(app.DB) if err != nil { - fmt.Fprintf(os.Stderr, "WARN: Failed to pull artist count: %s\n", err) + fmt.Fprintf(os.Stderr, "WARN: Failed to pull artist count: %v\n", err) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } tracks, err := controller.GetOrphanTracks(app.DB) if err != nil { - fmt.Fprintf(os.Stderr, "WARN: Failed to pull orphan tracks: %s\n", err) + fmt.Fprintf(os.Stderr, "WARN: Failed to pull orphan tracks: %v\n", err) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } trackCount, err := controller.GetTrackCount(app.DB) if err != nil { - fmt.Fprintf(os.Stderr, "WARN: Failed to pull track count: %s\n", err) + fmt.Fprintf(os.Stderr, "WARN: Failed to pull track count: %v\n", err) + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + return + } + + type BlogPost struct { + *model.BlogPost + Author *model.Account + } + blogPosts, err := controller.GetBlogPosts(app.DB, false, 1, 0) + if err != nil { + fmt.Fprintf(os.Stderr, "WARN: Failed to pull blog posts: %v\n", err) + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + return + } + var latestBlogPost *BlogPost = nil + if len(blogPosts) > 0 { + author, err := controller.GetAccountByID(app.DB, blogPosts[0].AuthorID) + if err != nil { + fmt.Fprintf(os.Stderr, "WARN: Failed to pull latest blog post author %s: %v\n", blogPosts[0].AuthorID, err) + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + return + } + latestBlogPost = &BlogPost{ + BlogPost: blogPosts[0], + Author: author, + } + } + blogCount, err := controller.GetBlogPostCount(app.DB, false) + if err != nil { + fmt.Fprintf(os.Stderr, "WARN: Failed to pull blog post count: %v\n", err) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } @@ -107,6 +137,8 @@ func adminIndexHandler(app *model.AppState) http.Handler { ArtistCount int Tracks []*model.Track TrackCount int + BlogPost *BlogPost + BlogCount int } err = templates.IndexTemplate.Execute(w, IndexData{ @@ -117,9 +149,11 @@ func adminIndexHandler(app *model.AppState) http.Handler { ArtistCount: artistCount, Tracks: tracks, TrackCount: trackCount, + BlogPost: latestBlogPost, + BlogCount: blogCount, }) if err != nil { - fmt.Fprintf(os.Stderr, "WARN: Failed to render admin index: %s\n", err) + fmt.Fprintf(os.Stderr, "WARN: Failed to render admin index: %v\n", err) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } diff --git a/admin/static/blog.js b/admin/static/blog.js index e69de29..3ce9111 100644 --- a/admin/static/blog.js +++ b/admin/static/blog.js @@ -0,0 +1,25 @@ +document.addEventListener('readystatechange', () => { + const newBlogBtn = document.getElementById("create-post"); + if (newBlogBtn) newBlogBtn.addEventListener("click", event => { + event.preventDefault(); + const id = prompt("Enter an ID for this blog post:"); + if (id == null || id == "") return; + + fetch("/api/v1/blog", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({id}) + }).then(res => { + if (res.ok) location = "/admin/blogs/" + id; + else { + res.text().then(err => { + alert(err); + console.error(err); + }); + } + }).catch(err => { + alert("Failed to create release. Check the console for details."); + console.error(err); + }); + }); +}); diff --git a/admin/templates/html/index.html b/admin/templates/html/index.html index af08c09..90214f1 100644 --- a/admin/templates/html/index.html +++ b/admin/templates/html/index.html @@ -4,6 +4,7 @@ + {{end}} {{define "content"}} @@ -52,6 +53,18 @@ {{block "track" .}}{{end}} {{end}} + +
+
+

Latest Blog Post ({{.BlogCount}} total)

+ Create New +
+ {{if .BlogPost}} + {{block "blogpost" .BlogPost}}{{end}} + {{else}} +

There are no blog posts.

+ {{end}} +
@@ -59,4 +72,5 @@ + {{end}} diff --git a/admin/templates/templates.go b/admin/templates/templates.go index 6244540..924188b 100644 --- a/admin/templates/templates.go +++ b/admin/templates/templates.go @@ -43,6 +43,7 @@ var IndexTemplate = template.Must(template.Must(BaseTemplate.Clone()).Parse( componentReleaseHTML, componentArtistHTML, componentTrackHTML, + componentBlogPostHTML, }, "\n"), )) diff --git a/controller/blog.go b/controller/blog.go index 7fb201e..4103737 100644 --- a/controller/blog.go +++ b/controller/blog.go @@ -44,6 +44,18 @@ func GetBlogPosts(db *sqlx.DB, onlyVisible bool, limit int, offset int) ([]*mode return blogs, nil } +func GetBlogPostCount(db *sqlx.DB, onlyVisible bool) (int, error) { + query := "SELECT count(*) FROM blogpost" + if onlyVisible { + query += " WHERE visible=true" + } + + var count int + err := db.Get(&count, query) + + return count, err +} + func CreateBlogPost(db *sqlx.DB, post *model.BlogPost) error { _, err := db.Exec( "INSERT INTO blogpost (id,title,description,visible,author,markdown,html,bluesky_actor,bluesky_post) " +