|
| 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. |
1 | 3 | module main
|
2 | 4 |
|
3 | 5 | import vweb
|
4 | 6 | 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 | +} |
5 | 17 |
|
6 | 18 | ['/:username/:repo_name/comments'; post]
|
7 | 19 | pub fn (mut app App) handle_add_comment(username string, repo_name string) vweb.Result {
|
8 | 20 | repo := app.find_repo_by_name_and_username(repo_name, username)
|
9 |
| - |
10 | 21 | if repo.id == 0 {
|
11 | 22 | return app.not_found()
|
12 | 23 | }
|
13 |
| - |
14 | 24 | text := app.form['text']
|
15 | 25 | issue_id := app.form['issue_id']
|
16 |
| - |
17 | 26 | is_text_empty := validation.is_string_empty(text)
|
18 | 27 | is_issue_id_empty := validation.is_string_empty(issue_id)
|
19 |
| - |
20 | 28 | if is_text_empty || is_issue_id_empty || !app.logged_in {
|
21 | 29 | app.error('Issue comment is not valid')
|
22 |
| - |
23 | 30 | return app.issue(username, repo_name, issue_id)
|
24 | 31 | }
|
25 |
| - |
26 | 32 | app.add_issue_comment(app.user.id, issue_id.int(), text) or {
|
27 | 33 | app.error('There was an error while inserting the comment')
|
28 |
| - |
29 | 34 | return app.issue(username, repo_name, issue_id)
|
30 | 35 | }
|
31 |
| - |
32 | 36 | // TODO: count comments
|
33 | 37 | app.increment_issue_comments(issue_id.int()) or { app.info(err.str()) }
|
34 |
| - |
35 | 38 | return app.redirect('/${username}/${repo_name}/issue/${issue_id}')
|
36 | 39 | }
|
| 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 | +} |
0 commit comments