Create user in database
This commit is contained in:
parent
e05c9185d6
commit
f35e5db1a9
29
db/user.go
Normal file
29
db/user.go
Normal file
|
@ -0,0 +1,29 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
gorm.Model
|
||||
Username string
|
||||
Password string
|
||||
}
|
||||
|
||||
var (
|
||||
ERROR_USER_USERNAME_NOT_UNIQUE = errors.New("Username already persists in database")
|
||||
)
|
||||
|
||||
func (u *User) BeforeCreate(ctx *gorm.DB) error {
|
||||
var duplicate User
|
||||
err := ctx.Find(&duplicate, User{Username: u.Username}).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if duplicate.ID != 0 {
|
||||
return ERROR_USER_USERNAME_NOT_UNIQUE
|
||||
}
|
||||
return nil
|
||||
}
|
Loading…
Reference in New Issue
Block a user