-
-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathorg.v
More file actions
86 lines (77 loc) · 1.94 KB
/
Copy pathorg.v
File metadata and controls
86 lines (77 loc) · 1.94 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
// 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 time
struct Org {
id int @[primary; sql: serial]
name string @[unique]
contact_email string
kind string
created_at time.Time
created_by int
}
struct OrgMember {
id int @[primary; sql: serial]
org_id int @[unique: 'org_member']
user_id int @[unique: 'org_member']
role string
}
pub fn (mut app App) add_org(name string, contact_email string, kind string, created_by int) !int {
new_org := Org{
name: name
contact_email: contact_email
kind: kind
created_at: time.now()
created_by: created_by
}
sql app.db {
insert new_org into Org
}!
row := app.get_org_by_name(name) or { return error('failed to load newly created org') }
return row.id
}
pub fn (app App) get_org_by_name(name string) ?Org {
rows := sql app.db {
select from Org where name == name limit 1
} or { [] }
if rows.len == 0 {
return none
}
return rows.first()
}
pub fn (app App) get_org_by_id(id int) ?Org {
rows := sql app.db {
select from Org where id == id limit 1
} or { [] }
if rows.len == 0 {
return none
}
return rows.first()
}
pub fn (mut app App) add_org_member(org_id int, user_id int, role string) ! {
member := OrgMember{
org_id: org_id
user_id: user_id
role: role
}
sql app.db {
insert member into OrgMember
}!
}
pub fn (app App) find_orgs_for_user(user_id int) []Org {
members := sql app.db {
select from OrgMember where user_id == user_id
} or { [] }
mut orgs := []Org{cap: members.len}
for m in members {
org := app.get_org_by_id(m.org_id) or { continue }
orgs << org
}
return orgs
}
pub fn (app App) is_org_member(org_id int, user_id int) bool {
count := sql app.db {
select count from OrgMember where org_id == org_id && user_id == user_id
} or { 0 }
return count > 0
}