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 @@