add model package with db queries
This commit is contained in:
110
main.go
110
main.go
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"api/model"
|
||||
"embed"
|
||||
_ "embed"
|
||||
"encoding/json"
|
||||
@@ -36,78 +37,6 @@ func sendJSON(w http.ResponseWriter, data any, status int) {
|
||||
}
|
||||
}
|
||||
|
||||
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 getItems(db *sqlx.DB) ([]*Item, error) {
|
||||
rows, err := 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
|
||||
}
|
||||
|
||||
func getItemTorrents(db *sqlx.DB, itemId int64) ([]*Torrent, error) {
|
||||
rows, err := 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 getTorrentById(db *sqlx.DB, torrentId int64) (Torrent, error) {
|
||||
var torrent Torrent
|
||||
|
||||
row := 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
|
||||
}
|
||||
|
||||
type JackettTorrent struct {
|
||||
Seeders string
|
||||
Peers string
|
||||
@@ -152,33 +81,12 @@ func main() {
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
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 := model.New(db)
|
||||
m.Init()
|
||||
|
||||
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)
|
||||
)`)
|
||||
|
||||
go func(db *sqlx.DB) {
|
||||
go func(m *model.Model) {
|
||||
for {
|
||||
items, _ := getItems(db)
|
||||
items, _ := m.GetItems()
|
||||
for _, item := range items {
|
||||
results, err := jackettClient.TVSearch(jackett.TVSearchOptions{
|
||||
Query: item.Query,
|
||||
@@ -229,7 +137,7 @@ func main() {
|
||||
|
||||
time.Sleep(10 * time.Second)
|
||||
}
|
||||
}(db)
|
||||
}(m)
|
||||
|
||||
mux := http.NewServeMux()
|
||||
|
||||
@@ -294,7 +202,7 @@ func main() {
|
||||
})
|
||||
|
||||
mux.HandleFunc("GET /api/items", func(w http.ResponseWriter, r *http.Request) {
|
||||
items, err := getItems(db)
|
||||
items, err := m.GetItems()
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 404)
|
||||
return
|
||||
@@ -310,7 +218,7 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
torrents, err := getItemTorrents(db, int64(itemId))
|
||||
torrents, err := m.GetItemTorrents(int64(itemId))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
@@ -337,7 +245,7 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
torrent, err := getTorrentById(db, int64(torrentId))
|
||||
torrent, err := m.GetTorrentById(int64(torrentId))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 404)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user