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/service/user/userHelper.go

37 lignes
979 o
Go
Brut Vue normale Historique

package userService
import (
"errors"
2017-05-06 22:14:02 +02:00
"github.com/ewhal/nyaa/db"
2017-05-06 21:37:10 +02:00
"github.com/ewhal/nyaa/model"
"net/http"
2017-05-06 21:37:10 +02:00
"github.com/ewhal/nyaa/util/log"
)
// FindUserByUserName creates a user.
2017-05-06 22:27:21 +02:00
func FindUserByUserName(userName string) (model.User, int, error) {
var user model.User
var err error
2017-05-06 22:32:02 +02:00
if db.ORM.Where("name=?", userName).First(&user).RecordNotFound() {
return user, http.StatusUnauthorized, err
}
return user, http.StatusOK, nil
}
// FindOrCreateUser creates a user.
2017-05-06 22:27:21 +02:00
func FindOrCreateUser(username string) (model.User, int, error) {
var user model.User
2017-05-06 22:27:21 +02:00
if db.ORM.Where("username=?", username).First(&user).RecordNotFound() {
var newUser model.User
newUser.Username = username
log.Debugf("user %+v\n", newUser)
if db.ORM.Create(&newUser).Error != nil {
return newUser, http.StatusBadRequest, errors.New("user not created")
}
log.Debugf("retrieved User %v\n", newUser)
return newUser, http.StatusOK, nil
}
return user, http.StatusBadRequest, nil
}