fixed aliases

This commit is contained in:
Niko Abeler 2022-08-21 12:27:28 +02:00
parent b8ce1ab774
commit 96d0a1e98b
2 changed files with 50 additions and 5 deletions

View File

@ -274,3 +274,48 @@ func TestCanGetMapWithAllPostAliases(t *testing.T) {
}
}
func TestAliasesHaveCorrectPost(t *testing.T) {
repo, _ := owl.CreateRepository(testRepoName())
user, _ := repo.CreateUser(randomUserName())
post1, _ := user.CreateNewPost("test-1")
post2, _ := user.CreateNewPost("test-2")
content := "---\n"
content += "title: Test\n"
content += "aliases: \n"
content += " - /foo/1\n"
content += "---\n"
content += "This is a test"
os.WriteFile(post1.ContentFile(), []byte(content), 0644)
content = "---\n"
content += "title: Test\n"
content += "aliases: \n"
content += " - /foo/2\n"
content += "---\n"
content += "This is a test"
os.WriteFile(post2.ContentFile(), []byte(content), 0644)
posts, _ := user.Posts()
if len(posts) != 2 {
t.Error("Wrong number of posts returned, expected 1, got ", len(posts))
}
var aliases map[string]*owl.Post
aliases, err := repo.PostAliases()
if err != nil {
t.Error("Error getting post aliases: ", err.Error())
}
if len(aliases) != 2 {
t.Error("Wrong number of aliases returned, expected 2, got ", len(aliases))
t.Error("Aliases: ", aliases)
}
if aliases["/foo/1"].Id() != post1.Id() {
t.Error("Alias '/foo/1' points to wrong post: ", aliases["/foo/1"].Id())
}
if aliases["/foo/2"].Id() != post2.Id() {
t.Error("Alias '/foo/2' points to wrong post: ", aliases["/foo/2"].Id())
}
}

10
user.go
View File

@ -52,15 +52,15 @@ func (user User) Name() string {
return user.name
}
func (user User) Posts() ([]Post, error) {
func (user User) Posts() ([]*Post, error) {
postFiles := listDir(path.Join(user.Dir(), "public"))
posts := make([]Post, 0)
posts := make([]*Post, 0)
for _, id := range postFiles {
// if is a directory and has index.md, add to posts
if dirExists(path.Join(user.Dir(), "public", id)) {
if fileExists(path.Join(user.Dir(), "public", id, "index.md")) {
post, _ := user.GetPost(id)
posts = append(posts, post)
posts = append(posts, &post)
}
}
}
@ -74,7 +74,7 @@ func (user User) Posts() ([]Post, error) {
}
type PostWithDate struct {
post Post
post *Post
date time.Time
}
@ -197,7 +197,7 @@ func (user User) PostAliases() (map[string]*Post, error) {
return post_aliases, err
}
for _, alias := range post.Aliases() {
post_aliases[alias] = &post
post_aliases[alias] = post
}
}
return post_aliases, nil