Repository navigation
Expand file tree
/
Copy pathpost.v
More file actions
98 lines (88 loc) · 2.29 KB
/
Copy pathpost.v
File metadata and controls
98 lines (88 loc) · 2.29 KB
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
module main
import time
struct Post {
id string
title string
text string
url string
nr_comments int
time string
}
struct Comment {
name string
text string
time string
}
fn (app mut App) find_all_posts() []Post {
rows := app.db.exec('
select id, title, extract(epoch from time)::int, extract(epoch from last_reply)::int, nr_comments
from posts
order by last_reply desc')
if rows.len == 0 {
app.cur_user.name = 'admin'
app.insert_post('Hello world!', 'Hello world from vorum ;)')
return [Post{title:'Hello world!', text: 'Hello world from vorum ;)'}]
}
mut posts := []Post
for row in rows {
id := row.vals[0]
title := row.vals[1]
posts << Post {
id: id
url : '/post/$id/' + clean_url(title)
title: title
nr_comments: row.vals[4].int()
time: time.unix(row.vals[3].int()).clean()
}
}
return posts
}
fn (app App) find_comments(post_id int) []Comment {
rows := app.db.exec('select * from comments where post_id=$post_id order by id')
mut comments := []Comment
for row in rows {
text := row.vals[2].replace('\n', '<br>')
name := row.vals[3]
comments << Comment {
name: name
text: text
time: time.unix(row.vals[5].int()).clean()
}
}
return comments
}
fn (app App) retrieve_post(id int) ?Post {
rows := app.db.exec('select title, text, is_blog from posts where id=$id')
if rows.len == 0 {
return error('no posts')
}
row := rows[0]
post := Post {
title: row.vals[0]
text: row.vals[1]
id: id.str()
}
return post
}
fn (app & App) insert_comment(post_id int, comment Comment) bool {
app.db.exec_param2('
insert into comments
(name, text, post_id)
values
($1,$2, \'$post_id\') ', comment.name, comment.text)
app.db.exec_param('
update posts
set nr_comments=nr_comments+1, last_reply=now()::timestamp
where id=$1', post_id.str())
return true // TODO vweb $ hack
}
fn (app & App) insert_post(title, text string) bool {
app.db.exec_param2('insert into posts (title, text) values ($1,$2)', title, '')
post_id := app.db.q_int('select id from posts order by id desc limit 1')
app.db.exec_param2('insert into comments (name, text, post_id) values ($1, $2, \'$post_id\') ',
app.cur_user.name, text)
return true // TODO vweb $ hack
}
fn clean_url(s string) string {
return s.replace(' ', '-').to_lower()
}