-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathssh_key.v
44 lines (36 loc) · 902 Bytes
/
ssh_key.v
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
module main
import time
struct SshKey {
id int @[primary; sql: serial]
user_id int @[unique: 'ssh_key']
title string @[unique: 'ssh_key']
key string
created_at time.Time
}
fn (mut app App) add_ssh_key(user_id int, title string, key string) ! {
ssh_keys := sql app.db {
select from SshKey where user_id == user_id && title == title limit 1
} or { [] }
if ssh_keys.len != 0 {
return error('SSH Key already exists')
}
new_ssh_key := SshKey{
user_id: user_id
title: title
key: key
created_at: time.now()
}
sql app.db {
insert new_ssh_key into SshKey
}!
}
fn (mut app App) find_ssh_keys(user_id int) []SshKey {
return sql app.db {
select from SshKey where user_id == user_id
} or { [] }
}
fn (mut app App) remove_ssh_key(user_id int, id int) ! {
sql app.db {
delete from SshKey where id == id && user_id == user_id
}!
}