Fix tests, remove unused admin page and code

This commit is contained in:
Ben Visness 2022-07-23 12:34:20 -05:00
parent 160f5d94c4
commit d5bdb9a7af
6 changed files with 12 additions and 138 deletions

View File

@ -1,57 +0,0 @@
package hmndata
import (
"context"
"git.handmade.network/hmn/hmn/src/db"
"git.handmade.network/hmn/hmn/src/models"
"git.handmade.network/hmn/hmn/src/oops"
"git.handmade.network/hmn/hmn/src/perf"
)
type EduArticleQuery struct {
Slugs []string
Types []models.EduArticleType
}
type EduArticleAndStuff struct {
Article models.EduArticle `db:"a"`
CurrentVersion models.EduArticleVersion `db:"v"`
}
func FetchEduArticles(
ctx context.Context,
dbConn db.ConnOrTx,
currentUser *models.User,
q EduArticleQuery,
) ([]*EduArticleAndStuff, error) {
perf := perf.ExtractPerf(ctx)
perf.StartBlock("SQL", "Fetch education articles")
defer perf.EndBlock()
var qb db.QueryBuilder
qb.Add(`
SELECT $columns
FROM
education_article AS a
JOIN education_article_version AS v ON a.current_version = v.id
WHERE
TRUE
`)
if len(q.Slugs) > 0 {
qb.Add(`AND a.slug = ANY ($?)`, q.Slugs)
}
if len(q.Types) > 0 {
qb.Add(`AND a.type = ANY ($?)`, q.Types)
}
if currentUser == nil || !currentUser.CanSeeUnpublishedEducationContent() {
qb.Add(`AND a.published`)
}
articles, err := db.Query[EduArticleAndStuff](ctx, dbConn, qb.String(), qb.Args()...)
if err != nil {
return nil, oops.New(err, "failed to fetch education articles")
}
return articles, nil
}

View File

@ -199,6 +199,18 @@ func TestEducationArticle(t *testing.T) {
AssertRegexMatch(t, BuildEducationArticle("foo"), RegexEducationArticle, map[string]string{"slug": "foo"})
}
func TestEducationArticleNew(t *testing.T) {
AssertRegexMatch(t, BuildEducationArticleNew(), RegexEducationArticleNew, nil)
}
func TestEducationArticleEdit(t *testing.T) {
AssertRegexMatch(t, BuildEducationArticleEdit("foo"), RegexEducationArticleEdit, map[string]string{"slug": "foo"})
}
func TestEducationArticleDelete(t *testing.T) {
AssertRegexMatch(t, BuildEducationArticleDelete("foo"), RegexEducationArticleDelete, map[string]string{"slug": "foo"})
}
func TestForum(t *testing.T) {
AssertRegexMatch(t, hmn.BuildForum(nil, 1), RegexForum, nil)
AssertRegexMatch(t, hmn.BuildForum([]string{"wip"}, 2), RegexForum, map[string]string{"subforums": "wip", "page": "2"})
@ -296,22 +308,6 @@ func TestBlogPostReply(t *testing.T) {
AssertSubdomain(t, hero.BuildBlogPostReply(1, 2), "hero")
}
func TestLibrary(t *testing.T) {
AssertRegexMatch(t, BuildLibrary(), RegexLibrary, nil)
}
func TestLibraryAll(t *testing.T) {
AssertRegexMatch(t, BuildLibraryAll(), RegexLibraryAll, nil)
}
func TestLibraryTopic(t *testing.T) {
AssertRegexMatch(t, BuildLibraryTopic(1), RegexLibraryTopic, map[string]string{"topicid": "1"})
}
func TestLibraryResource(t *testing.T) {
AssertRegexMatch(t, BuildLibraryResource(1), RegexLibraryResource, map[string]string{"resourceid": "1"})
}
func TestEpisodeGuide(t *testing.T) {
AssertRegexMatch(t, hero.BuildEpisodeList(""), RegexEpisodeList, map[string]string{"topic": ""})
AssertRegexMatch(t, hero.BuildEpisodeList("code"), RegexEpisodeList, map[string]string{"topic": "code"})

View File

@ -481,13 +481,6 @@ func BuildEducationArticleDelete(slug string) string {
return Url(fmt.Sprintf("/education/%s/delete", slug), nil)
}
var RegexEducationAdmin = regexp.MustCompile(`^/education/admin$`)
func BuildEducationAdmin() string {
defer CatchPanic()
return Url("/education/admin", nil)
}
/*
* Forums
*/

View File

@ -1,21 +0,0 @@
{{ template "base.html" . }}
{{ define "content" }}
<h2>Articles</h2>
<ul>
{{ range .Articles }}
<li>
{{ .Title }}
</li>
{{ end }}
</ul>
<h2>Glossary Terms</h2>
<ul>
{{ range .GlossaryTerms }}
<li>
{{ .Title }}
</li>
{{ end }}
</ul>
{{ end }}

View File

@ -9,7 +9,6 @@ import (
"time"
"git.handmade.network/hmn/hmn/src/db"
"git.handmade.network/hmn/hmn/src/hmndata"
"git.handmade.network/hmn/hmn/src/hmnurl"
"git.handmade.network/hmn/hmn/src/models"
"git.handmade.network/hmn/hmn/src/parsing"
@ -93,41 +92,6 @@ func EducationArticle(c *RequestContext) ResponseData {
return res
}
func EducationAdmin(c *RequestContext) ResponseData {
articles, err := hmndata.FetchEduArticles(c, c.Conn, c.CurrentUser, hmndata.EduArticleQuery{})
if err != nil {
return c.ErrorResponse(http.StatusInternalServerError, err)
}
var tmplArticles []templates.EduArticle
var tmplGlossaryTerms []templates.EduArticle
for _, a := range articles {
tmpl := templates.EducationArticleToTemplate(&a.Article)
switch a.Article.Type {
case models.EduArticleTypeArticle:
tmplArticles = append(tmplArticles, tmpl)
case models.EduArticleTypeGlossary:
tmplGlossaryTerms = append(tmplGlossaryTerms, tmpl)
}
}
type adminData struct {
templates.BaseData
Articles []templates.EduArticle
GlossaryTerms []templates.EduArticle
}
tmpl := adminData{
BaseData: getBaseData(c, "Education Admin", nil),
Articles: tmplArticles,
GlossaryTerms: tmplGlossaryTerms,
}
var res ResponseData
res.MustWriteTemplate("education_admin.html", tmpl, c.Perf)
return res
}
func EducationArticleNew(c *RequestContext) ResponseData {
type adminData struct {
editorData

View File

@ -119,7 +119,6 @@ func NewWebsiteRoutes(conn *pgxpool.Pool) http.Handler {
hmnOnly.GET(hmnurl.RegexEducationIndex, EducationIndex)
hmnOnly.GET(hmnurl.RegexEducationGlossary, EducationGlossary)
hmnOnly.GET(hmnurl.RegexEducationAdmin, educationAuthorsOnly(EducationAdmin))
hmnOnly.GET(hmnurl.RegexEducationArticleNew, educationAuthorsOnly(EducationArticleNew))
hmnOnly.POST(hmnurl.RegexEducationArticleNew, educationAuthorsOnly(EducationArticleNewSubmit))
hmnOnly.GET(hmnurl.RegexEducationArticle, EducationArticle) // Article stuff must be last so `/glossary` and others do not match as an article slug