Skip to content

Commit 44af45f

Browse files
committed
simplify issue.v
1 parent 0f14d71 commit 44af45f

File tree

3 files changed

+30
-74
lines changed

3 files changed

+30
-74
lines changed

src/issue_service.v renamed to src/issue.v

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,35 @@ module main
44

55
import time
66

7+
struct Issue {
8+
id int [primary; sql: serial]
9+
mut:
10+
author_id int
11+
repo_id int
12+
is_pr bool
13+
assigned []int [skip]
14+
labels []int [skip]
15+
comments_count int
16+
title string
17+
text string
18+
created_at int
19+
status IssueStatus [skip]
20+
linked_issues []int [skip]
21+
repo_author string [skip]
22+
repo_name string [skip]
23+
}
24+
25+
enum IssueStatus {
26+
open = 0
27+
closed = 1
28+
}
29+
30+
struct Label {
31+
id int
32+
name string
33+
color string
34+
}
35+
736
fn (mut app App) add_issue(repo_id int, author_id int, title string, text string) ! {
837
issue := Issue{
938
title: title

src/issue_entities.v

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

src/issue_routes.v

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,14 @@ type CommentWithUser = ItemWithUser[Comment]
1515
['/api/v1/:username/:repo_name/issues/count']
1616
fn (mut app App) handle_issues_count(username string, repo_name string) vweb.Result {
1717
has_access := app.has_user_repo_read_access_by_repo_name(app.user.id, username, repo_name)
18-
1918
if !has_access {
2019
return app.json_error('Not found')
2120
}
22-
2321
repo := app.find_repo_by_name_and_username(repo_name, username)
24-
2522
if repo.id == 0 {
2623
return app.json_error('Not found')
2724
}
28-
2925
count := app.get_repo_issue_count(repo.id)
30-
3126
return app.json(api.ApiIssueCount{
3227
success: true
3328
result: count
@@ -39,13 +34,10 @@ pub fn (mut app App) new_issue(username string, repo_name string) vweb.Result {
3934
if !app.logged_in {
4035
return app.not_found()
4136
}
42-
4337
repo := app.find_repo_by_name_and_username(repo_name, username)
44-
4538
if repo.id == 0 {
4639
return app.not_found()
4740
}
48-
4941
return $vweb.html()
5042
}
5143

@@ -60,33 +52,24 @@ pub fn (mut app App) handle_add_repo_issue(username string, repo_name string) vw
6052
if !app.logged_in || (app.logged_in && app.user.posts_count >= posts_per_day) {
6153
return app.redirect_to_index()
6254
}
63-
6455
repo := app.find_repo_by_name_and_username(repo_name, username)
65-
6656
if repo.id == 0 {
6757
return app.not_found()
6858
}
69-
7059
title := app.form['title']
7160
text := app.form['text']
72-
7361
is_title_empty := validation.is_string_empty(title)
7462
is_text_empty := validation.is_string_empty(text)
75-
7663
if is_title_empty || is_text_empty {
7764
return app.redirect('/${username}/${repo_name}/issues/new')
7865
}
79-
8066
app.increment_user_post(mut app.user) or { app.info(err.str()) }
8167
app.add_issue(repo.id, app.user.id, title, text) or { app.info(err.str()) }
8268
app.increment_repo_issues(repo.id) or { app.info(err.str()) }
83-
8469
has_first_issue_activity := app.has_activity(app.user.id, 'first_issue')
85-
8670
if !has_first_issue_activity {
8771
app.add_activity(app.user.id, 'first_issue') or { app.info(err.str()) }
8872
}
89-
9073
return app.redirect('/${username}/${repo_name}/issues')
9174
}
9275

@@ -98,25 +81,19 @@ pub fn (mut app App) handle_get_repo_issues(username string, repo_name string) v
9881
['/:username/:repo_name/issues/:page']
9982
pub fn (mut app App) issues(username string, repo_name string, page int) vweb.Result {
10083
repo := app.find_repo_by_name_and_username(repo_name, username)
101-
10284
if repo.id == 0 {
103-
app.not_found()
85+
return app.not_found()
10486
}
105-
10687
mut issues_with_users := []IssueWithUser{}
107-
10888
for issue in app.find_repo_issues_as_page(repo.id, page) {
10989
user := app.get_user_by_id(issue.author_id) or { continue }
110-
11190
issues_with_users << IssueWithUser{
11291
item: issue
11392
user: user
11493
}
11594
}
116-
11795
mut first := false
11896
mut last := false
119-
12097
if repo.open_issues_count > commits_per_page {
12198
offset := page * commits_per_page
12299
delta := repo.open_issues_count - offset
@@ -131,35 +108,27 @@ pub fn (mut app App) issues(username string, repo_name string, page int) vweb.Re
131108
last = true
132109
first = true
133110
}
134-
135111
page_count := calculate_pages(repo.open_issues_count, commits_per_page)
136112
prev_page, next_page := generate_prev_next_pages(page)
137-
138113
return $vweb.html()
139114
}
140115

141116
['/:username/:repo_name/issue/:id']
142117
pub fn (mut app App) issue(username string, repo_name string, id string) vweb.Result {
143118
repo := app.find_repo_by_name_and_username(repo_name, username)
144-
145119
if repo.id == 0 {
146120
return app.not_found()
147121
}
148-
149122
issue := app.find_issue_by_id(id.int()) or { return app.not_found() }
150123
issue_author := app.get_user_by_id(issue.author_id) or { return app.not_found() }
151-
152124
mut comments_with_users := []CommentWithUser{}
153-
154125
for comment in app.get_all_issue_comments(issue.id) {
155126
user := app.get_user_by_id(comment.author_id) or { continue }
156-
157127
comments_with_users << CommentWithUser{
158128
item: comment
159129
user: user
160130
}
161131
}
162-
163132
return $vweb.html()
164133
}
165134

@@ -168,27 +137,21 @@ pub fn (mut app App) user_issues(username string, page int) vweb.Result {
168137
if !app.logged_in {
169138
return app.not_found()
170139
}
171-
172140
if app.user.username != username {
173141
return app.not_found()
174142
}
175-
176143
exists, user := app.check_username(username)
177-
178144
if !exists {
179145
return app.not_found()
180146
}
181-
182147
mut issues := app.find_user_issues(user.id)
183148
mut first := false
184149
mut last := false
185-
186150
for i, issue in issues {
187151
repo := app.find_repo_by_id(issue.repo_id)
188152
issues[i].repo_author = repo.user_name
189153
issues[i].repo_name = repo.name
190154
}
191-
192155
if issues.len > commits_per_page {
193156
offset := page * commits_per_page
194157
delta := issues.len - offset
@@ -203,24 +166,18 @@ pub fn (mut app App) user_issues(username string, page int) vweb.Result {
203166
last = true
204167
first = true
205168
}
206-
207169
mut issues_with_users := []IssueWithUser{}
208-
209170
for issue in issues {
210171
issue_author := app.get_user_by_id(issue.author_id) or { continue }
211-
212172
issues_with_users << IssueWithUser{
213173
item: issue
214174
user: issue_author
215175
}
216176
}
217-
218177
mut last_site := 0
219178
if page > 0 {
220179
last_site = page - 1
221180
}
222-
223181
next_site := page + 1
224-
225182
return $vweb.html()
226183
}

0 commit comments

Comments
 (0)