add model package with db queries

This commit is contained in:
2026-02-03 12:32:46 +03:00
parent 7ca3587407
commit 7d647a00b4
4 changed files with 130 additions and 101 deletions

21
model/items.go Normal file
View File

@@ -0,0 +1,21 @@
package model
import "fmt"
func (m *Model) GetItems() ([]*Item, error) {
rows, err := m.db.Queryx("SELECT * FROM items")
if err != nil {
return nil, fmt.Errorf("couldn't query db: %v", err)
}
items := []*Item{}
for rows.Next() {
item := &Item{}
if err := rows.StructScan(&item); err != nil {
continue
}
items = append(items, item)
}
return items, nil
}

68
model/model.go Normal file
View File

@@ -0,0 +1,68 @@
package model
import (
"time"
"github.com/jmoiron/sqlx"
)
type Model struct {
db *sqlx.DB
}
type Torrent struct {
ID int64 `json:"id" db:"id"`
Title string `json:"title" db:"title"`
Guid string `json:"guid" db:"guid"`
Indexer string `json:"indexer" db:"indexer"`
Pubdate time.Time `json:"pubdate" db:"pubdate"`
Size int `json:"size" db:"size"`
DownloadURL string `json:"downloadUrl" db:"download_url"`
Seeders int `json:"seeders" db:"seeders"`
Peers int `json:"peers" db:"peers"`
Category int `json:"category" db:"category"`
Hash *string `json:"hash" db:"hash"`
Downloaded bool `json:"downloaded"`
CreatedAt time.Time `json:"createdAt" db:"created_at"`
ItemID int `json:"itemId" db:"item_id"`
}
type Item struct {
ID int64 `json:"id" db:"id"`
Query string `json:"query" db:"query"`
Category int `json:"category" db:"category"`
CreatedAt time.Time `json:"createdAt" db:"created_at"`
Torrents []Torrent `json:"torrents,omitempty"`
}
func (m *Model) Init() {
m.db.MustExec(`CREATE TABLE IF NOT EXISTS items (
id integer primary key,
query varchar not null,
category integer not null,
created_at datetime default CURRENT_TIMESTAMP
)`)
m.db.MustExec(`CREATE TABLE IF NOT EXISTS torrents (
id integer primary key,
title varchar not null,
guid varchar not null unique,
indexer varchar not null,
pubdate datetime not null,
size integer not null,
download_url varchar,
seeders integer not null,
peers integer not null,
category integer not null,
hash varchar,
created_at datetime default CURRENT_TIMESTAMP,
item_id integer not null,
FOREIGN KEY (item_id) REFERENCES users(id)
)`)
}
func New(db *sqlx.DB) *Model {
return &Model{
db: db,
}
}

32
model/torrents.go Normal file
View 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
}