Skip to content

Commit 0f14d71

Browse files
committed
simplify comment.v
1 parent 92bb280 commit 0f14d71

File tree

3 files changed

+37
-49
lines changed

3 files changed

+37
-49
lines changed
Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,64 @@
1+
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
2+
// Use of this source code is governed by a GPL license that can be found in the LICENSE file.
13
module main
24

35
import vweb
46
import validation
7+
import time
8+
9+
struct Comment {
10+
mut:
11+
id int [primary; sql: serial]
12+
author_id int
13+
issue_id int
14+
created_at int
15+
text string
16+
}
517

618
['/:username/:repo_name/comments'; post]
719
pub fn (mut app App) handle_add_comment(username string, repo_name string) vweb.Result {
820
repo := app.find_repo_by_name_and_username(repo_name, username)
9-
1021
if repo.id == 0 {
1122
return app.not_found()
1223
}
13-
1424
text := app.form['text']
1525
issue_id := app.form['issue_id']
16-
1726
is_text_empty := validation.is_string_empty(text)
1827
is_issue_id_empty := validation.is_string_empty(issue_id)
19-
2028
if is_text_empty || is_issue_id_empty || !app.logged_in {
2129
app.error('Issue comment is not valid')
22-
2330
return app.issue(username, repo_name, issue_id)
2431
}
25-
2632
app.add_issue_comment(app.user.id, issue_id.int(), text) or {
2733
app.error('There was an error while inserting the comment')
28-
2934
return app.issue(username, repo_name, issue_id)
3035
}
31-
3236
// TODO: count comments
3337
app.increment_issue_comments(issue_id.int()) or { app.info(err.str()) }
34-
3538
return app.redirect('/${username}/${repo_name}/issue/${issue_id}')
3639
}
40+
41+
fn (mut app App) add_issue_comment(author_id int, issue_id int, text string) ! {
42+
comment := Comment{
43+
author_id: author_id
44+
issue_id: issue_id
45+
created_at: int(time.now().unix)
46+
text: text
47+
}
48+
49+
sql app.db {
50+
insert comment into Comment
51+
}!
52+
}
53+
54+
fn (mut app App) get_all_issue_comments(issue_id int) []Comment {
55+
comments := sql app.db {
56+
select from Comment where issue_id == issue_id
57+
} or { []Comment{} }
58+
59+
return comments
60+
}
61+
62+
fn (c Comment) relative() string {
63+
return time.unix(c.created_at).relative()
64+
}

src/comment_entities.v

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/comment_service.v

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)