initial commit
git-svn-id: svn://losandesgames.com/alfheim-website@1 15359d88-9307-4e75-a9c1-e5686e5897df
This commit is contained in:
commit
de52d92eb7
15 files changed
+664
No files matched your search
@@ -0,0 +1,61 @@
|
||||
package models
|
||||
|
||||
import "errors"
|
||||
import "time"
|
||||
import "golang.org/x/crypto/bcrypt"
|
||||
import "database/sql"
|
||||
import _ "github.com/lib/pq"
|
||||
|
||||
var Errnorecord = errors.New("no matching record found")
|
||||
var Errinvalidcredentials = errors.New("invalid credentials")
|
||||
var ErrDuplicateemail = errors.New("duplicate email")
|
||||
|
||||
type Account struct {
|
||||
Id int32
|
||||
Username string
|
||||
Password []byte
|
||||
Color int32
|
||||
Firstname string
|
||||
Lastname string
|
||||
Email string
|
||||
Created time.Time
|
||||
}
|
||||
|
||||
type Usermodel struct {
|
||||
DB *sql.DB
|
||||
}
|
||||
|
||||
func (m *Usermodel) Insert(username string, password string, firstname string, lastname string, email string) error {
|
||||
hashedpassword, err := bcrypt.GenerateFromPassword([]byte(password), 12)
|
||||
|
||||
stmt := `INSERT INTO accounts (username, password, firstname, lastname, email, created) VALUES ($1, $2, $3, $4, $5, NOW());`
|
||||
|
||||
_, err = m.DB.Exec(stmt, username, string(hashedpassword), firstname, lastname, email)
|
||||
if err != nil {
|
||||
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Usermodel) Get_account(id int32) (Account, error) {
|
||||
stmt := `SELECT id, username, password, color FROM accounts WHERE id = $1;`
|
||||
row := m.DB.QueryRow(stmt, id)
|
||||
var account Account
|
||||
err := row.Scan(&account.Id, &account.Username, &account.Password, &account.Color)
|
||||
if err == sql.ErrNoRows {
|
||||
return Account{}, sql.ErrNoRows
|
||||
} else if err != nil {
|
||||
return Account{}, err
|
||||
}
|
||||
|
||||
return account, nil
|
||||
}
|
||||
|
||||
func (m *Usermodel) Authenticate(username string, password string) (int, error) {
|
||||
var id int32
|
||||
var hashedpassword []byte
|
||||
row := m.DB.QueryRow("SELECT id, password FROM accounts WHERE username = $1", username)
|
||||
err := row.Scan(&id, &hashedpassword)
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user