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