| 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. |
| 3 | module main |
| 4 | |
| 5 | import time |
| 6 | |
| 7 | struct Milestone { |
| 8 | id int @[primary; sql: serial] |
| 9 | mut: |
| 10 | repo_id int |
| 11 | title string |
| 12 | description string |
| 13 | due_date int // unix seconds, 0 if not set |
| 14 | is_closed bool |
| 15 | created_at int |
| 16 | } |
| 17 | |
| 18 | fn (m &Milestone) status_label() string { |
| 19 | return if m.is_closed { 'milestone_status_closed' } else { 'milestone_status_open' } |
| 20 | } |
| 21 | |
| 22 | fn (m &Milestone) due_date_str() string { |
| 23 | if m.due_date == 0 { |
| 24 | return '' |
| 25 | } |
| 26 | t := time.unix(m.due_date) |
| 27 | return '${t.year:04d}-${t.month:02d}-${t.day:02d}' |
| 28 | } |
| 29 | |
| 30 | fn (mut app App) add_milestone(repo_id int, title string, description string, due_date int) !int { |
| 31 | m := Milestone{ |
| 32 | repo_id: repo_id |
| 33 | title: title |
| 34 | description: description |
| 35 | due_date: due_date |
| 36 | created_at: int(time.now().unix()) |
| 37 | } |
| 38 | sql app.db { |
| 39 | insert m into Milestone |
| 40 | }! |
| 41 | return db_last_insert_id(app.db) |
| 42 | } |
| 43 | |
| 44 | fn (mut app App) list_repo_milestones(repo_id int) []Milestone { |
| 45 | return sql app.db { |
| 46 | select from Milestone where repo_id == repo_id order by id desc |
| 47 | } or { []Milestone{} } |
| 48 | } |
| 49 | |
| 50 | fn (mut app App) find_milestone(id int) ?Milestone { |
| 51 | rows := sql app.db { |
| 52 | select from Milestone where id == id limit 1 |
| 53 | } or { []Milestone{} } |
| 54 | if rows.len == 0 { |
| 55 | return none |
| 56 | } |
| 57 | return rows.first() |
| 58 | } |
| 59 | |
| 60 | fn (mut app App) set_milestone_closed(id int, closed bool) ! { |
| 61 | sql app.db { |
| 62 | update Milestone set is_closed = closed where id == id |
| 63 | }! |
| 64 | } |
| 65 | |
| 66 | fn (mut app App) delete_milestone(id int) ! { |
| 67 | sql app.db { |
| 68 | delete from Milestone where id == id |
| 69 | }! |
| 70 | } |
| 71 | |
| 72 | fn (mut app App) delete_repo_milestones(repo_id int) ! { |
| 73 | sql app.db { |
| 74 | delete from Milestone where repo_id == repo_id |
| 75 | }! |
| 76 | } |
| 77 | |
| 78 | fn parse_yyyy_mm_dd(s string) int { |
| 79 | if s == '' { |
| 80 | return 0 |
| 81 | } |
| 82 | t := time.parse_iso8601(s + 'T00:00:00Z') or { time.parse(s) or { return 0 } } |
| 83 | return int(t.unix()) |
| 84 | } |
| 85 | |