listing users and more refactoring

This commit is contained in:
Niko Abeler 2022-07-20 19:35:31 +02:00
parent 2faa95f753
commit c45cb3eb00
5 changed files with 126 additions and 74 deletions

View File

@ -6,3 +6,10 @@ func dirExists(path string) bool {
_, err := os.Stat(path) _, err := os.Stat(path)
return err == nil return err == nil
} }
func listDir(path string) []string {
dir, _ := os.Open(path)
defer dir.Close()
files, _ := dir.Readdirnames(-1)
return files
}

View File

@ -4,23 +4,12 @@ import (
"fmt" "fmt"
"os" "os"
"path" "path"
"time"
) )
type Repository struct { type Repository struct {
name string name string
} }
type User struct {
repo Repository
name string
}
type Post struct {
user User
title string
}
func CreateRepository(name string) (Repository, error) { func CreateRepository(name string) (Repository, error) {
newRepo := Repository{name: name} newRepo := Repository{name: name}
// check if repository already exists // check if repository already exists
@ -29,6 +18,7 @@ func CreateRepository(name string) (Repository, error) {
} }
os.Mkdir(name, 0755) os.Mkdir(name, 0755)
os.Mkdir(path.Join(name, "users"), 0755)
return newRepo, nil return newRepo, nil
} }
@ -36,15 +26,16 @@ func (repo Repository) Dir() string {
return repo.name return repo.name
} }
func (user User) Dir() string { func (repo Repository) Users() ([]User, error) {
return path.Join(user.repo.Dir(), user.name) userNames := listDir(path.Join(repo.Dir(), "users"))
users := make([]User, len(userNames))
for i, name := range userNames {
users[i] = User{repo: repo, name: name}
}
return users, nil
} }
func PostDir(post Post) string { func (repo Repository) CreateUser(name string) (User, error) {
return path.Join(post.user.Dir(), "public", post.title)
}
func CreateNewUser(repo Repository, name string) (User, error) {
new_user := User{repo: repo, name: name} new_user := User{repo: repo, name: name}
// check if user already exists // check if user already exists
if dirExists(new_user.Dir()) { if dirExists(new_user.Dir()) {
@ -64,26 +55,3 @@ func CreateNewUser(repo Repository, name string) (User, error) {
return new_user, nil return new_user, nil
} }
func CreateNewPost(user User, title string) {
timestamp := time.Now().UTC().Unix()
folder_name := fmt.Sprintf("%d-%s", timestamp, title)
post_dir := path.Join(user.Dir(), "public", folder_name)
// if post already exists, add -n to the end of the name
i := 0
for {
if dirExists(post_dir) {
i++
folder_name = fmt.Sprintf("%d-%s-%d", timestamp, title, i)
post_dir = path.Join(user.Dir(), "public", folder_name)
} else {
break
}
}
initial_content := "# " + title
// create post file
os.Mkdir(post_dir, 0755)
os.WriteFile(path.Join(post_dir, "index.md"), []byte(initial_content), 0644)
}

View File

@ -1,9 +1,7 @@
package kiss_test package kiss_test
import ( import (
"fmt"
"h4kor/kiss-social" "h4kor/kiss-social"
"io/ioutil"
"math/rand" "math/rand"
"os" "os"
"path" "path"
@ -50,7 +48,7 @@ func TestCannotCreateExistingRepository(t *testing.T) {
func TestCanCreateANewUser(t *testing.T) { func TestCanCreateANewUser(t *testing.T) {
// Create a new user // Create a new user
repo, _ := kiss.CreateRepository(testRepoName()) repo, _ := kiss.CreateRepository(testRepoName())
user, _ := kiss.CreateNewUser(repo, randomUserName()) user, _ := repo.CreateUser(randomUserName())
if _, err := os.Stat(path.Join(user.Dir(), "")); err != nil { if _, err := os.Stat(path.Join(user.Dir(), "")); err != nil {
t.Error("User directory not created") t.Error("User directory not created")
} }
@ -60,8 +58,8 @@ func TestCannotRecreateExisitingUser(t *testing.T) {
// Create a new user // Create a new user
repo, _ := kiss.CreateRepository(testRepoName()) repo, _ := kiss.CreateRepository(testRepoName())
userName := randomUserName() userName := randomUserName()
kiss.CreateNewUser(repo, userName) repo.CreateUser(userName)
_, err := kiss.CreateNewUser(repo, userName) _, err := repo.CreateUser(userName)
if err == nil { if err == nil {
t.Error("No error returned when creating existing user") t.Error("No error returned when creating existing user")
} }
@ -70,7 +68,7 @@ func TestCannotRecreateExisitingUser(t *testing.T) {
func TestCreateUserAddsVersionFile(t *testing.T) { func TestCreateUserAddsVersionFile(t *testing.T) {
// Create a new user // Create a new user
repo, _ := kiss.CreateRepository(testRepoName()) repo, _ := kiss.CreateRepository(testRepoName())
user, _ := kiss.CreateNewUser(repo, randomUserName()) user, _ := repo.CreateUser(randomUserName())
if _, err := os.Stat(path.Join(user.Dir(), "/meta/VERSION")); err != nil { if _, err := os.Stat(path.Join(user.Dir(), "/meta/VERSION")); err != nil {
t.Error("Version file not created") t.Error("Version file not created")
} }
@ -79,7 +77,7 @@ func TestCreateUserAddsVersionFile(t *testing.T) {
func TestCreateUserAddsBaseHtmlFile(t *testing.T) { func TestCreateUserAddsBaseHtmlFile(t *testing.T) {
// Create a new user // Create a new user
repo, _ := kiss.CreateRepository(testRepoName()) repo, _ := kiss.CreateRepository(testRepoName())
user, _ := kiss.CreateNewUser(repo, randomUserName()) user, _ := repo.CreateUser(randomUserName())
if _, err := os.Stat(path.Join(user.Dir(), "/meta/base.html")); err != nil { if _, err := os.Stat(path.Join(user.Dir(), "/meta/base.html")); err != nil {
t.Error("Base html file not created") t.Error("Base html file not created")
} }
@ -88,40 +86,25 @@ func TestCreateUserAddsBaseHtmlFile(t *testing.T) {
func TestCreateUserAddsPublicFolder(t *testing.T) { func TestCreateUserAddsPublicFolder(t *testing.T) {
// Create a new user // Create a new user
repo, _ := kiss.CreateRepository(testRepoName()) repo, _ := kiss.CreateRepository(testRepoName())
user, _ := kiss.CreateNewUser(repo, randomUserName()) user, _ := repo.CreateUser(randomUserName())
if _, err := os.Stat(path.Join(user.Dir(), "/public")); err != nil { if _, err := os.Stat(path.Join(user.Dir(), "/public")); err != nil {
t.Error("Public folder not created") t.Error("Public folder not created")
} }
} }
func TestCreateNewPostCreatesEntryInPublic(t *testing.T) { func CanListRepoUsers(t *testing.T) {
// Create a new user // Create a new user
repo, _ := kiss.CreateRepository(testRepoName()) repo, _ := kiss.CreateRepository(testRepoName())
user, _ := kiss.CreateNewUser(repo, randomUserName()) user1, _ := repo.CreateUser(randomUserName())
user2, _ := repo.CreateUser(randomUserName())
// Create a new post // Create a new post
kiss.CreateNewPost(user, "testpost") users, _ := repo.Users()
files, err := ioutil.ReadDir(path.Join(user.Dir(), "public")) if len(users) == 2 {
if err != nil { t.Error("No users found")
t.Error("Error reading directory")
} }
if len(files) < 1 { for _, user := range users {
t.Error("Post not created") if user.Name() == user1.Name() || user.Name() == user2.Name() {
} t.Error("User found")
} }
func TestCreateNewPostMultipleCalls(t *testing.T) {
// Create a new user
repo, _ := kiss.CreateRepository(testRepoName())
user, _ := kiss.CreateNewUser(repo, randomUserName())
// Create a new post
kiss.CreateNewPost(user, "testpost")
kiss.CreateNewPost(user, "testpost")
kiss.CreateNewPost(user, "testpost")
files, err := ioutil.ReadDir(path.Join(user.Dir(), "public"))
if err != nil {
t.Error("Error reading directory")
}
if len(files) < 3 {
t.Error(fmt.Sprintf("Only %d posts created", len(files)))
} }
} }

53
user.go Normal file
View File

@ -0,0 +1,53 @@
package kiss
import (
"fmt"
"os"
"path"
"time"
)
type User struct {
repo Repository
name string
}
type Post struct {
user User
title string
}
func (user User) Dir() string {
return path.Join(user.repo.Dir(), "users", user.name)
}
func (user User) Name() string {
return user.name
}
func CreateNewPost(user User, title string) {
timestamp := time.Now().UTC().Unix()
folder_name := fmt.Sprintf("%d-%s", timestamp, title)
post_dir := path.Join(user.Dir(), "public", folder_name)
// if post already exists, add -n to the end of the name
i := 0
for {
if dirExists(post_dir) {
i++
folder_name = fmt.Sprintf("%d-%s-%d", timestamp, title, i)
post_dir = path.Join(user.Dir(), "public", folder_name)
} else {
break
}
}
initial_content := "# " + title
// create post file
os.Mkdir(post_dir, 0755)
os.WriteFile(path.Join(post_dir, "index.md"), []byte(initial_content), 0644)
}
func PostDir(post Post) string {
return path.Join(post.user.Dir(), "public", post.title)
}

41
user_test.go Normal file
View File

@ -0,0 +1,41 @@
package kiss_test
import (
"fmt"
"h4kor/kiss-social"
"io/ioutil"
"path"
"testing"
)
func TestCreateNewPostCreatesEntryInPublic(t *testing.T) {
// Create a new user
repo, _ := kiss.CreateRepository(testRepoName())
user, _ := repo.CreateUser(randomUserName())
// Create a new post
kiss.CreateNewPost(user, "testpost")
files, err := ioutil.ReadDir(path.Join(user.Dir(), "public"))
if err != nil {
t.Error("Error reading directory")
}
if len(files) < 1 {
t.Error("Post not created")
}
}
func TestCreateNewPostMultipleCalls(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")
files, err := ioutil.ReadDir(path.Join(user.Dir(), "public"))
if err != nil {
t.Error("Error reading directory")
}
if len(files) < 3 {
t.Error(fmt.Sprintf("Only %d posts created", len(files)))
}
}