add model package with db queries
This commit is contained in:
68
model/model.go
Normal file
68
model/model.go
Normal 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,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user