From e0517643a657d77c7e14a33f5987c665a53337e4 Mon Sep 17 00:00:00 2001 From: xing Date: Wed, 14 Sep 2022 21:30:59 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A6=96=E9=A1=B5=20=E8=BF=91=E6=9C=9F?= =?UTF-8?q?=E6=96=87=E7=AB=A0=20=E5=BD=92=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- middleware/cache.go | 1 + models/globalInit.go | 2 +- models/model.go | 13 +++++++--- models/wp_posts.go | 14 ++++++++++ route/actions.go | 29 ++++++++++++++++++--- route/route.go | 10 +++++-- templates/layout/sidebar.html | 49 +++++------------------------------ templates/templatefs.go | 6 +++++ 8 files changed, 72 insertions(+), 52 deletions(-) create mode 100644 templates/templatefs.go diff --git a/middleware/cache.go b/middleware/cache.go index f904fb7..0701f9c 100644 --- a/middleware/cache.go +++ b/middleware/cache.go @@ -11,4 +11,5 @@ func SetStaticFileCache(c *gin.Context) { if len(f) > 1 && helper.IsContainInArr(f[0], []string{"wp-includes", "wp-content"}) { c.Header("Cache-Control", "private, max-age=86400") } + c.Next() } diff --git a/models/globalInit.go b/models/globalInit.go index 43225e1..3ab6df2 100644 --- a/models/globalInit.go +++ b/models/globalInit.go @@ -27,7 +27,7 @@ func InitTerms() (err error) { name = append(name, "twentyfifteen") terms, err := Find[WpTerms](SqlBuilder{{ "tt.taxonomy", "in", "", - }, {"t.name", "in", ""}}, "t.term_id", nil, SqlBuilder{{ + }, {"t.name", "in", ""}}, "t.term_id", "", nil, SqlBuilder{{ "t", "inner join", "wp_term_taxonomy tt", "t.term_id = tt.term_id", }}, 1, themes, name) for _, wpTerms := range terms { diff --git a/models/model.go b/models/model.go index ef6e5c6..d66f75d 100644 --- a/models/model.go +++ b/models/model.go @@ -213,16 +213,23 @@ func Select[T Model](sql string, params ...interface{}) ([]T, error) { return r, nil } -func Find[T Model](where ParseWhere, fields string, order SqlBuilder, join SqlBuilder, limit int, in ...[]interface{}) (r []T, err error) { +func Find[T Model](where ParseWhere, fields, group string, order SqlBuilder, join SqlBuilder, limit int, in ...[]interface{}) (r []T, err error) { var rr T w, args := where.ParseWhere(in...) j := join.parseJoin() - tp := "select %s from %s %s %s %s %s" + groupBy := "" + if group != "" { + g := strings.Builder{} + g.WriteString(" group by ") + g.WriteString(group) + groupBy = g.String() + } + tp := "select %s from %s %s %s %s %s %s" l := "" if limit > 0 { l = fmt.Sprintf(" limit %d", limit) } - sql := fmt.Sprintf(tp, fields, rr.Table(), j, w, order.parseOrderBy(), l) + sql := fmt.Sprintf(tp, fields, rr.Table(), j, w, groupBy, order.parseOrderBy(), l) err = db.Db.Select(&r, sql, args...) return } diff --git a/models/wp_posts.go b/models/wp_posts.go index a401c9e..e82808a 100644 --- a/models/wp_posts.go +++ b/models/wp_posts.go @@ -38,3 +38,17 @@ func (w WpPosts) PrimaryKey() string { func (w WpPosts) Table() string { return "wp_posts" } + +func (w PostArchive) PrimaryKey() string { + return "ID" +} + +func (w PostArchive) Table() string { + return "wp_posts" +} + +type PostArchive struct { + Year string `db:"year"` + Month string `db:"month"` + Posts int `db:"posts"` +} diff --git a/route/actions.go b/route/actions.go index 8014e4d..fec2d8d 100644 --- a/route/actions.go +++ b/route/actions.go @@ -36,7 +36,7 @@ func index(c *gin.Context) { if len(needQuery) > 0 { rawPosts, err := models.Find[models.WpPosts](models.SqlBuilder{{ "Id", "in", "", - }}, "a.*,d.name category_name", nil, models.SqlBuilder{{ + }}, "a.*,d.name category_name", "", nil, models.SqlBuilder{{ "a", "left join", "wp_term_relationships b", "a.Id=b.object_id", }, { "left join", "wp_term_taxonomy c", "b.term_taxonomy_id=c.term_taxonomy_id", @@ -56,8 +56,31 @@ func index(c *gin.Context) { pp := post.(models.WpPosts) allPosts = append(allPosts, pp) } + recent, _ := recentPosts() + archive, _ := archives() + c.HTML(http.StatusOK, "index.html", gin.H{ - "posts": allPosts, - "options": models.Options, + "posts": allPosts, + "options": models.Options, + "recentPosts": recent, + "archives": archive, }) } + +func recentPosts() (r []models.WpPosts, err error) { + r, err = models.Find[models.WpPosts](models.SqlBuilder{{ + "post_type", "post", + }, {"post_status", "publish"}}, "ID,post_title", "", models.SqlBuilder{{"post_date", "desc"}}, nil, 5) + if err != nil { + return + } + + return +} + +func archives() (r []models.PostArchive, err error) { + r, err = models.Find[models.PostArchive](models.SqlBuilder{ + {"post_type", "post"}, {"post_status", "publish"}, + }, "YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts", "year,month", models.SqlBuilder{{"year", "desc"}, {"month", "desc"}}, nil, 0) + return +} diff --git a/route/route.go b/route/route.go index 3dd1776..1cfdd03 100644 --- a/route/route.go +++ b/route/route.go @@ -4,6 +4,7 @@ import ( "github.com/gin-gonic/gin" "github/fthvgb1/wp-go/middleware" "github/fthvgb1/wp-go/static" + "github/fthvgb1/wp-go/templates" "html/template" "net/http" "time" @@ -28,9 +29,14 @@ func SetupRouter() *gin.Engine { FS: static.FsEx, Path: "wp-content", })) - r.LoadHTMLGlob("templates/**/*") - // Ping test + loadTemplates(r, "**/*") r.GET("/", index) return r } + +func loadTemplates(engine *gin.Engine, pattern string) { + templ := template.New("").Funcs(engine.FuncMap).Delims("{{", "}}") + templ = template.Must(templ.ParseFS(templates.TemplateFs, pattern)) + engine.SetHTMLTemplate(templ) +} diff --git a/templates/layout/sidebar.html b/templates/layout/sidebar.html index 6f48863..ee3d1a4 100644 --- a/templates/layout/sidebar.html +++ b/templates/layout/sidebar.html @@ -13,21 +13,11 @@

近期文章

@@ -40,36 +30,9 @@