| 1 | module main |
| 2 | |
| 3 | import time |
| 4 | |
| 5 | struct SshKey { |
| 6 | id int @[primary; sql: serial] |
| 7 | user_id int @[unique: 'ssh_key'] |
| 8 | title string @[unique: 'ssh_key'] |
| 9 | key string |
| 10 | created_at time.Time |
| 11 | } |
| 12 | |
| 13 | fn (mut app App) add_ssh_key(user_id int, title string, key string) ! { |
| 14 | ssh_keys := sql app.db { |
| 15 | select from SshKey where user_id == user_id && title == title limit 1 |
| 16 | } or { [] } |
| 17 | |
| 18 | if ssh_keys.len != 0 { |
| 19 | return error('SSH Key already exists') |
| 20 | } |
| 21 | |
| 22 | new_ssh_key := SshKey{ |
| 23 | user_id: user_id |
| 24 | title: title |
| 25 | key: key |
| 26 | created_at: time.now() |
| 27 | } |
| 28 | |
| 29 | sql app.db { |
| 30 | insert new_ssh_key into SshKey |
| 31 | }! |
| 32 | } |
| 33 | |
| 34 | fn (mut app App) find_ssh_keys(user_id int) []SshKey { |
| 35 | return sql app.db { |
| 36 | select from SshKey where user_id == user_id |
| 37 | } or { [] } |
| 38 | } |
| 39 | |
| 40 | fn (mut app App) remove_ssh_key(user_id int, id int) ! { |
| 41 | sql app.db { |
| 42 | delete from SshKey where id == id && user_id == user_id |
| 43 | }! |
| 44 | } |
| 45 | |