forum.gno

1.06 Kb ยท 64 lines
 1package storage
 2
 3import (
 4	"strconv"
 5
 6	"gno.land/p/nt/avl"
 7)
 8
 9func init() {
10	// we write to three common data structure for persistence
11	// avl.Tree, map and slice.
12	posts0 := avl.NewTree()
13	b0 := &TreeBoard{0, posts0}
14	boards.Set(strconv.Itoa(0), b0)
15
16	posts1 := make(map[int]Post)
17	b1 := &MapBoard{1, posts1}
18	boards.Set(strconv.Itoa(1), b1)
19
20	posts2 := []Post{}
21	b2 := &SliceBoard{2, posts2}
22	boards.Set(strconv.Itoa(2), b2)
23}
24
25// post to all boards.
26func AddPost(title, content string) {
27	for i := 0; i < boards.Size(); i++ {
28		boardId := strconv.Itoa(i)
29		b, ok := boards.Get(boardId)
30		if ok {
31			b.(Board).AddPost(title, content)
32		}
33	}
34}
35
36func GetPost(boardId, postId int) string {
37	b, ok := boards.Get(strconv.Itoa(boardId))
38	var res string
39
40	if ok {
41		p, ok := b.(Board).GetPost(postId)
42		if ok {
43			res = p.title + "," + p.content
44		}
45	}
46	return res
47}
48
49func GetPostSize(boardId int) int {
50	b, ok := boards.Get(strconv.Itoa(boardId))
51	var res int
52
53	if ok {
54		res = b.(Board).Size()
55	} else {
56		res = -1
57	}
58
59	return res
60}
61
62func GetBoardSize() int {
63	return boards.Size()
64}