-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathcomment.v
61 lines (53 loc) · 1.74 KB
/
comment.v
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
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by a GPL license that can be found in the LICENSE file.
module main
import veb
import validation
import time
struct Comment {
mut:
id int @[primary; sql: serial]
author_id int
issue_id int
created_at int
text string
}
@['/:username/:repo_name/comments'; post]
pub fn (mut app App) handle_add_comment(username string, repo_name string) veb.Result {
app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() }
text := ctx.form['text']
issue_id := ctx.form['issue_id']
is_text_empty := validation.is_string_empty(text)
is_issue_id_empty := validation.is_string_empty(issue_id)
if is_text_empty || is_issue_id_empty || !ctx.logged_in {
ctx.error('Issue comment is not valid')
return app.issue(mut ctx, username, repo_name, issue_id)
}
app.add_issue_comment(ctx.user.id, issue_id.int(), text) or {
ctx.error('There was an error while inserting the comment')
return app.issue(mut ctx, username, repo_name, issue_id)
}
// TODO: count comments
app.increment_issue_comments(issue_id.int()) or { app.info(err.str()) }
return ctx.redirect('/${username}/${repo_name}/issue/${issue_id}')
}
fn (mut app App) add_issue_comment(author_id int, issue_id int, text string) ! {
comment := Comment{
author_id: author_id
issue_id: issue_id
created_at: int(time.now().unix())
text: text
}
sql app.db {
insert comment into Comment
}!
}
fn (mut app App) get_all_issue_comments(issue_id int) []Comment {
comments := sql app.db {
select from Comment where issue_id == issue_id
} or { []Comment{} }
return comments
}
fn (c Comment) relative() string {
return time.unix(c.created_at).relative()
}