-
Notifications
You must be signed in to change notification settings - Fork 0
/
bookmark.go
50 lines (39 loc) · 943 Bytes
/
bookmark.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package storage
import (
"errors"
"gorm.io/gorm"
)
type Bookmark struct {
gorm.Model
CardID string `gorm:"index:idx_bookmark_card_id"`
}
type bookmarkRepo struct {
db *gorm.DB
}
func NewBookmarkRepo(db *gorm.DB) *bookmarkRepo {
db.AutoMigrate(&Bookmark{})
repo := &bookmarkRepo{db: db}
return repo
}
func (r *bookmarkRepo) AddBookmark(cardID string) error {
b := Bookmark{CardID: cardID}
err := r.db.Create(&b).Error
return err
}
func (r *bookmarkRepo) RemoveBookmark(cardID string) error {
err := r.db.Where("card_id = ?", cardID).Delete(&Bookmark{}).Error
return err
}
func (r *bookmarkRepo) FindBookmark(cardID string) (*Bookmark, error) {
var b Bookmark
err := r.db.Where("card_id = ?", cardID).First(&b).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, err
}
return &b, err
}
func (r *bookmarkRepo) List() ([]Bookmark, error) {
var bs []Bookmark
err := r.db.Find(&bs).Error
return bs, err
}