Live video on the AT Protocol
1package model
2
3import (
4 "errors"
5
6 "gorm.io/gorm"
7)
8
9type Repo struct {
10 DID string `gorm:"primaryKey;column:did" json:"did"`
11 Handle string `gorm:"index" json:"handle"`
12 PDS string `json:"pds"`
13 Version string `json:"version"`
14 RootCID string `json:"rootCid"`
15}
16
17func (Repo) TableName() string {
18 return "repos"
19}
20
21func (m *DBModel) GetRepo(did string) (*Repo, error) {
22 var repoModel Repo
23 res := m.DB.Where("did = ?", did).First(&repoModel)
24 if errors.Is(res.Error, gorm.ErrRecordNotFound) {
25 return nil, nil
26 }
27 if res.Error != nil {
28 return nil, res.Error
29 }
30 return &repoModel, nil
31}
32
33func (m *DBModel) GetAllRepos() ([]Repo, error) {
34 var repos []Repo
35 res := m.DB.Find(&repos)
36 if res.Error != nil {
37 return nil, res.Error
38 }
39 return repos, nil
40}
41
42func (m *DBModel) GetRepoByHandle(handle string) (*Repo, error) {
43 var repoModel Repo
44 res := m.DB.Where("handle = ?", handle).First(&repoModel)
45 if errors.Is(res.Error, gorm.ErrRecordNotFound) {
46 return nil, nil
47 }
48 if res.Error != nil {
49 return nil, res.Error
50 }
51 return &repoModel, nil
52}
53
54func (m *DBModel) GetRepoBySigningKey(signingKey string) (*Repo, error) {
55 var repoModel Repo
56 res := m.DB.Where("signing_key = ?", signingKey).First(&repoModel)
57 if errors.Is(res.Error, gorm.ErrRecordNotFound) {
58 return nil, nil
59 }
60 if res.Error != nil {
61 return nil, res.Error
62 }
63 return &repoModel, nil
64}
65
66func (m *DBModel) GetRepoByHandleOrDID(arg string) (*Repo, error) {
67 repo, err := m.GetRepoByHandle(arg)
68 if err != nil {
69 return nil, err
70 }
71 if repo != nil {
72 return repo, nil
73 }
74 return m.GetRepo(arg)
75}
76
77func (m *DBModel) UpdateRepo(repo *Repo) error {
78 return m.DB.Save(repo).Error
79}