All blogs
This commit is contained in:
@@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/CalebQ42/darkstorm-server/internal/backend"
|
"github.com/CalebQ42/darkstorm-server/internal/backend"
|
||||||
)
|
)
|
||||||
@@ -42,3 +43,30 @@ func blogHandle(w http.ResponseWriter, r *http.Request, blog string) {
|
|||||||
}
|
}
|
||||||
sendContent(w, r, bl.HTMX(blogApp, r.Context()), bl.Title, bl.Favicon)
|
sendContent(w, r, bl.HTMX(blogApp, r.Context()), bl.Title, bl.Favicon)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func blogListHandle(w http.ResponseWriter, r *http.Request) {
|
||||||
|
pag, _ := strconv.Atoi(r.URL.Query().Get("page"))
|
||||||
|
list, err := blogApp.BlogList(r.Context(), int64(pag))
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
sendContent(w, r, "Error getting page", "", "")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
out := ""
|
||||||
|
for i := range list {
|
||||||
|
out += "<p>" + list[i].HTMX() + "</p>"
|
||||||
|
}
|
||||||
|
if pag > 0 || len(list) == 50 {
|
||||||
|
out += "<div id='blog-list-page-selector'>"
|
||||||
|
if pag > 0 {
|
||||||
|
pagNum := strconv.Itoa(pag - 1)
|
||||||
|
out += "<a href='https://darkstorm.tech/list?page=" + pagNum + "' hx-get='/list?page='" + pagNum + "' hx-push-url='true' hx-target='#content'><Previous</a>"
|
||||||
|
}
|
||||||
|
if len(list) == 50 {
|
||||||
|
pagNum := strconv.Itoa(pag + 1)
|
||||||
|
out += "<a href='https://darkstorm.tech/list?page=" + pagNum + "' hx-get='/list?page='" + pagNum + "' hx-push-url='true' hx-target='#content'>Next></a>"
|
||||||
|
}
|
||||||
|
out += "</div>"
|
||||||
|
}
|
||||||
|
sendContent(w, r, out, "", "")
|
||||||
|
}
|
||||||
|
|||||||
+12
-3
@@ -278,13 +278,22 @@ func (b *BlogApp) reqLatestBlogs(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
type BlogListResult struct {
|
type BlogListResult struct {
|
||||||
ID string `json:"id" bson:"_id"`
|
ID string `json:"id" bson:"_id"`
|
||||||
|
Title string `json:"title" bson:"title"`
|
||||||
CreateTime int `json:"createTime" bson:"createTime"`
|
CreateTime int `json:"createTime" bson:"createTime"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b BlogListResult) HTMX() string {
|
||||||
|
return "<a class='blog-list-item' href='https://darkstorm.tech/" +
|
||||||
|
b.ID +
|
||||||
|
"' hx-push-url='true' hx-target='#content' hx-get='/" +
|
||||||
|
b.ID +
|
||||||
|
"'>" + b.Title + "</a>"
|
||||||
|
}
|
||||||
|
|
||||||
func (b *BlogApp) BlogList(ctx context.Context, page int64) ([]BlogListResult, error) {
|
func (b *BlogApp) BlogList(ctx context.Context, page int64) ([]BlogListResult, error) {
|
||||||
res, err := b.blogCol.Find(ctx, bson.M{}, options.Find().
|
res, err := b.blogCol.Find(ctx, bson.M{"staticPage": false, "draft": false}, options.Find().
|
||||||
SetProjection(bson.M{"_id": 1, "createTime": 1}).
|
SetProjection(bson.M{"_id": 1, "createTime": 1, "title": 1}).
|
||||||
SetSort(bson.M{"createTime": 1}).
|
SetSort(bson.M{"createTime": -1}).
|
||||||
SetLimit(50).
|
SetLimit(50).
|
||||||
SetSkip(page*50))
|
SetSkip(page*50))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -122,6 +122,7 @@ func setupWebsite(mux *http.ServeMux) {
|
|||||||
}
|
}
|
||||||
mux.HandleFunc("GET /files/{w...}", filesRequest)
|
mux.HandleFunc("GET /files/{w...}", filesRequest)
|
||||||
mux.HandleFunc("GET /portfolio", portfolioRequest)
|
mux.HandleFunc("GET /portfolio", portfolioRequest)
|
||||||
|
mux.HandleFunc("GET /list", blogListHandle)
|
||||||
mux.HandleFunc("/", mainHandle)
|
mux.HandleFunc("/", mainHandle)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user