llist/util.go -rw-r--r-- 3.7 KiB View raw
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package main

import (
	"database/sql"
	"fmt"
	"net/url"
	"strings"
	"time"
)

func (e Link) addLink() error {
	if u, err := url.Parse(e.Url); err != nil {
		return err
	} else if !u.IsAbs() {
		return fmt.Errorf("URL not absolute")
	}

	r, err := db.Exec(IN_LINK, e.Title, e.Url, e.Name)
	if err != nil {
		return err
	}

	id, err := r.LastInsertId()
	if err != nil {
		return err
	}

	tx, err := db.Begin()
	if err != nil {
		return err
	}
	for _, t := range e.Tags {
		T := strings.TrimSpace(t)
		if T == "" {
			continue
		}
		_, err := tx.Exec(IN_TAG, T, id)
		if err != nil {
			return err
		}
	}
	tx.Commit()
	return err
}

func (c Comment) addComment(pid, cid int) (int64, error) {
	r, err := db.Exec(IN_COMM, pid, cid, c.Name, c.Text)
	if err != nil {
		return -1, nil
	}
	return r.LastInsertId()
}

func queryLink(pid int) (Link, error) {
	row := db.QueryRow(SE_LINK, pid)
	var l Link
	err := row.Scan(&l.Id, &l.Title, &l.Url, &l.Posted, &l.Name, &l.CCount)

	l.Comments, err = queryComments(pid, -1)
	if err != nil {
		return Link{}, err
	}

	return l, err
}

func queryLinks(page int, tag string) ([]Link, error) {
	links := make([]Link, 0, ITEMS+1)

	var (
		err  error
		rows *sql.Rows
	)
	if tag == "" {
		rows, err = db.Query(SE_LINKS, ITEMS+1, (page-1)*ITEMS)
	} else {
		rows, err = db.Query(SE_BY_TAG, tag, ITEMS+1, (page-1)*ITEMS)
	}
	if err != nil {
		return nil, err
	}
	defer rows.Close()

	for rows.Next() {
		var l Link
		rows.Scan(&l.Id, &l.Title, &l.Url, &l.Posted, &l.Name, &l.CCount)

		trows, err := db.Query(SE_TAGS, l.Id)
		if err != nil {
			return nil, err
		}
		var tag string
		for trows.Next() {
			trows.Scan(&tag)
			l.Tags = append(l.Tags, tag)
		}

		links = append(links, l)
	}

	return links, rows.Err()
}

func queryComments(postId, replyId int) ([]Comment, error) {
	var comments []Comment

	var err error
	var rows *sql.Rows
	rows, err = db.Query(SE_COMM, postId, replyId)
	if err != nil {
		return nil, err
	}
	defer rows.Close()

	var id int
	var name, content string
	var posted time.Time
	for rows.Next() {
		rows.Scan(&id, &posted, &name, &content)
		comments = append(comments, Comment{
			Id:     id,
			Posted: posted,
			Name:   name,
			Text:   content,
		})
	}

	for i := range comments {
		comments[i].Replies, err = queryComments(postId, comments[i].Id)
		if err != nil {
			return nil, err
		}
	}

	return comments, rows.Err()
}

func queryUser(name string, lpage, cpage int) ([]Link, []Comment, error) {
	links := make([]Link, 0, UITEMS+1)
	comms := make([]Comment, 0, UITEMS+1)

	rows, err := db.Query(SE_LINK_BY_USER, name, UITEMS+1, (lpage-1)*UITEMS)
	if err != nil {
		return nil, nil, err
	}
	for rows.Next() {
		var (
			id, count  int
			title, url string
			posted     time.Time
		)
		rows.Scan(&id, &title, &url, &posted, &count)
		links = append(links, Link{
			Id:     id,
			Posted: posted,
			Name:   name,
			CCount: count,
			Title:  title,
			Url:    url,
		})
	}

	rows, err = db.Query(SE_COMM_BY_USER, name, UITEMS+1, (cpage-1)*UITEMS)
	if err != nil {
		return nil, nil, err
	}
	for rows.Next() {
		var (
			id, link int
			content  string
			posted   time.Time
		)
		rows.Scan(&id, &link, &content, &posted)
		comms = append(comms, Comment{
			Id:       id,
			Name:     name,
			Posted:   posted,
			Text:     content,
			Response: link,
		})
	}

	return links, comms, nil
}

func queryTags() ([]string, error) {
	var tags []string

	rows, err := db.Query(SE_TAG_BY_OCC)
	if err != nil {
		return nil, err
	}
	var tag string
	for rows.Next() {
		rows.Scan(&tag)
		tags = append(tags, tag)
	}

	return tags, rows.Err()
}

func queryTitle(id int) (string, error) {
	title, ok := titles[id]
	if ok {
		return title, nil
	}

	row := db.QueryRow(SE_TITLE, id)
	if err := row.Scan(&title); err != nil {
		return "", err
	}
	return title, nil
}