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