add model package with db queries
This commit is contained in:
32
model/torrents.go
Normal file
32
model/torrents.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package model
|
||||
|
||||
import "fmt"
|
||||
|
||||
func (m *Model) GetItemTorrents(itemId int64) ([]*Torrent, error) {
|
||||
rows, err := m.db.Queryx("SELECT * FROM torrents WHERE item_id = ?", itemId)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("couldn't query db: %v", err)
|
||||
}
|
||||
|
||||
torrents := []*Torrent{}
|
||||
for rows.Next() {
|
||||
torrent := &Torrent{}
|
||||
if err := rows.StructScan(&torrent); err != nil {
|
||||
continue
|
||||
}
|
||||
torrents = append(torrents, torrent)
|
||||
}
|
||||
|
||||
return torrents, nil
|
||||
}
|
||||
|
||||
func (m *Model) GetTorrentById(torrentId int64) (*Torrent, error) {
|
||||
torrent := new(Torrent)
|
||||
|
||||
row := m.db.QueryRowx("SELECT * FROM torrents WHERE id = ?", torrentId)
|
||||
if err := row.StructScan(&torrent); err != nil {
|
||||
return torrent, fmt.Errorf("couldn't query torrent: %v", err)
|
||||
}
|
||||
|
||||
return torrent, nil
|
||||
}
|
||||
Reference in New Issue
Block a user