forked from OpenBazaar/openbazaar-go
/
moderatedstores.go
59 lines (52 loc) · 1.29 KB
/
moderatedstores.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
51
52
53
54
55
56
57
58
59
package db
import (
"database/sql"
"strconv"
"sync"
)
type ModeratedDB struct {
db *sql.DB
lock sync.RWMutex
}
func (m *ModeratedDB) Put(peerId string) error {
m.lock.Lock()
defer m.lock.Unlock()
tx, _ := m.db.Begin()
stmt, _ := tx.Prepare("insert into moderatedstores(peerID) values(?)")
defer stmt.Close()
_, err := stmt.Exec(peerId)
if err != nil {
tx.Rollback()
return err
}
tx.Commit()
return nil
}
func (m *ModeratedDB) Get(offsetId string, limit int) ([]string, error) {
m.lock.RLock()
defer m.lock.RUnlock()
var stm string
if offsetId != "" {
stm = "select peerID from moderatedstores order by rowid desc limit " + strconv.Itoa(limit) + " offset ((select coalesce(max(rowid)+1, 0) from moderatedstores)-(select rowid from moderatedstores where peerID='" + offsetId + "'))"
} else {
stm = "select peerID from moderatedstores order by rowid desc limit " + strconv.Itoa(limit) + " offset 0"
}
var ret []string
rows, err := m.db.Query(stm)
if err != nil {
return ret, err
}
defer rows.Close()
for rows.Next() {
var peerID string
rows.Scan(&peerID)
ret = append(ret, peerID)
}
return ret, nil
}
func (m *ModeratedDB) Delete(follower string) error {
m.lock.Lock()
defer m.lock.Unlock()
m.db.Exec("delete from moderatedstores where peerID=?", follower)
return nil
}