Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0

Make table name for torrents selectable

Cette révision appartient à :
Eliot Whalan 2017-05-15 16:24:06 +10:00
Parent 8f38e2394a
révision 7d5369cfe1
2 fichiers modifiés avec 10 ajouts et 9 suppressions

Voir le fichier

@ -13,6 +13,7 @@ const (
// LastOldTorrentID is the highest torrent ID // LastOldTorrentID is the highest torrent ID
// that was copied from the original Nyaa // that was copied from the original Nyaa
LastOldTorrentID = 923000 LastOldTorrentID = 923000
TableName = "sukebei_torrents"
) )
type Config struct { type Config struct {

Voir le fichier

@ -24,8 +24,8 @@ func GetFeeds() (result []model.Feed, err error) {
result = make([]model.Feed, 0, 50) result = make([]model.Feed, 0, 50)
rows, err := db.ORM.DB(). rows, err := db.ORM.DB().
Query( Query(
"SELECT `torrent_id` AS `id`, `torrent_name` AS `name`, `torrent_hash` AS `hash`, `timestamp` FROM `torrents` " + "SELECT `torrent_id` AS `id`, `torrent_name` AS `name`, `torrent_hash` AS `hash`, `timestamp` FROM `" + config.TableName +
"ORDER BY `timestamp` desc LIMIT 50") "` ORDER BY `timestamp` desc LIMIT 50")
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -53,7 +53,7 @@ func GetTorrentById(id string) (torrent model.Torrent, err error) {
return return
} }
tmp := db.ORM.Where("torrent_id = ?", id).Preload("Comments").Preload("FileList") tmp := db.ORM.Table(config.TableName).Where("torrent_id = ?", id).Preload("Comments").Preload("FileList")
err = tmp.Error err = tmp.Error
if err != nil { if err != nil {
return return
@ -91,7 +91,7 @@ func GetTorrentById(id string) (torrent model.Torrent, err error) {
// won't fetch user or comments // won't fetch user or comments
func GetRawTorrentById(id uint) (torrent model.Torrent, err error) { func GetRawTorrentById(id uint) (torrent model.Torrent, err error) {
err = nil err = nil
if db.ORM.Where("torrent_id = ?", id).Find(&torrent).RecordNotFound() { if db.ORM.Table(config.TableName).Table(config.TableName).Where("torrent_id = ?", id).Find(&torrent).RecordNotFound() {
err = errors.New("Article is not found.") err = errors.New("Article is not found.")
} }
return return
@ -122,7 +122,7 @@ func getTorrentsOrderBy(parameters *serviceBase.WhereParams, orderBy string, lim
conditions := strings.Join(conditionArray, " AND ") conditions := strings.Join(conditionArray, " AND ")
if countAll { if countAll {
// FIXME: `deleted_at IS NULL` is duplicate in here because GORM handles this for us // FIXME: `deleted_at IS NULL` is duplicate in here because GORM handles this for us
err = db.ORM.Model(&torrents).Where(conditions, params...).Count(&count).Error err = db.ORM.Model(&torrents).Table(config.TableName).Where(conditions, params...).Count(&count).Error
if err != nil { if err != nil {
return return
} }
@ -130,7 +130,7 @@ func getTorrentsOrderBy(parameters *serviceBase.WhereParams, orderBy string, lim
// TODO: Vulnerable to injections. Use query builder. (is it?) // TODO: Vulnerable to injections. Use query builder. (is it?)
// build custom db query for performance reasons // build custom db query for performance reasons
dbQuery := "SELECT * FROM torrents" dbQuery := "SELECT * FROM " + config.TableName
if conditions != "" { if conditions != "" {
dbQuery = dbQuery + " WHERE " + conditions dbQuery = dbQuery + " WHERE " + conditions
} }
@ -177,17 +177,17 @@ func GetAllTorrentsDB() ([]model.Torrent, int, error) {
func DeleteTorrent(id string) (int, error) { func DeleteTorrent(id string) (int, error) {
var torrent model.Torrent var torrent model.Torrent
if db.ORM.First(&torrent, id).RecordNotFound() { if db.ORM.Table(config.TableName).First(&torrent, id).RecordNotFound() {
return http.StatusNotFound, errors.New("Torrent is not found.") return http.StatusNotFound, errors.New("Torrent is not found.")
} }
if db.ORM.Delete(&torrent).Error != nil { if db.ORM.Table(config.TableName).Delete(&torrent).Error != nil {
return http.StatusInternalServerError, errors.New("Torrent is not deleted.") return http.StatusInternalServerError, errors.New("Torrent is not deleted.")
} }
return http.StatusOK, nil return http.StatusOK, nil
} }
func UpdateTorrent(torrent model.Torrent) (int, error) { func UpdateTorrent(torrent model.Torrent) (int, error) {
if db.ORM.Save(torrent).Error != nil { if db.ORM.Table(config.TableName).Save(torrent).Error != nil {
return http.StatusInternalServerError, errors.New("Torrent is not updated.") return http.StatusInternalServerError, errors.New("Torrent is not updated.")
} }