listing users and more refactoring
This commit is contained in:
parent
2faa95f753
commit
c45cb3eb00
|
@ -6,3 +6,10 @@ func dirExists(path string) bool {
|
|||
_, err := os.Stat(path)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func listDir(path string) []string {
|
||||
dir, _ := os.Open(path)
|
||||
defer dir.Close()
|
||||
files, _ := dir.Readdirnames(-1)
|
||||
return files
|
||||
}
|
||||
|
|
|
@ -4,23 +4,12 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Repository struct {
|
||||
name string
|
||||
}
|
||||
|
||||
type User struct {
|
||||
repo Repository
|
||||
name string
|
||||
}
|
||||
|
||||
type Post struct {
|
||||
user User
|
||||
title string
|
||||
}
|
||||
|
||||
func CreateRepository(name string) (Repository, error) {
|
||||
newRepo := Repository{name: name}
|
||||
// check if repository already exists
|
||||
|
@ -29,6 +18,7 @@ func CreateRepository(name string) (Repository, error) {
|
|||
}
|
||||
|
||||
os.Mkdir(name, 0755)
|
||||
os.Mkdir(path.Join(name, "users"), 0755)
|
||||
return newRepo, nil
|
||||
}
|
||||
|
||||
|
@ -36,15 +26,16 @@ func (repo Repository) Dir() string {
|
|||
return repo.name
|
||||
}
|
||||
|
||||
func (user User) Dir() string {
|
||||
return path.Join(user.repo.Dir(), user.name)
|
||||
func (repo Repository) Users() ([]User, error) {
|
||||
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 {
|
||||
return path.Join(post.user.Dir(), "public", post.title)
|
||||
}
|
||||
|
||||
func CreateNewUser(repo Repository, name string) (User, error) {
|
||||
func (repo Repository) CreateUser(name string) (User, error) {
|
||||
new_user := User{repo: repo, name: name}
|
||||
// check if user already exists
|
||||
if dirExists(new_user.Dir()) {
|
||||
|
@ -64,26 +55,3 @@ func CreateNewUser(repo Repository, name string) (User, error) {
|
|||
|
||||
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)
|
||||
}
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
package kiss_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"h4kor/kiss-social"
|
||||
"io/ioutil"
|
||||
"math/rand"
|
||||
"os"
|
||||
"path"
|
||||
|
@ -50,7 +48,7 @@ func TestCannotCreateExistingRepository(t *testing.T) {
|
|||
func TestCanCreateANewUser(t *testing.T) {
|
||||
// Create a new user
|
||||
repo, _ := kiss.CreateRepository(testRepoName())
|
||||
user, _ := kiss.CreateNewUser(repo, randomUserName())
|
||||
user, _ := repo.CreateUser(randomUserName())
|
||||
if _, err := os.Stat(path.Join(user.Dir(), "")); err != nil {
|
||||
t.Error("User directory not created")
|
||||
}
|
||||
|
@ -60,8 +58,8 @@ func TestCannotRecreateExisitingUser(t *testing.T) {
|
|||
// Create a new user
|
||||
repo, _ := kiss.CreateRepository(testRepoName())
|
||||
userName := randomUserName()
|
||||
kiss.CreateNewUser(repo, userName)
|
||||
_, err := kiss.CreateNewUser(repo, userName)
|
||||
repo.CreateUser(userName)
|
||||
_, err := repo.CreateUser(userName)
|
||||
if err == nil {
|
||||
t.Error("No error returned when creating existing user")
|
||||
}
|
||||
|
@ -70,7 +68,7 @@ func TestCannotRecreateExisitingUser(t *testing.T) {
|
|||
func TestCreateUserAddsVersionFile(t *testing.T) {
|
||||
// Create a new user
|
||||
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 {
|
||||
t.Error("Version file not created")
|
||||
}
|
||||
|
@ -79,7 +77,7 @@ func TestCreateUserAddsVersionFile(t *testing.T) {
|
|||
func TestCreateUserAddsBaseHtmlFile(t *testing.T) {
|
||||
// Create a new user
|
||||
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 {
|
||||
t.Error("Base html file not created")
|
||||
}
|
||||
|
@ -88,40 +86,25 @@ func TestCreateUserAddsBaseHtmlFile(t *testing.T) {
|
|||
func TestCreateUserAddsPublicFolder(t *testing.T) {
|
||||
// Create a new user
|
||||
repo, _ := kiss.CreateRepository(testRepoName())
|
||||
user, _ := kiss.CreateNewUser(repo, randomUserName())
|
||||
user, _ := repo.CreateUser(randomUserName())
|
||||
if _, err := os.Stat(path.Join(user.Dir(), "/public")); err != nil {
|
||||
t.Error("Public folder not created")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateNewPostCreatesEntryInPublic(t *testing.T) {
|
||||
func CanListRepoUsers(t *testing.T) {
|
||||
// Create a new user
|
||||
repo, _ := kiss.CreateRepository(testRepoName())
|
||||
user, _ := kiss.CreateNewUser(repo, randomUserName())
|
||||
user1, _ := repo.CreateUser(randomUserName())
|
||||
user2, _ := 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")
|
||||
users, _ := repo.Users()
|
||||
if len(users) == 2 {
|
||||
t.Error("No users found")
|
||||
}
|
||||
for _, user := range users {
|
||||
if user.Name() == user1.Name() || user.Name() == user2.Name() {
|
||||
t.Error("User found")
|
||||
}
|
||||
if len(files) < 1 {
|
||||
t.Error("Post not created")
|
||||
}
|
||||
}
|
||||
|
||||
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)))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
|
@ -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)))
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue