Compare commits
No commits in common. "eaa2f6587d4ce503f028c16d2044381afa34f752" and "21912d4ec21bf30275bc0f1376d7281bdd27ed24" have entirely different histories.
eaa2f6587d
...
21912d4ec2
13 changed files with 41 additions and 379 deletions
|
|
@ -66,7 +66,7 @@ func serveBlogIndex(app *model.AppState) http.Handler {
|
||||||
|
|
||||||
if post.CreatedAt.Year() != collectionYear || i == len(posts) - 1 {
|
if post.CreatedAt.Year() != collectionYear || i == len(posts) - 1 {
|
||||||
if i == len(posts) - 1 {
|
if i == len(posts) - 1 {
|
||||||
collectionPosts = append([]*blogPost{&authoredPost}, collectionPosts...)
|
collectionPosts = append(collectionPosts, &authoredPost)
|
||||||
}
|
}
|
||||||
collections = append(collections, &blogPostGroup{
|
collections = append(collections, &blogPostGroup{
|
||||||
Year: collectionYear,
|
Year: collectionYear,
|
||||||
|
|
|
||||||
|
|
@ -62,69 +62,39 @@ func adminIndexHandler(app *model.AppState) http.Handler {
|
||||||
|
|
||||||
releases, err := controller.GetAllReleases(app.DB, false, 3, true)
|
releases, err := controller.GetAllReleases(app.DB, false, 3, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "WARN: Failed to pull releases: %v\n", err)
|
fmt.Fprintf(os.Stderr, "WARN: Failed to pull releases: %s\n", err)
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
releaseCount, err := controller.GetReleaseCount(app.DB, false)
|
releaseCount, err := controller.GetReleaseCount(app.DB, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "WARN: Failed to pull releases count: %v\n", err)
|
fmt.Fprintf(os.Stderr, "WARN: Failed to pull releases count: %s\n", err)
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
artists, err := controller.GetAllArtists(app.DB)
|
artists, err := controller.GetAllArtists(app.DB)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "WARN: Failed to pull artists: %v\n", err)
|
fmt.Fprintf(os.Stderr, "WARN: Failed to pull artists: %s\n", err)
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
artistCount, err := controller.GetArtistCount(app.DB)
|
artistCount, err := controller.GetArtistCount(app.DB)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "WARN: Failed to pull artist count: %v\n", err)
|
fmt.Fprintf(os.Stderr, "WARN: Failed to pull artist count: %s\n", err)
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
tracks, err := controller.GetOrphanTracks(app.DB)
|
tracks, err := controller.GetOrphanTracks(app.DB)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "WARN: Failed to pull orphan tracks: %v\n", err)
|
fmt.Fprintf(os.Stderr, "WARN: Failed to pull orphan tracks: %s\n", err)
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
trackCount, err := controller.GetTrackCount(app.DB)
|
trackCount, err := controller.GetTrackCount(app.DB)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "WARN: Failed to pull track count: %v\n", err)
|
fmt.Fprintf(os.Stderr, "WARN: Failed to pull track count: %s\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)
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -137,8 +107,6 @@ func adminIndexHandler(app *model.AppState) http.Handler {
|
||||||
ArtistCount int
|
ArtistCount int
|
||||||
Tracks []*model.Track
|
Tracks []*model.Track
|
||||||
TrackCount int
|
TrackCount int
|
||||||
BlogPost *BlogPost
|
|
||||||
BlogCount int
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err = templates.IndexTemplate.Execute(w, IndexData{
|
err = templates.IndexTemplate.Execute(w, IndexData{
|
||||||
|
|
@ -149,11 +117,9 @@ func adminIndexHandler(app *model.AppState) http.Handler {
|
||||||
ArtistCount: artistCount,
|
ArtistCount: artistCount,
|
||||||
Tracks: tracks,
|
Tracks: tracks,
|
||||||
TrackCount: trackCount,
|
TrackCount: trackCount,
|
||||||
BlogPost: latestBlogPost,
|
|
||||||
BlogCount: blogCount,
|
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "WARN: Failed to render admin index: %v\n", err)
|
fmt.Fprintf(os.Stderr, "WARN: Failed to render admin index: %s\n", err)
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,5 @@
|
||||||
.blog-collection {
|
|
||||||
display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
gap: .5em;
|
|
||||||
}
|
|
||||||
|
|
||||||
.blog-collection h2 {
|
.blog-collection h2 {
|
||||||
margin: 0 0 0 1em;
|
margin: .5em 1em;
|
||||||
font-size: 1em;
|
font-size: 1em;
|
||||||
text-transform: uppercase;
|
text-transform: uppercase;
|
||||||
font-weight: 600;
|
font-weight: 600;
|
||||||
|
|
|
||||||
|
|
@ -1,25 +0,0 @@
|
||||||
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);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
@ -4,7 +4,6 @@
|
||||||
<link rel="stylesheet" href="/admin/static/releases.css">
|
<link rel="stylesheet" href="/admin/static/releases.css">
|
||||||
<link rel="stylesheet" href="/admin/static/artists.css">
|
<link rel="stylesheet" href="/admin/static/artists.css">
|
||||||
<link rel="stylesheet" href="/admin/static/tracks.css">
|
<link rel="stylesheet" href="/admin/static/tracks.css">
|
||||||
<link rel="stylesheet" href="/admin/static/blog.css">
|
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
{{define "content"}}
|
{{define "content"}}
|
||||||
|
|
@ -53,18 +52,6 @@
|
||||||
{{block "track" .}}{{end}}
|
{{block "track" .}}{{end}}
|
||||||
{{end}}
|
{{end}}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="card" id="blogs">
|
|
||||||
<div class="card-header">
|
|
||||||
<h2><a href="/admin/blogs/">Latest Blog Post</a> <small>({{.BlogCount}} total)</small></h2>
|
|
||||||
<a class="button new" id="create-post">Create New</a>
|
|
||||||
</div>
|
|
||||||
{{if .BlogPost}}
|
|
||||||
{{block "blogpost" .BlogPost}}{{end}}
|
|
||||||
{{else}}
|
|
||||||
<p>There are no blog posts.</p>
|
|
||||||
{{end}}
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</main>
|
</main>
|
||||||
|
|
@ -72,5 +59,4 @@
|
||||||
<script type="module" src="/admin/static/releases.js"></script>
|
<script type="module" src="/admin/static/releases.js"></script>
|
||||||
<script type="module" src="/admin/static/artists.js"></script>
|
<script type="module" src="/admin/static/artists.js"></script>
|
||||||
<script type="module" src="/admin/static/tracks.js"></script>
|
<script type="module" src="/admin/static/tracks.js"></script>
|
||||||
<script type="module" src="/admin/static/blog.js"></script>
|
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,6 @@ var IndexTemplate = template.Must(template.Must(BaseTemplate.Clone()).Parse(
|
||||||
componentReleaseHTML,
|
componentReleaseHTML,
|
||||||
componentArtistHTML,
|
componentArtistHTML,
|
||||||
componentTrackHTML,
|
componentTrackHTML,
|
||||||
componentBlogPostHTML,
|
|
||||||
}, "\n"),
|
}, "\n"),
|
||||||
))
|
))
|
||||||
|
|
||||||
|
|
|
||||||
40
api/api.go
40
api/api.go
|
|
@ -1,14 +1,14 @@
|
||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"arimelody-web/controller"
|
"arimelody-web/controller"
|
||||||
"arimelody-web/model"
|
"arimelody-web/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Handler(app *model.AppState) http.Handler {
|
func Handler(app *model.AppState) http.Handler {
|
||||||
|
|
@ -45,7 +45,7 @@ func Handler(app *model.AppState) http.Handler {
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
artistIndexHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
mux.Handle("/v1/artist", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
case http.MethodGet:
|
case http.MethodGet:
|
||||||
// GET /api/v1/artist
|
// GET /api/v1/artist
|
||||||
|
|
@ -56,9 +56,7 @@ func Handler(app *model.AppState) http.Handler {
|
||||||
default:
|
default:
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
}
|
}
|
||||||
})
|
}))
|
||||||
mux.Handle("/v1/artist/", artistIndexHandler)
|
|
||||||
mux.Handle("/v1/artist", artistIndexHandler)
|
|
||||||
|
|
||||||
// RELEASE ENDPOINTS
|
// RELEASE ENDPOINTS
|
||||||
|
|
||||||
|
|
@ -89,7 +87,7 @@ func Handler(app *model.AppState) http.Handler {
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
musicIndexHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
mux.Handle("/v1/music", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
case http.MethodGet:
|
case http.MethodGet:
|
||||||
// GET /api/v1/music
|
// GET /api/v1/music
|
||||||
|
|
@ -100,9 +98,7 @@ func Handler(app *model.AppState) http.Handler {
|
||||||
default:
|
default:
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
}
|
}
|
||||||
})
|
}))
|
||||||
mux.Handle("/v1/music/", musicIndexHandler)
|
|
||||||
mux.Handle("/v1/music", musicIndexHandler)
|
|
||||||
|
|
||||||
// TRACK ENDPOINTS
|
// TRACK ENDPOINTS
|
||||||
|
|
||||||
|
|
@ -133,7 +129,7 @@ func Handler(app *model.AppState) http.Handler {
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
trackIndexHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
mux.Handle("/v1/track", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
case http.MethodGet:
|
case http.MethodGet:
|
||||||
// GET /api/v1/track (admin)
|
// GET /api/v1/track (admin)
|
||||||
|
|
@ -144,17 +140,7 @@ func Handler(app *model.AppState) http.Handler {
|
||||||
default:
|
default:
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
}
|
}
|
||||||
})
|
}))
|
||||||
mux.Handle("/v1/track/", trackIndexHandler)
|
|
||||||
mux.Handle("/v1/track", trackIndexHandler)
|
|
||||||
|
|
||||||
// BLOG ENDPOINTS
|
|
||||||
|
|
||||||
mux.Handle("GET /v1/blog/{id}", ServeBlog(app))
|
|
||||||
mux.Handle("PUT /v1/blog/{id}", requireAccount(UpdateBlog(app)))
|
|
||||||
mux.Handle("DELETE /v1/blog/{id}", requireAccount(DeleteBlog(app)))
|
|
||||||
mux.Handle("GET /v1/blog", ServeAllBlogs(app))
|
|
||||||
mux.Handle("POST /v1/blog", requireAccount(CreateBlog(app)))
|
|
||||||
|
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
session, err := getSession(app, r)
|
session, err := getSession(app, r)
|
||||||
|
|
|
||||||
|
|
@ -99,7 +99,7 @@ func CreateArtist(app *model.AppState) http.Handler {
|
||||||
}
|
}
|
||||||
|
|
||||||
if artist.ID == "" {
|
if artist.ID == "" {
|
||||||
http.Error(w, "Artist ID cannot be blank", http.StatusBadRequest)
|
http.Error(w, "Artist ID cannot be blank\n", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if artist.Name == "" { artist.Name = artist.ID }
|
if artist.Name == "" { artist.Name = artist.ID }
|
||||||
|
|
@ -107,7 +107,7 @@ func CreateArtist(app *model.AppState) http.Handler {
|
||||||
err = controller.CreateArtist(app.DB, &artist)
|
err = controller.CreateArtist(app.DB, &artist)
|
||||||
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("Artist %s already exists", artist.ID), http.StatusBadRequest)
|
http.Error(w, fmt.Sprintf("Artist %s already exists\n", artist.ID), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Printf("WARN: Failed to create artist %s: %s\n", artist.ID, err)
|
fmt.Printf("WARN: Failed to create artist %s: %s\n", artist.ID, err)
|
||||||
|
|
|
||||||
217
api/blog.go
217
api/blog.go
|
|
@ -1,217 +0,0 @@
|
||||||
package api
|
|
||||||
|
|
||||||
import (
|
|
||||||
"arimelody-web/controller"
|
|
||||||
"arimelody-web/log"
|
|
||||||
"arimelody-web/model"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func ServeAllBlogs(app *model.AppState) http.Handler {
|
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
session := r.Context().Value("session").(*model.Session)
|
|
||||||
|
|
||||||
onlyVisible := true
|
|
||||||
if session != nil && session.Account != nil {
|
|
||||||
onlyVisible = false
|
|
||||||
}
|
|
||||||
|
|
||||||
posts, err := controller.GetBlogPosts(app.DB, onlyVisible, -1, 0)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "WARN: Failed to fetch blog posts: %v\n", err)
|
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
type (
|
|
||||||
BlogAuthor struct {
|
|
||||||
ID string `json:"id"`
|
|
||||||
Avatar string `json:"avatar"`
|
|
||||||
}
|
|
||||||
|
|
||||||
BlogPost struct {
|
|
||||||
ID string `json:"id"`
|
|
||||||
Title string `json:"title"`
|
|
||||||
Description string `json:"description"`
|
|
||||||
Author BlogAuthor `json:"author"`
|
|
||||||
CreatedAt time.Time `json:"created_at"`
|
|
||||||
}
|
|
||||||
)
|
|
||||||
resPosts := []*BlogPost{}
|
|
||||||
|
|
||||||
for _, post := range posts {
|
|
||||||
author, err := controller.GetAccountByID(app.DB, post.AuthorID)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "WARN: Failed to fetch author for blog post %s: %v\n", post.ID, err)
|
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
resPosts = append(resPosts, &BlogPost{
|
|
||||||
ID: post.ID,
|
|
||||||
Title: post.Title,
|
|
||||||
Description: post.Description,
|
|
||||||
Author: BlogAuthor{
|
|
||||||
ID: author.Username,
|
|
||||||
Avatar: author.AvatarURL.String,
|
|
||||||
},
|
|
||||||
CreatedAt: post.CreatedAt,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
err = json.NewEncoder(w).Encode(resPosts)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "WARN: Failed to serve blog posts: %v\n", err)
|
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func ServeBlog(app *model.AppState) http.Handler {
|
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
session := r.Context().Value("session").(*model.Session)
|
|
||||||
privileged := session != nil && session.Account != nil
|
|
||||||
blogID := r.PathValue("id")
|
|
||||||
|
|
||||||
blog, err := controller.GetBlogPost(app.DB, blogID)
|
|
||||||
if err != nil {
|
|
||||||
if strings.Contains(err.Error(), "no rows") {
|
|
||||||
http.NotFound(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fmt.Fprintf(os.Stderr, "WARN: Failed to fetch blog post %s: %v\n", blogID, err)
|
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if !blog.Visible && !privileged {
|
|
||||||
http.NotFound(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err = json.NewEncoder(w).Encode(blog)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "WARN: Failed to serve blog post %s: %v\n", blogID, err)
|
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func CreateBlog(app *model.AppState) http.Handler {
|
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
session := r.Context().Value("session").(*model.Session)
|
|
||||||
|
|
||||||
var blog model.BlogPost
|
|
||||||
err := json.NewDecoder(r.Body).Decode(&blog)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if blog.ID == "" {
|
|
||||||
http.Error(w, "Post ID cannot be empty", http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if blog.Title == "" { blog.Title = blog.ID }
|
|
||||||
|
|
||||||
if !blog.CreatedAt.Equal(time.Unix(0, 0)) {
|
|
||||||
blog.CreatedAt = time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 0, 0, 0, 0, time.UTC)
|
|
||||||
}
|
|
||||||
|
|
||||||
blog.AuthorID = session.Account.ID
|
|
||||||
|
|
||||||
err = controller.CreateBlogPost(app.DB, &blog)
|
|
||||||
if err != nil {
|
|
||||||
if strings.Contains(err.Error(), "duplicate key") {
|
|
||||||
http.Error(w, fmt.Sprintf("Post %s already exists", blog.ID), http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fmt.Printf("WARN: Failed to create blog %s: %s\n", blog.ID, err)
|
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
app.Log.Info(log.TYPE_BLOG, "Blog post \"%s\" created by \"%s\".", blog.ID, session.Account.Username)
|
|
||||||
|
|
||||||
w.Header().Add("Content-Type", "application/json")
|
|
||||||
w.WriteHeader(http.StatusCreated)
|
|
||||||
err = json.NewEncoder(w).Encode(blog)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("WARN: Blog post %s created, but failed to send JSON response: %s\n", blog.ID, err)
|
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func UpdateBlog(app *model.AppState) http.Handler {
|
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
session := r.Context().Value("session").(*model.Session)
|
|
||||||
blogID := r.PathValue("id")
|
|
||||||
|
|
||||||
blog, err := controller.GetBlogPost(app.DB, blogID)
|
|
||||||
if err != nil {
|
|
||||||
if strings.Contains(err.Error(), "no rows") {
|
|
||||||
http.NotFound(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fmt.Fprintf(os.Stderr, "WARN: Failed to fetch blog post %s: %v\n", blogID, err)
|
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err = json.NewDecoder(r.Body).Decode(blog)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("WARN: Failed to update blog %s: %s\n", blog.ID, err)
|
|
||||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err = controller.UpdateBlogPost(app.DB, blogID, blog)
|
|
||||||
if err != nil {
|
|
||||||
if strings.Contains(err.Error(), "no rows") {
|
|
||||||
http.NotFound(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fmt.Printf("WARN: Failed to update release %s: %s\n", blogID, err)
|
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
||||||
}
|
|
||||||
|
|
||||||
app.Log.Info(log.TYPE_BLOG, "Blog post \"%s\" updated by \"%s\".", blog.ID, session.Account.Username)
|
|
||||||
|
|
||||||
w.Header().Add("Content-Type", "application/json")
|
|
||||||
w.WriteHeader(http.StatusCreated)
|
|
||||||
err = json.NewEncoder(w).Encode(blog)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("WARN: Blog post %s created, but failed to send JSON response: %s\n", blog.ID, err)
|
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func DeleteBlog(app *model.AppState) http.Handler {
|
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
session := r.Context().Value("session").(*model.Session)
|
|
||||||
blogID := r.PathValue("id")
|
|
||||||
|
|
||||||
rowsAffected, err := controller.DeleteBlogPost(app.DB, blogID)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("WARN: Failed to delete blog post %s: %s\n", blogID, err)
|
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if rowsAffected == 0 {
|
|
||||||
http.NotFound(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
app.Log.Info(log.TYPE_BLOG, "Blog post \"%s\" deleted by \"%s\".", blogID, session.Account.Username)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
@ -200,7 +200,7 @@ func CreateRelease(app *model.AppState) http.Handler {
|
||||||
}
|
}
|
||||||
|
|
||||||
if release.ID == "" {
|
if release.ID == "" {
|
||||||
http.Error(w, "Release ID cannot be empty", http.StatusBadRequest)
|
http.Error(w, "Release ID cannot be empty\n", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -216,7 +216,7 @@ func CreateRelease(app *model.AppState) http.Handler {
|
||||||
err = controller.CreateRelease(app.DB, &release)
|
err = controller.CreateRelease(app.DB, &release)
|
||||||
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", release.ID), http.StatusBadRequest)
|
http.Error(w, fmt.Sprintf("Release %s already exists\n", release.ID), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Printf("WARN: Failed to create release %s: %s\n", release.ID, err)
|
fmt.Printf("WARN: Failed to create release %s: %s\n", release.ID, err)
|
||||||
|
|
|
||||||
|
|
@ -86,7 +86,7 @@ func CreateTrack(app *model.AppState) http.Handler {
|
||||||
}
|
}
|
||||||
|
|
||||||
if track.Title == "" {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -121,7 +121,7 @@ func UpdateTrack(app *model.AppState, track *model.Track) http.Handler {
|
||||||
}
|
}
|
||||||
|
|
||||||
if track.Title == "" {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -44,18 +44,6 @@ func GetBlogPosts(db *sqlx.DB, onlyVisible bool, limit int, offset int) ([]*mode
|
||||||
return blogs, nil
|
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 {
|
func CreateBlogPost(db *sqlx.DB, post *model.BlogPost) error {
|
||||||
_, err := db.Exec(
|
_, err := db.Exec(
|
||||||
"INSERT INTO blogpost (id,title,description,visible,author,markdown,html,bluesky_actor,bluesky_post) " +
|
"INSERT INTO blogpost (id,title,description,visible,author,markdown,html,bluesky_actor,bluesky_post) " +
|
||||||
|
|
@ -93,18 +81,3 @@ func UpdateBlogPost(db *sqlx.DB, postID string, post *model.BlogPost) error {
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeleteBlogPost(db *sqlx.DB, postID string) (int64, error) {
|
|
||||||
result, err := db.Exec(
|
|
||||||
"DELETE FROM blogpost "+
|
|
||||||
"WHERE id=$1",
|
|
||||||
postID,
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
rowsAffected, _ := result.RowsAffected()
|
|
||||||
|
|
||||||
return rowsAffected, nil
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -11,17 +11,17 @@ import (
|
||||||
|
|
||||||
type (
|
type (
|
||||||
BlogPost struct {
|
BlogPost struct {
|
||||||
ID string `json:"id" db:"id"`
|
ID string `db:"id"`
|
||||||
Title string `json:"title" db:"title"`
|
Title string `db:"title"`
|
||||||
Description string `json:"description" db:"description"`
|
Description string `db:"description"`
|
||||||
Visible bool `json:"visible" db:"visible"`
|
Visible bool `db:"visible"`
|
||||||
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
CreatedAt time.Time `db:"created_at"`
|
||||||
ModifiedAt sql.NullTime `json:"modified_at" db:"modified_at"`
|
ModifiedAt sql.NullTime `db:"modified_at"`
|
||||||
AuthorID string `json:"author" db:"author"`
|
AuthorID string `db:"author"`
|
||||||
Markdown string `json:"markdown" db:"markdown"`
|
Markdown string `db:"markdown"`
|
||||||
HTML template.HTML `json:"html" db:"html"`
|
HTML template.HTML `db:"html"`
|
||||||
BlueskyActorID *string `json:"bluesky_actor" db:"bluesky_actor"`
|
BlueskyActorID *string `db:"bluesky_actor"`
|
||||||
BlueskyPostID *string `json:"bluesky_post" db:"bluesky_post"`
|
BlueskyPostID *string `db:"bluesky_post"`
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -40,12 +40,12 @@ func (b *BlogPost) GetMonth() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BlogPost) PrintDate() string {
|
func (b *BlogPost) PrintDate() string {
|
||||||
return b.CreatedAt.Format("2 January 2006, 15:04")
|
return b.CreatedAt.Format("2 January 2006, 03:04")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BlogPost) PrintModifiedDate() string {
|
func (b *BlogPost) PrintModifiedDate() string {
|
||||||
if !b.ModifiedAt.Valid {
|
if !b.ModifiedAt.Valid {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
return b.ModifiedAt.Time.Format("2 January 2006, 15:04")
|
return b.ModifiedAt.Time.Format("2 January 2006, 03:04")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue