Skip to content

Commit c125657

Browse files
committed
lots of fixes
1 parent 2017daa commit c125657

File tree

3 files changed

+77
-59
lines changed

3 files changed

+77
-59
lines changed

main.v

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ pub mut:
2121
cur_user User
2222
}
2323

24+
pub fn (app mut App) reset() {
25+
app.cur_user = User{}
26+
}
27+
28+
2429
fn main() {
2530
println('Running vorum on http://localhost:$port')
2631
mut app := App{}
@@ -41,32 +46,35 @@ pub fn (app mut App) index() {
4146

4247
// TODO ['/post/:id/:title']
4348
// TODO `fn (app App) post(id int)`
44-
pub fn (app &App) post() {
49+
pub fn (app mut App) post() {
4550
id := app.get_post_id()
4651
post := app.retrieve_post(id) or {
4752
app.vweb.redirect('/')
4853
return
4954
}
55+
app.auth()
5056
comments := app.find_comments(id)
51-
show_form := true
57+
show_form := app.cur_user.name != ''
5258
$vweb.html()
5359
}
5460

5561
// new post
56-
pub fn (app &App) new() {
62+
pub fn (app mut App) new() {
63+
app.auth()
64+
logged_in := app.cur_user.name != ''
5765
$vweb.html()
5866
}
5967

6068
// [post]
61-
pub fn (app & App) new_post() {
69+
pub fn (app mut App) new_post() {
70+
app.auth()
6271
mut name := ''
6372
if app.cur_user.name != '' {
6473
name = app.cur_user.name
65-
}
66-
else {
74+
} else {
6775
// not logged in
68-
//return
69-
name = 'admin' // TODO remove
76+
app.vweb.redirect('/new')
77+
return
7078
}
7179
title := app.vweb.form['title']
7280
mut text := app.vweb.form['text']
@@ -83,16 +91,17 @@ pub fn (app & App) new_post() {
8391
}
8492

8593
// [post]
86-
fn (app & App) comment() {
94+
fn (app mut App) comment() {
95+
app.auth()
8796
post_id := app.get_post_id()
8897
mut name := ''// b.form['name']
8998
if app.cur_user.name != '' {
9099
name = app.cur_user.name
91100
}
92101
else {
93102
// not logged in
94-
//return
95-
name = 'admin' // TODO remove
103+
app.vweb.redirect('/')
104+
return
96105
}
97106
mut comment_text := app.vweb.form['text']
98107
if name == '' || comment_text == '' {

new.html

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
@header
2-
3-
<div class='center'>
4-
<form action='/new_post' method='post'>
5-
<input type=text required minlength=4 name=title placeholder='Title'>
6-
<textarea name='text' required minlength=10 placeholder='Write your post here'></textarea>
7-
<input type='submit' value='Post'>
8-
</form>
9-
</div>
10-
11-
<div class='sep' style='border:0'></div>
12-
13-
<div class='center'>
2+
3+
<div class='sep' style='border:0'></div>
4+
5+
@if logged_in
6+
<div class='center'>
7+
<form action='/new_post' method='post'>
8+
<input type=text required minlength=4 name=title placeholder='Title'>
9+
<textarea name='text' required minlength=10 placeholder='Write your post here'></textarea>
10+
<input type='submit' value='Post'>
11+
</form>
12+
</div>
13+
14+
@else
15+
16+
<div class='center'>
1417
<a href='https://github.com/login/oauth/authorize?response_type=code&client_id=@CLIENT_ID'
15-
>Log in via GitHub to create a new topic</a>
16-
</div>
18+
>Log in via GitHub to create a new topic</a>
19+
</div>
20+
21+
@end
1722

18-
<br>
19-
<br>
20-
<br>
21-
<br>
23+
<br>
24+
<br>
25+
<br>
26+
<br>
2227

2328

2429

post.html

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,54 @@
11
@header
22

3-
<h1>@post.title</h1>
3+
<h1>@post.title</h1>
44

5-
<div class="sep" style="border:0"></div>
5+
<div class="sep" style="border:0"></div>
66

7-
<div class="center">
7+
<div class="center">
88

99
@for comment in comments
1010
<div class=comment>
1111
<b>@comment.name</b> <i>@comment.time</i>
1212
<div class=text>@comment.text</div>
13-
</div>
14-
@end
13+
</div>
14+
@end
1515

1616

17-
@if show_form
18-
<form method="post" action="/comment/@post.id/title">
19-
<textarea name="text" required minlength=3
20-
placeholder="Write your comment here"></textarea>
21-
<input type="submit" value="Post">
22-
</form>
23-
@end
17+
@if show_form
18+
<form method="post" action="/comment/@post.id/title">
19+
<textarea name="text" required minlength=3
20+
placeholder="Write your comment here"></textarea>
21+
<input type="submit" value="Post">
22+
</form>
23+
@end
2424

2525

26-
</div>
26+
</div>
2727

28-
<div class="sep" style="border:0;"></div>
28+
<div class="sep" style="border:0;"></div>
2929

30-
<div class="center">
30+
@if !show_form
31+
32+
<div class="center">
3133
<a href="https://github.com/login/oauth/authorize?response_type=code&client_id=@CLIENT_ID"
32-
>Log in via GitHub to comment</a>
33-
</div>
34-
35-
<div class="center">
36-
<!-- beautiful layout -->
37-
<br>
38-
<br>
39-
<br>
40-
34+
>Log in via GitHub to comment</a>
35+
</div>
36+
37+
@end
38+
39+
<div class="center">
40+
<!-- beautiful layout -->
41+
<br>
42+
<br>
43+
<br>
44+
4145
<center style="color:#777">
42-
Powered by <a target=_blank href="https://github.com/vlang/vorum">vorum</a>,
46+
Powered by <a target=_blank href="https://github.com/vlang/vorum">vorum</a>,
4347
open-source blogging/forum software written in V
44-
</center>
45-
46-
<br>
47-
<br>
48-
</div>
48+
</center>
49+
50+
<br>
51+
<br>
52+
</div>
4953

5054

0 commit comments

Comments
 (0)