list user index
This commit is contained in:
parent
50bb7ff078
commit
ec1c18bc54
|
@ -50,19 +50,16 @@ func handler(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) {
|
||||
users, err := repo.Users()
|
||||
html, err := kiss.RenderUserList(repo)
|
||||
|
||||
if err != nil {
|
||||
println("Error getting users: ", err.Error())
|
||||
w.Write([]byte("Error getting users"))
|
||||
println("Error rendering index: ", err.Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte("Internal server error"))
|
||||
return
|
||||
}
|
||||
w.Write([]byte("Index"))
|
||||
w.Write([]byte("<ul>"))
|
||||
for _, user := range users {
|
||||
w.Write([]byte("<li>"))
|
||||
w.Write([]byte(user.Name()))
|
||||
w.Write([]byte("</li>"))
|
||||
}
|
||||
println("Rendering index")
|
||||
w.Write([]byte(html))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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>
|
|
@ -1,8 +1,8 @@
|
|||
{{range .Users}}
|
||||
{{range .}}
|
||||
<ul>
|
||||
<li>
|
||||
<a href="{{ .Path() }}">
|
||||
{{ .Name() }}
|
||||
<a href="{{ .Path }}">
|
||||
{{ .Name }}
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
|
|
|
@ -30,6 +30,11 @@ func getTestUser() kiss.User {
|
|||
return user
|
||||
}
|
||||
|
||||
func getTestRepo() kiss.Repository {
|
||||
repo, _ := kiss.CreateRepository(testRepoName())
|
||||
return repo
|
||||
}
|
||||
|
||||
func contains(s []string, e string) bool {
|
||||
for _, a := range s {
|
||||
if a == e {
|
||||
|
|
38
renderer.go
38
renderer.go
|
@ -66,16 +66,40 @@ func RenderIndexPage(user User) (string, error) {
|
|||
|
||||
var html bytes.Buffer
|
||||
t, err := template.New("index").Parse(baseTemplate)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
t.Execute(&html, data)
|
||||
|
||||
return html.String(), err
|
||||
return html.String(), nil
|
||||
|
||||
}
|
||||
|
||||
// func RenderUserList(user User) (string, error) {
|
||||
// base_template, _ := user.Template()
|
||||
// users, _ := user.repo.Users()
|
||||
// template.New("user_list").Parse()
|
||||
// return strings.Replace(template, "{{content}}", userHtml, -1), nil
|
||||
// }
|
||||
func RenderUserList(repo Repository) (string, error) {
|
||||
baseTemplate, _ := repo.Template()
|
||||
users, _ := repo.Users()
|
||||
t, err := template.New("user_list").Parse(userListTemplateStr)
|
||||
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
|
||||
|
||||
}
|
||||
|
|
|
@ -2,6 +2,8 @@ package kiss_test
|
|||
|
||||
import (
|
||||
"h4kor/kiss-social"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
@ -49,3 +51,34 @@ func TestCanRenderIndexPage(t *testing.T) {
|
|||
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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"embed"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
)
|
||||
|
@ -11,7 +12,7 @@ import (
|
|||
//go:embed embed/initial/base.html
|
||||
var base_template string
|
||||
|
||||
//go:embed embed/initial/static/*
|
||||
//go:embed embed/*
|
||||
var static_files embed.FS
|
||||
|
||||
var VERSION = "0.0.1"
|
||||
|
@ -40,6 +41,10 @@ func CreateRepository(name string) (Repository, error) {
|
|||
src_data, _ := static_files.ReadFile(file.Name())
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -66,6 +71,16 @@ func (repo Repository) UsersDir() string {
|
|||
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) {
|
||||
userNames := listDir(repo.UsersDir())
|
||||
users := make([]User, len(userNames))
|
||||
|
|
|
@ -157,3 +157,24 @@ func TestNewRepoGetsStaticFiles(t *testing.T) {
|
|||
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")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue