Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0

Better DB error handling

Cette révision appartient à :
bakape 2017-05-09 20:23:21 +03:00
Parent c5e226c7a6
révision c46c386981
6 fichiers modifiés avec 62 ajouts et 31 suppressions

Voir le fichier

@ -55,7 +55,10 @@ func main() {
if err != nil {
log.CheckError(err)
}
db.ORM, _ = db.GormInit(conf)
db.ORM, err = db.GormInit(conf)
if err != nil {
log.Fatal(err.Error())
}
initI18N()
go signals.Handle()
if len(config.TorrentFileStorage) > 0 {

Voir le fichier

@ -104,6 +104,10 @@ func ApiUploadHandler(w http.ResponseWriter, r *http.Request) {
Date: time.Now(),
Filesize: 0,
Description: string(b.Description)}
db.ORM.Create(&torrent)
err = db.ORM.Create(&torrent).Error
if err != nil {
util.SendError(w, err, 500)
return
}
fmt.Printf("%+v\n", torrent)
}

Voir le fichier

@ -11,6 +11,7 @@ import (
"github.com/ewhal/nyaa/model"
"github.com/ewhal/nyaa/service/captcha"
"github.com/ewhal/nyaa/service/user"
"github.com/ewhal/nyaa/util"
"github.com/ewhal/nyaa/util/languages"
"github.com/gorilla/mux"
)
@ -42,7 +43,11 @@ func UploadHandler(w http.ResponseWriter, r *http.Request) {
Filesize: uploadForm.Filesize, // FIXME: should set to NULL instead of 0
Description: uploadForm.Description,
UploaderId: user.Id}
db.ORM.Create(&torrent)
err = db.ORM.Create(&torrent).Error
if err != nil {
util.SendError(w, err, 500)
return
}
fmt.Printf("%+v\n", torrent)
url, err := Router.Get("view_torrent").URL("id", strconv.FormatUint(uint64(torrent.Id), 10))
if err == nil {

Voir le fichier

@ -9,6 +9,7 @@ import (
"github.com/ewhal/nyaa/model"
"github.com/ewhal/nyaa/service/captcha"
"github.com/ewhal/nyaa/service/torrent"
"github.com/ewhal/nyaa/util"
"github.com/ewhal/nyaa/util/languages"
"github.com/ewhal/nyaa/util/log"
"github.com/gorilla/mux"
@ -47,11 +48,15 @@ func PostCommentHandler(w http.ResponseWriter, r *http.Request) {
idNum_, err := strconv.Atoi(id)
var idNum uint = uint(idNum_)
var userId uint = 0
if (currentUser.Id > 0) {
if currentUser.Id > 0 {
userId = currentUser.Id
}
comment := model.Comment{TorrentId: idNum, UserId: userId, Content: content, CreatedAt: time.Now()}
db.ORM.Create(&comment)
err = db.ORM.Create(&comment).Error
if err != nil {
util.SendError(w, err, 500)
return
}
url, err := Router.Get("view_torrent").URL("id", id)
if err == nil {

Voir le fichier

@ -23,48 +23,61 @@ type WhereParams struct {
*/
// don't need raw SQL once we get MySQL
func GetFeeds() []model.Feed {
result := make([]model.Feed, 0, 50)
func GetFeeds() (result []model.Feed, err error) {
result = make([]model.Feed, 0, 50)
rows, err := db.ORM.DB().
Query(
"SELECT `torrent_id` AS `id`, `torrent_name` AS `name`, `torrent_hash` AS `hash`, `timestamp` FROM `torrents` " +
"ORDER BY `timestamp` desc LIMIT 50")
if err == nil {
for rows.Next() {
item := model.Feed{}
rows.Scan(&item.Id, &item.Name, &item.Hash, &item.Timestamp)
magnet := util.InfoHashToMagnet(strings.TrimSpace(item.Hash), item.Name, config.Trackers...)
item.Magnet = magnet
// memory hog
result = append(result, item)
}
rows.Close()
if err != nil {
return nil, err
}
return result
defer rows.Close()
for rows.Next() {
item := model.Feed{}
err = rows.Scan(&item.Id, &item.Name, &item.Hash, &item.Timestamp)
if err != nil {
return
}
magnet := util.InfoHashToMagnet(strings.TrimSpace(item.Hash), item.Name, config.Trackers...)
item.Magnet = magnet
// memory hog
result = append(result, item)
}
err = rows.Err()
return
}
func GetTorrentById(id string) (model.Torrents, error) {
var torrent model.Torrents
func GetTorrentById(id string) (torrent model.Torrents, err error) {
id_int, err := strconv.Atoi(id)
if err != nil {
return torrent, err
return
}
tmp := db.ORM.Where("torrent_id = ?", id).Preload("Comments")
err = tmp.Error
if err != nil {
return
}
if id_int <= config.LastOldTorrentId {
// only preload old comments if they could actually exist
tmp = tmp.Preload("OldComments")
}
if tmp.Find(&torrent).RecordNotFound() {
return torrent, errors.New("Article is not found.")
err = errors.New("Article is not found.")
return
}
// .Preload("Comments.User") doesn't work
for i := range torrent.Comments {
torrent.Comments[i].User = new(model.User)
db.ORM.Where("user_id = ?", torrent.Comments[i].UserId).Find(torrent.Comments[i].User)
err = db.ORM.Where("user_id = ?", torrent.Comments[i].UserId).Find(torrent.Comments[i].User).Error
if err != nil {
return
}
}
return torrent, nil
return
}
func GetTorrentsOrderByNoCount(parameters *WhereParams, orderBy string, limit int, offset int) (torrents []model.Torrents, err error) {
@ -77,7 +90,9 @@ func GetTorrentsOrderBy(parameters *WhereParams, orderBy string, limit int, offs
return
}
func getTorrentsOrderBy(parameters *WhereParams, orderBy string, limit int, offset int, countAll bool) (torrents []model.Torrents, count int, err error) {
func getTorrentsOrderBy(parameters *WhereParams, orderBy string, limit int, offset int, countAll bool) (
torrents []model.Torrents, count int, err error,
) {
var conditionArray []string
if strings.HasPrefix(orderBy, "filesize") {
// torrents w/ NULL filesize fuck up the sorting on postgres

Voir le fichier

@ -4,14 +4,13 @@ import (
"errors"
"net/http"
"github.com/gorilla/securecookie"
"golang.org/x/crypto/bcrypt"
formStruct "github.com/ewhal/nyaa/service/user/form"
"github.com/ewhal/nyaa/db"
"github.com/ewhal/nyaa/model"
formStruct "github.com/ewhal/nyaa/service/user/form"
"github.com/ewhal/nyaa/util/log"
"github.com/ewhal/nyaa/util/modelHelper"
"github.com/gorilla/securecookie"
"golang.org/x/crypto/bcrypt"
)
var cookieHandler = securecookie.New(
@ -149,6 +148,6 @@ func CurrentUser(r *http.Request) (model.User, error) {
if db.ORM.Where("api_token = ?", token).First(&user).RecordNotFound() {
return user, errors.New("User is not found.")
}
db.ORM.Model(&user)
return user, nil
err = db.ORM.Model(&user).Error
return user, err
}