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
Chris MacLeod 14378fbf39 Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds

The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.

* Style changes and old code removal in router

Router needs a lot of work done to its (lack of) error handling.

* Dead code removal and style changes

Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.

* Finish dead code removal and style changes

Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.

* Fix accidental find-and-replace

* Style, error checking, saftey, bug fix changes

* Redo error checking erased during merge

* Re-add merge-erased fix. Make Safe safe.
2017-05-09 21:34:40 -05:00

36 lignes
979 o
Go

package userService
import (
"errors"
"github.com/ewhal/nyaa/db"
"github.com/ewhal/nyaa/model"
"net/http"
"github.com/ewhal/nyaa/util/log"
)
// FindUserByUserName creates a user.
func FindUserByUserName(userName string) (model.User, int, error) {
var user model.User
var err error
if db.ORM.Where("name=?", userName).First(&user).RecordNotFound() {
return user, http.StatusUnauthorized, err
}
return user, http.StatusOK, nil
}
// FindOrCreateUser creates a user.
func FindOrCreateUser(username string) (model.User, int, error) {
var user model.User
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
}