Monorepo for Tangled
1package db
2
3import (
4 "database/sql"
5 "fmt"
6 "strings"
7 "time"
8
9 "tangled.org/core/appview/models"
10 "tangled.org/core/orm"
11)
12
13func AddCollaborator(e Execer, c models.Collaborator) error {
14 var repoDid *string
15 if c.RepoDid != "" {
16 repoDid = &c.RepoDid
17 }
18
19 _, err := e.Exec(
20 `insert into collaborators (did, rkey, subject_did, repo_at, repo_did) values (?, ?, ?, ?, ?);`,
21 c.Did, c.Rkey, c.SubjectDid, c.RepoAt, repoDid,
22 )
23 return err
24}
25
26func DeleteCollaborator(e Execer, filters ...orm.Filter) error {
27 var conditions []string
28 var args []any
29 for _, filter := range filters {
30 conditions = append(conditions, filter.Condition())
31 args = append(args, filter.Arg()...)
32 }
33
34 whereClause := ""
35 if conditions != nil {
36 whereClause = " where " + strings.Join(conditions, " and ")
37 }
38
39 query := fmt.Sprintf(`delete from collaborators %s`, whereClause)
40
41 _, err := e.Exec(query, args...)
42 return err
43}
44
45func CollaboratingIn(e Execer, collaborator string) ([]models.Repo, error) {
46 rows, err := e.Query(`select repo_at from collaborators where subject_did = ?`, collaborator)
47 if err != nil {
48 return nil, err
49 }
50 defer rows.Close()
51
52 var repoAts []string
53 for rows.Next() {
54 var aturi string
55 err := rows.Scan(&aturi)
56 if err != nil {
57 return nil, err
58 }
59 repoAts = append(repoAts, aturi)
60 }
61 if err := rows.Err(); err != nil {
62 return nil, err
63 }
64 if repoAts == nil {
65 return nil, nil
66 }
67
68 return GetRepos(e, 0, orm.FilterIn("at_uri", repoAts))
69}
70
71func GetCollaborators(e Execer, filters ...orm.Filter) ([]models.Collaborator, error) {
72 var collaborators []models.Collaborator
73 var conditions []string
74 var args []any
75 for _, filter := range filters {
76 conditions = append(conditions, filter.Condition())
77 args = append(args, filter.Arg()...)
78 }
79 whereClause := ""
80 if conditions != nil {
81 whereClause = " where " + strings.Join(conditions, " and ")
82 }
83 query := fmt.Sprintf(`select
84 id,
85 did,
86 rkey,
87 subject_did,
88 repo_at,
89 created,
90 repo_did
91 from collaborators %s`,
92 whereClause,
93 )
94 rows, err := e.Query(query, args...)
95 if err != nil {
96 return nil, err
97 }
98 defer rows.Close()
99 for rows.Next() {
100 var collaborator models.Collaborator
101 var createdAt string
102 var collabRepoDid sql.NullString
103 if err := rows.Scan(
104 &collaborator.Id,
105 &collaborator.Did,
106 &collaborator.Rkey,
107 &collaborator.SubjectDid,
108 &collaborator.RepoAt,
109 &createdAt,
110 &collabRepoDid,
111 ); err != nil {
112 return nil, err
113 }
114 collaborator.Created, err = time.Parse(time.RFC3339, createdAt)
115 if err != nil {
116 collaborator.Created = time.Now()
117 }
118 if collabRepoDid.Valid {
119 collaborator.RepoDid = collabRepoDid.String
120 }
121 collaborators = append(collaborators, collaborator)
122 }
123 if err := rows.Err(); err != nil {
124 return nil, err
125 }
126 return collaborators, nil
127}