list user posts

This commit is contained in:
Niko Abeler 2022-07-20 21:12:18 +02:00
parent c45cb3eb00
commit 407b4ce7a2
2 changed files with 26 additions and 0 deletions

View File

@ -25,6 +25,15 @@ func (user User) Name() string {
return user.name
}
func (user User) Posts() ([]Post, error) {
postNames := listDir(path.Join(user.Dir(), "public"))
posts := make([]Post, len(postNames))
for i, name := range postNames {
posts[i] = Post{user: user, title: name}
}
return posts, nil
}
func CreateNewPost(user User, title string) {
timestamp := time.Now().UTC().Unix()
folder_name := fmt.Sprintf("%d-%s", timestamp, title)

View File

@ -39,3 +39,20 @@ func TestCreateNewPostMultipleCalls(t *testing.T) {
t.Error(fmt.Sprintf("Only %d posts created", len(files)))
}
}
func TestCanListUserPosts(t *testing.T) {
// Create a new user
repo, _ := kiss.CreateRepository(testRepoName())
user, _ := repo.CreateUser(randomUserName())
// Create a new post
kiss.CreateNewPost(user, "testpost")
kiss.CreateNewPost(user, "testpost")
kiss.CreateNewPost(user, "testpost")
posts, err := user.Posts()
if err != nil {
t.Error("Error reading posts")
}
if len(posts) != 3 {
t.Error("No posts found")
}
}