list user index

This commit is contained in:
Niko Abeler 2022-07-24 20:29:31 +02:00
parent 50bb7ff078
commit ec1c18bc54
8 changed files with 132 additions and 21 deletions

View File

@ -50,19 +50,16 @@ func handler(repo kiss.Repository) func(http.ResponseWriter, *http.Request) {
func indexHandler(repo kiss.Repository) func(http.ResponseWriter, *http.Request) { func indexHandler(repo kiss.Repository) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
users, err := repo.Users() html, err := kiss.RenderUserList(repo)
if err != nil { if err != nil {
println("Error getting users: ", err.Error()) println("Error rendering index: ", err.Error())
w.Write([]byte("Error getting users")) w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Internal server error"))
return return
} }
w.Write([]byte("Index")) println("Rendering index")
w.Write([]byte("<ul>")) w.Write([]byte(html))
for _, user := range users {
w.Write([]byte("<li>"))
w.Write([]byte(user.Name()))
w.Write([]byte("</li>"))
}
} }
} }

View File

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ .Title }}</title>
<link rel="stylesheet" href="/static/pico.min.css">
</head>
<body>
{{ .Content }}
</body>
</html>

View File

@ -1,8 +1,8 @@
{{range .Users}} {{range .}}
<ul> <ul>
<li> <li>
<a href="{{ .Path() }}"> <a href="{{ .Path }}">
{{ .Name() }} {{ .Name }}
</a> </a>
</li> </li>
</ul> </ul>

View File

@ -30,6 +30,11 @@ func getTestUser() kiss.User {
return user return user
} }
func getTestRepo() kiss.Repository {
repo, _ := kiss.CreateRepository(testRepoName())
return repo
}
func contains(s []string, e string) bool { func contains(s []string, e string) bool {
for _, a := range s { for _, a := range s {
if a == e { if a == e {

View File

@ -66,16 +66,40 @@ func RenderIndexPage(user User) (string, error) {
var html bytes.Buffer var html bytes.Buffer
t, err := template.New("index").Parse(baseTemplate) t, err := template.New("index").Parse(baseTemplate)
if err != nil {
return "", err
}
t.Execute(&html, data) t.Execute(&html, data)
return html.String(), err return html.String(), nil
} }
// func RenderUserList(user User) (string, error) { func RenderUserList(repo Repository) (string, error) {
// base_template, _ := user.Template() baseTemplate, _ := repo.Template()
// users, _ := user.repo.Users() users, _ := repo.Users()
// template.New("user_list").Parse() t, err := template.New("user_list").Parse(userListTemplateStr)
// return strings.Replace(template, "{{content}}", userHtml, -1), nil if err != nil {
// } return "", err
}
var userHtml bytes.Buffer
t.Execute(&userHtml, users)
data := PageContent{
Title: "Index",
Content: template.HTML(userHtml.String()),
}
var html bytes.Buffer
t, err = template.New("index").Parse(baseTemplate)
if err != nil {
return "", err
}
t.Execute(&html, data)
return html.String(), nil
}

View File

@ -2,6 +2,8 @@ package kiss_test
import ( import (
"h4kor/kiss-social" "h4kor/kiss-social"
"os"
"path"
"strings" "strings"
"testing" "testing"
) )
@ -49,3 +51,34 @@ func TestCanRenderIndexPage(t *testing.T) {
t.Error("Post title not rendered. Got: " + result) t.Error("Post title not rendered. Got: " + result)
} }
} }
func TestRenderIndexPageWithBrokenBaseTemplate(t *testing.T) {
user := getTestUser()
user.CreateNewPost("testpost1")
user.CreateNewPost("testpost2")
os.WriteFile(path.Join(user.Dir(), "meta/base.html"), []byte("{{content}}"), 0644)
_, err := kiss.RenderIndexPage(user)
if err == nil {
t.Error("Expected error rendering index page, got nil")
}
}
func TestRenderUserList(t *testing.T) {
repo := getTestRepo()
repo.CreateUser("user1")
repo.CreateUser("user2")
result, err := kiss.RenderUserList(repo)
if err != nil {
t.Error("Error rendering user list: " + err.Error())
}
if !strings.Contains(result, "user1") {
t.Error("Post title not rendered. Got: " + result)
}
if !strings.Contains(result, "user2") {
t.Error("Post title not rendered. Got: " + result)
}
}

View File

@ -4,6 +4,7 @@ import (
"embed" "embed"
_ "embed" _ "embed"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path" "path"
) )
@ -11,7 +12,7 @@ import (
//go:embed embed/initial/base.html //go:embed embed/initial/base.html
var base_template string var base_template string
//go:embed embed/initial/static/* //go:embed embed/*
var static_files embed.FS var static_files embed.FS
var VERSION = "0.0.1" var VERSION = "0.0.1"
@ -40,6 +41,10 @@ func CreateRepository(name string) (Repository, error) {
src_data, _ := static_files.ReadFile(file.Name()) src_data, _ := static_files.ReadFile(file.Name())
os.WriteFile(newRepo.StaticDir()+"/"+file.Name(), src_data, 0644) os.WriteFile(newRepo.StaticDir()+"/"+file.Name(), src_data, 0644)
} }
// copy repo_base.html to base.html
src_data, _ := static_files.ReadFile("embed/initial/repo_base.html")
os.WriteFile(newRepo.Dir()+"/base.html", src_data, 0644)
return newRepo, nil return newRepo, nil
} }
@ -66,6 +71,16 @@ func (repo Repository) UsersDir() string {
return path.Join(repo.Dir(), "users") return path.Join(repo.Dir(), "users")
} }
func (repo Repository) Template() (string, error) {
// load base.html
path := path.Join(repo.Dir(), "base.html")
base_html, err := ioutil.ReadFile(path)
if err != nil {
return "", err
}
return string(base_html), nil
}
func (repo Repository) Users() ([]User, error) { func (repo Repository) Users() ([]User, error) {
userNames := listDir(repo.UsersDir()) userNames := listDir(repo.UsersDir())
users := make([]User, len(userNames)) users := make([]User, len(userNames))

View File

@ -157,3 +157,24 @@ func TestNewRepoGetsStaticFiles(t *testing.T) {
t.Error("No static files found") t.Error("No static files found")
} }
} }
func TestNewRepoGetsBaseHtml(t *testing.T) {
// Create a new user
repo, _ := kiss.CreateRepository(testRepoName())
if _, err := os.Stat(path.Join(repo.Dir(), "/base.html")); err != nil {
t.Error("Base html file not found")
}
}
func TestCanGetRepoTemplate(t *testing.T) {
// Create a new user
repo, _ := kiss.CreateRepository(testRepoName())
// Get the user
template, err := repo.Template()
if err != nil {
t.Error("Error getting template: ", err.Error())
}
if template == "" {
t.Error("Template not returned")
}
}