-
-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathproject_routes.v
More file actions
175 lines (163 loc) · 7.09 KB
/
Copy pathproject_routes.v
File metadata and controls
175 lines (163 loc) · 7.09 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright (c) 2019-2026 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
struct ProjectColumnView {
column ProjectColumn
cards []ProjectCard
}
@['/:username/:repo_name/projects']
pub fn (mut app App) handle_get_repo_projects(mut ctx Context, username string, repo_name string) veb.Result {
repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() }
if !app.has_user_repo_read_access(ctx, ctx.user.id, repo.id) && !repo.is_public {
return ctx.not_found()
}
projects := app.list_repo_projects(repo.id)
return $veb.html('templates/projects.html')
}
@['/:username/:repo_name/projects/new']
pub fn (mut app App) new_project(mut ctx Context, username string, repo_name string) veb.Result {
if !ctx.logged_in {
return ctx.redirect_to_login()
}
repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() }
if repo.user_id != ctx.user.id {
return ctx.redirect('/${username}/${repo_name}/projects')
}
return $veb.html('templates/new/project.html')
}
@['/:username/:repo_name/projects'; post]
pub fn (mut app App) handle_create_project(mut ctx Context, username string, repo_name string) veb.Result {
if !ctx.logged_in {
return ctx.redirect_to_login()
}
repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() }
if repo.user_id != ctx.user.id {
return ctx.redirect('/${username}/${repo_name}/projects')
}
name := ctx.form['name']
desc := ctx.form['description']
if validation.is_string_empty(name) {
return ctx.redirect('/${username}/${repo_name}/projects/new')
}
id := app.add_project(repo.id, name, desc) or {
ctx.error('Could not create project')
return ctx.redirect('/${username}/${repo_name}/projects/new')
}
return ctx.redirect('/${username}/${repo_name}/projects/${id}')
}
@['/:username/:repo_name/projects/:id']
pub fn (mut app App) view_project(mut ctx Context, username string, repo_name string, id string) veb.Result {
repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() }
if !app.has_user_repo_read_access(ctx, ctx.user.id, repo.id) && !repo.is_public {
return ctx.not_found()
}
project := app.find_project(id.int()) or { return ctx.not_found() }
if project.repo_id != repo.id {
return ctx.not_found()
}
columns := app.list_project_columns(project.id)
mut views := []ProjectColumnView{}
for col in columns {
views << ProjectColumnView{
column: col
cards: app.list_project_cards(col.id)
}
}
can_edit := ctx.logged_in && repo.user_id == ctx.user.id
return $veb.html('templates/project.html')
}
@['/:username/:repo_name/projects/:id/columns'; post]
pub fn (mut app App) handle_add_project_column(mut ctx Context, username string, repo_name string, id string) veb.Result {
if !ctx.logged_in {
return ctx.redirect_to_login()
}
repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() }
project := app.find_project(id.int()) or { return ctx.not_found() }
if project.repo_id != repo.id || repo.user_id != ctx.user.id {
return ctx.redirect('/${username}/${repo_name}/projects/${id}')
}
name := ctx.form['name']
if validation.is_string_empty(name) {
return ctx.redirect('/${username}/${repo_name}/projects/${id}')
}
pos := app.list_project_columns(project.id).len
app.add_project_column(project.id, name, pos) or {}
return ctx.redirect('/${username}/${repo_name}/projects/${id}')
}
@['/:username/:repo_name/projects/:id/columns/:col_id/delete'; post]
pub fn (mut app App) handle_delete_project_column(mut ctx Context, username string, repo_name string, id string, col_id string) veb.Result {
if !ctx.logged_in {
return ctx.redirect_to_login()
}
repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() }
project := app.find_project(id.int()) or { return ctx.not_found() }
if project.repo_id != repo.id || repo.user_id != ctx.user.id {
return ctx.redirect('/${username}/${repo_name}/projects/${id}')
}
app.delete_project_column(col_id.int()) or {}
return ctx.redirect('/${username}/${repo_name}/projects/${id}')
}
@['/:username/:repo_name/projects/:id/columns/:col_id/cards'; post]
pub fn (mut app App) handle_add_project_card(mut ctx Context, username string, repo_name string, id string, col_id string) veb.Result {
if !ctx.logged_in {
return ctx.redirect_to_login()
}
repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() }
project := app.find_project(id.int()) or { return ctx.not_found() }
if project.repo_id != repo.id || repo.user_id != ctx.user.id {
return ctx.redirect('/${username}/${repo_name}/projects/${id}')
}
col := app.find_project_column(col_id.int()) or { return ctx.not_found() }
if col.project_id != project.id {
return ctx.not_found()
}
title := ctx.form['title']
note := ctx.form['note']
if validation.is_string_empty(title) {
return ctx.redirect('/${username}/${repo_name}/projects/${id}')
}
app.add_project_card(col.id, title, note) or {}
return ctx.redirect('/${username}/${repo_name}/projects/${id}')
}
@['/:username/:repo_name/projects/:id/cards/:card_id/delete'; post]
pub fn (mut app App) handle_delete_project_card(mut ctx Context, username string, repo_name string, id string, card_id string) veb.Result {
if !ctx.logged_in {
return ctx.redirect_to_login()
}
repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() }
project := app.find_project(id.int()) or { return ctx.not_found() }
if project.repo_id != repo.id || repo.user_id != ctx.user.id {
return ctx.redirect('/${username}/${repo_name}/projects/${id}')
}
app.delete_project_card(card_id.int()) or {}
return ctx.redirect('/${username}/${repo_name}/projects/${id}')
}
@['/:username/:repo_name/projects/:id/cards/:card_id/move'; post]
pub fn (mut app App) handle_move_project_card(mut ctx Context, username string, repo_name string, id string, card_id string) veb.Result {
if !ctx.logged_in {
return ctx.redirect_to_login()
}
repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() }
project := app.find_project(id.int()) or { return ctx.not_found() }
if project.repo_id != repo.id || repo.user_id != ctx.user.id {
return ctx.redirect('/${username}/${repo_name}/projects/${id}')
}
new_col := ctx.form['column_id'].int()
app.move_project_card(card_id.int(), new_col) or {}
return ctx.redirect('/${username}/${repo_name}/projects/${id}')
}
@['/:username/:repo_name/projects/:id/delete'; post]
pub fn (mut app App) handle_delete_project(mut ctx Context, username string, repo_name string, id string) veb.Result {
if !ctx.logged_in {
return ctx.redirect_to_login()
}
repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() }
project := app.find_project(id.int()) or { return ctx.not_found() }
if project.repo_id != repo.id || repo.user_id != ctx.user.id {
return ctx.redirect('/${username}/${repo_name}/projects/${id}')
}
app.delete_project(project.id) or {}
return ctx.redirect('/${username}/${repo_name}/projects')
}