115 lines
2.9 KiB
Go
115 lines
2.9 KiB
Go
package models
|
|
|
|
import (
|
|
"database/sql"
|
|
"time"
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
polochon "github.com/odwrtw/polochon/lib"
|
|
)
|
|
|
|
const (
|
|
upsertMovieTorrentQuery = `
|
|
INSERT INTO movie_torrents (imdb_id, url, source, quality, upload_user,
|
|
seeders, leechers, size)
|
|
VALUES (:imdb_id, :url, :source, :quality, :upload_user, :seeders,
|
|
:leechers, :size)
|
|
ON CONFLICT (imdb_id, quality, source)
|
|
DO UPDATE SET imdb_id=:imdb_id, url=:url, source=:source, quality=:quality,
|
|
upload_user=:upload_user, seeders=:seeders, leechers=:leechers, size=:size
|
|
RETURNING id;`
|
|
|
|
getMovieTorrentQueryByImdbID = `
|
|
SELECT *
|
|
FROM movie_torrents WHERE imdb_id=$1;`
|
|
)
|
|
|
|
// movieTorrentDB represents the MovieTorrent in the DB
|
|
type movieTorrentDB struct {
|
|
ID string `db:"id"`
|
|
ImdbID string `db:"imdb_id"`
|
|
URL string `db:"url"`
|
|
Source string `db:"source"`
|
|
Quality string `db:"quality"`
|
|
UploadUser string `db:"upload_user"`
|
|
Seeders int `db:"seeders"`
|
|
Leechers int `db:"leechers"`
|
|
Size int `db:"size"`
|
|
Created time.Time `db:"created_at"`
|
|
Updated time.Time `db:"updated_at"`
|
|
}
|
|
|
|
// newTorrentFromMovieTorrentDB creates a new polochon.Torrent from a
|
|
// movieTorrentDB
|
|
func newTorrentFromMovieTorrentDB(mDB *movieTorrentDB) *polochon.Torrent {
|
|
return &polochon.Torrent{
|
|
ImdbID: mDB.ImdbID,
|
|
Quality: polochon.Quality(mDB.Quality),
|
|
Type: polochon.TypeMovie,
|
|
Result: &polochon.TorrentResult{
|
|
URL: mDB.URL,
|
|
Seeders: mDB.Seeders,
|
|
Leechers: mDB.Leechers,
|
|
Source: mDB.Source,
|
|
UploadUser: mDB.UploadUser,
|
|
Size: mDB.Size,
|
|
},
|
|
}
|
|
}
|
|
|
|
// newMovieTorrentDB returns a MovieTorrent ready to be put in DB from a
|
|
// Torrent
|
|
func newMovieTorrentDB(t *polochon.Torrent) movieTorrentDB {
|
|
m := movieTorrentDB{
|
|
ImdbID: t.ImdbID,
|
|
Quality: string(t.Quality),
|
|
}
|
|
|
|
if t.Result == nil {
|
|
return m
|
|
}
|
|
|
|
m.URL = t.Result.URL
|
|
m.Source = t.Result.Source
|
|
m.UploadUser = t.Result.UploadUser
|
|
m.Seeders = t.Result.Seeders
|
|
m.Leechers = t.Result.Leechers
|
|
m.Size = t.Result.Size
|
|
return m
|
|
}
|
|
|
|
// GetMovieTorrents returns polochon.Torrents from the database
|
|
func GetMovieTorrents(db *sqlx.DB, imdbID string) ([]*polochon.Torrent, error) {
|
|
var torrentsDB = []*movieTorrentDB{}
|
|
// Get the torrents from the DB
|
|
err := db.Select(&torrentsDB, getMovieTorrentQueryByImdbID, imdbID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if len(torrentsDB) == 0 {
|
|
return nil, sql.ErrNoRows
|
|
}
|
|
|
|
// Create polochon Torrents from the movieTorrentDB
|
|
var torrents []*polochon.Torrent
|
|
for _, torrentDB := range torrentsDB {
|
|
torrent := newTorrentFromMovieTorrentDB(torrentDB)
|
|
torrents = append(torrents, torrent)
|
|
}
|
|
|
|
return torrents, nil
|
|
}
|
|
|
|
// UpsertMovieTorrent adds or updates MovieTorrent in db
|
|
func UpsertMovieTorrent(db *sqlx.DB, t *polochon.Torrent) error {
|
|
mDB := newMovieTorrentDB(t)
|
|
r, err := db.NamedQuery(upsertMovieTorrentQuery, mDB)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer r.Close()
|
|
|
|
return nil
|
|
}
|