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) GetRepoByHandle(handle string) (*Repo, error) {
34 var repoModel Repo
35 res := m.DB.Where("handle = ?", handle).First(&repoModel)
36 if errors.Is(res.Error, gorm.ErrRecordNotFound) {
37 return nil, nil
38 }
39 if res.Error != nil {
40 return nil, res.Error
41 }
42 return &repoModel, nil
43}
44
45func (m *DBModel) GetRepoBySigningKey(signingKey string) (*Repo, error) {
46 var repoModel Repo
47 res := m.DB.Where("signing_key = ?", signingKey).First(&repoModel)
48 if errors.Is(res.Error, gorm.ErrRecordNotFound) {
49 return nil, nil
50 }
51 if res.Error != nil {
52 return nil, res.Error
53 }
54 return &repoModel, nil
55}
56
57func (m *DBModel) GetRepoByHandleOrDID(arg string) (*Repo, error) {
58 repo, err := m.GetRepoByHandle(arg)
59 if err != nil {
60 return nil, err
61 }
62 if repo != nil {
63 return repo, nil
64 }
65 return m.GetRepo(arg)
66}
67
68func (m *DBModel) UpdateRepo(repo *Repo) error {
69 return m.DB.Save(repo).Error
70}