Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/models/torrents/delete.go

52 lignes
1,6 Kio
Go
Brut Vue normale Historique

package torrents
2017-06-29 01:06:30 +02:00
import (
"errors"
2017-06-29 13:15:23 +02:00
"github.com/NyaaPantsu/nyaa/util/log"
2017-06-29 01:06:30 +02:00
"net/http"
"github.com/NyaaPantsu/nyaa/models"
)
// DeleteTorrent : delete a torrent based on id
func DeleteTorrent(id uint) (*models.Torrent, int, error) {
var torrent models.Torrent
if models.ORM.First(&torrent, id).RecordNotFound() {
return &torrent, http.StatusNotFound, errors.New("Torrent is not found")
}
if models.ORM.Delete(&torrent).Error != nil {
return &torrent, http.StatusInternalServerError, errors.New("Torrent was not deleted")
}
if models.ElasticSearchClient != nil {
err := torrent.DeleteFromESIndex(models.ElasticSearchClient)
if err == nil {
log.Infof("Successfully deleted torrent to ES index.")
} else {
log.Errorf("Unable to delete torrent to ES index: %s", err)
}
}
return &torrent, http.StatusOK, nil
}
// DefinitelyDelete : deletes definitely a torrent based on id
func DefinitelyDelete(id uint) (*models.Torrent, int, error) {
var torrent models.Torrent
if models.ORM.Unscoped().Model(&torrent).First(&torrent, id).RecordNotFound() {
return &torrent, http.StatusNotFound, errors.New("Torrent is not found")
}
if models.ORM.Unscoped().Model(&torrent).Delete(&torrent).Error != nil {
return &torrent, http.StatusInternalServerError, errors.New("Torrent was not deleted")
}
if models.ElasticSearchClient != nil {
err := torrent.DeleteFromESIndex(models.ElasticSearchClient)
if err == nil {
log.Infof("Successfully deleted torrent to ES index.")
} else {
log.Errorf("Unable to delete torrent to ES index: %s", err)
}
}
return &torrent, http.StatusOK, nil
}