plz / project.v
176 lines · 157 sloc · 3.83 KB · d4104e7627c1594b9de8e026959b196932f293c8
Raw
1// Copyright (c) 2019-2026 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.
3module main
4
5import time
6import veb
7
8struct Project {
9 id int @[primary; sql: serial]
10mut:
11 repo_id int
12 name string
13 description string
14 created_at int
15}
16
17struct ProjectColumn {
18 id int @[primary; sql: serial]
19mut:
20 project_id int
21 name string
22 position int
23}
24
25struct ProjectCard {
26 id int @[primary; sql: serial]
27mut:
28 column_id int
29 title string
30 note string
31 position int
32 issue_id int // 0 if a free-form note
33 created_at int
34}
35
36fn (p &Project) formatted_name() veb.RawHtml {
37 return html_escape_text(p.name)
38}
39
40fn (mut app App) add_project(repo_id int, name string, description string) !int {
41 pr := Project{
42 repo_id: repo_id
43 name: name
44 description: description
45 created_at: int(time.now().unix())
46 }
47 sql app.db {
48 insert pr into Project
49 }!
50 project_id := db_last_insert_id(mut app.db)
51 if project_id != 0 {
52 for i, col_name in ['Todo', 'In progress', 'Done'] {
53 app.add_project_column(project_id, col_name, i) or {}
54 }
55 }
56 return project_id
57}
58
59fn (mut app App) list_repo_projects(repo_id int) []Project {
60 return sql app.db {
61 select from Project where repo_id == repo_id order by id desc
62 } or { []Project{} }
63}
64
65fn (mut app App) find_project(id int) ?Project {
66 rows := sql app.db {
67 select from Project where id == id limit 1
68 } or { []Project{} }
69 if rows.len == 0 {
70 return none
71 }
72 return rows.first()
73}
74
75fn (mut app App) delete_project(id int) ! {
76 cols := app.list_project_columns(id)
77 for col in cols {
78 sql app.db {
79 delete from ProjectCard where column_id == col.id
80 }!
81 }
82 sql app.db {
83 delete from ProjectColumn where project_id == id
84 }!
85 sql app.db {
86 delete from Project where id == id
87 }!
88}
89
90fn (mut app App) delete_repo_projects(repo_id int) ! {
91 prs := app.list_repo_projects(repo_id)
92 for pr in prs {
93 app.delete_project(pr.id) or {}
94 }
95}
96
97fn (mut app App) add_project_column(project_id int, name string, position int) !int {
98 c := ProjectColumn{
99 project_id: project_id
100 name: name
101 position: position
102 }
103 sql app.db {
104 insert c into ProjectColumn
105 }!
106 return db_last_insert_id(mut app.db)
107}
108
109fn (mut app App) list_project_columns(project_id int) []ProjectColumn {
110 return sql app.db {
111 select from ProjectColumn where project_id == project_id order by position
112 } or { []ProjectColumn{} }
113}
114
115fn (mut app App) find_project_column(id int) ?ProjectColumn {
116 rows := sql app.db {
117 select from ProjectColumn where id == id limit 1
118 } or { []ProjectColumn{} }
119 if rows.len == 0 {
120 return none
121 }
122 return rows.first()
123}
124
125fn (mut app App) delete_project_column(id int) ! {
126 sql app.db {
127 delete from ProjectCard where column_id == id
128 }!
129 sql app.db {
130 delete from ProjectColumn where id == id
131 }!
132}
133
134fn (mut app App) add_project_card(column_id int, title string, note string) ! {
135 pos := sql app.db {
136 select count from ProjectCard where column_id == column_id
137 } or { 0 }
138 c := ProjectCard{
139 column_id: column_id
140 title: title
141 note: note
142 position: pos
143 created_at: int(time.now().unix())
144 }
145 sql app.db {
146 insert c into ProjectCard
147 }!
148}
149
150fn (mut app App) list_project_cards(column_id int) []ProjectCard {
151 return sql app.db {
152 select from ProjectCard where column_id == column_id order by position
153 } or { []ProjectCard{} }
154}
155
156fn (mut app App) find_project_card(id int) ?ProjectCard {
157 rows := sql app.db {
158 select from ProjectCard where id == id limit 1
159 } or { []ProjectCard{} }
160 if rows.len == 0 {
161 return none
162 }
163 return rows.first()
164}
165
166fn (mut app App) move_project_card(card_id int, new_column_id int) ! {
167 sql app.db {
168 update ProjectCard set column_id = new_column_id where id == card_id
169 }!
170}
171
172fn (mut app App) delete_project_card(id int) ! {
173 sql app.db {
174 delete from ProjectCard where id == id
175 }!
176}
177