1package db
2
3import (
4 "fmt"
5 "strings"
6
7 "tangled.org/core/appview/models"
8)
9
10func AddCollaborator(e Execer, c models.Collaborator) error {
11 _, err := e.Exec(
12 `insert into collaborators (did, rkey, subject_did, repo_at) values (?, ?, ?, ?);`,
13 c.Did, c.Rkey, c.SubjectDid, c.RepoAt,
14 )
15 return err
16}
17
18func DeleteCollaborator(e Execer, filters ...filter) error {
19 var conditions []string
20 var args []any
21 for _, filter := range filters {
22 conditions = append(conditions, filter.Condition())
23 args = append(args, filter.Arg()...)
24 }
25
26 whereClause := ""
27 if conditions != nil {
28 whereClause = " where " + strings.Join(conditions, " and ")
29 }
30
31 query := fmt.Sprintf(`delete from collaborators %s`, whereClause)
32
33 _, err := e.Exec(query, args...)
34 return err
35}
36
37func CollaboratingIn(e Execer, collaborator string) ([]models.Repo, error) {
38 rows, err := e.Query(`select repo_at from collaborators where subject_did = ?`, collaborator)
39 if err != nil {
40 return nil, err
41 }
42 defer rows.Close()
43
44 var repoAts []string
45 for rows.Next() {
46 var aturi string
47 err := rows.Scan(&aturi)
48 if err != nil {
49 return nil, err
50 }
51 repoAts = append(repoAts, aturi)
52 }
53 if err := rows.Err(); err != nil {
54 return nil, err
55 }
56 if repoAts == nil {
57 return nil, nil
58 }
59
60 return GetRepos(e, 0, FilterIn("at_uri", repoAts))
61}