Compare commits

..

No commits in common. "a2ad14d388e62565371f5aa8f4580b6fb95979e6" and "2f23e2ab91e7ffb4b602cd7381015acb2b6d63c3" have entirely different histories.

7 changed files with 18 additions and 138 deletions

1
.gitignore vendored
View File

@ -9,7 +9,6 @@
*.so *.so
*.dylib *.dylib
go-cook go-cook
*.db
# Test binary, built with `go test -c` # Test binary, built with `go test -c`
*.test *.test

View File

@ -1,8 +0,0 @@
{
"cSpell.words": [
"glebarez",
"gocook",
"huandu",
"sqlbuilder"
]
}

View File

@ -1,5 +0,0 @@
migrate-up:
GOOSE_DRIVER=sqlite3 GOOSE_DBSTRING=./gocook.db goose -dir ./api/migrations up
migrate-down:
GOOSE_DRIVER=sqlite3 GOOSE_DBSTRING=./gocook.db goose -dir ./api/migrations down

View File

@ -1,16 +0,0 @@
-- +goose Up
-- +goose StatementBegin
SELECT 'up SQL query';
CREATE TABLE USERS (
ID INTEGER PRIMARY KEY AUTOINCREMENT,
Name TEXT NOT NULL,
Hash TEXT NOT NULL,
CreatedAt DATETIME NOT NULL,
LastUpdated DATETIME NOT NULL
)
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
SELECT 'down SQL query';
DROP TABLE IF EXISTS USERS;
-- +goose StatementEnd

View File

@ -1,11 +1,6 @@
package models package models
import "time"
type UserModel struct { type UserModel struct {
Id int
Name string Name string
Hash string Hash string
CreatedAt time.Time
LastUpdated time.Time
} }

View File

@ -2,9 +2,9 @@ package repositories
import ( import (
"database/sql" "database/sql"
"fmt"
"go-cook/api/models" "go-cook/api/models"
"time" "errors"
"fmt"
"github.com/huandu/go-sqlbuilder" "github.com/huandu/go-sqlbuilder"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
@ -25,77 +25,25 @@ func (ur UserRepository) GetByName(name string) (models.UserModel, error) {
builder := sqlbuilder.NewSelectBuilder() builder := sqlbuilder.NewSelectBuilder()
builder.Select("*").From("users").Where( builder.Select("*").From("users").Where(
builder.E("Name", name), builder.E("Name", name),
) );
query, args := builder.Build()
rows, err := ur.connection.Query(query, args...)
if err != nil {
return models.UserModel{}, err
}
return ur.processRows(rows)[0], nil return models.UserModel{}, errors.New("user was not found")
} }
func (ur UserRepository) NewUser(name, password string) (int64, error) { func (ur UserRepository) NewUser(name string, password string) (int, error) {
passwordBytes := []byte(password) passwordBytes := []byte(password)
hash, err := bcrypt.GenerateFromPassword(passwordBytes, bcrypt.DefaultCost) hash, err := bcrypt.GenerateFromPassword(passwordBytes, bcrypt.DefaultCost)
if err != nil { if err != nil {
return 0, err return 0, err
} }
fmt.Println(hash)
dt := time.Now() query := sqlbuilder.NewInsertBuilder()
queryBuilder := sqlbuilder.NewInsertBuilder() query.InsertInto("users")
queryBuilder.InsertInto("users") query.Cols("name", "hash")
queryBuilder.Cols("Name", "Hash", "LastUpdated", "CreatedAt") query.Values(name, string(hash))
queryBuilder.Values(name, string(hash), dt, dt) ur.connection.Query(query.Build())
query, args := queryBuilder.Build()
_, err = ur.connection.Exec(query, args...)
if err != nil {
return 0, err
}
return 1, nil return 1, nil
} }
// If the hash matches what we have in the database, an error will not be returned.
// If the user does not exist or the hash does not match, an error will be returned
func (ur UserRepository) CheckUserHash(name, password string) error {
record, err := ur.GetByName(name)
if err != nil {
return err
}
err = bcrypt.CompareHashAndPassword([]byte(record.Hash), []byte(password))
if err != nil {
return err
}
return nil
}
func (ur UserRepository) processRows(rows *sql.Rows) []models.UserModel {
items := []models.UserModel{}
for rows.Next() {
var id int
var name string
var hash string
var createdAt time.Time
var lastUpdated time.Time
err := rows.Scan(&id, &name, &hash, &createdAt, &lastUpdated)
if err != nil {
fmt.Println(err)
}
items = append(items, models.UserModel{
Id: id,
Name: name,
Hash: hash,
CreatedAt: createdAt,
LastUpdated: lastUpdated,
})
}
return items
}

View File

@ -1,40 +1,7 @@
package repositories_test package repositories_test
import ( import "testing"
"database/sql"
"go-cook/api/repositories"
"log"
"testing"
_ "github.com/glebarez/go-sqlite" func TestNewUser(t *testing.T) {
)
func TestCanCreateNewUser(t *testing.T) {
db, err := sql.Open("sqlite", "../../gocook.db")
if err != nil {
log.Println("unable to open connection")
t.FailNow()
}
defer db.Close()
repo := repositories.NewUserRepository(db)
updated, err := repo.NewUser("testing", "NotSecure")
if err != nil {
log.Println(err)
t.FailNow()
}
log.Println(updated)
}
func TestCanFindUserInTable(t *testing.T) {
db, err := sql.Open("sqlite", "../../gocook.db")
if err != nil {
log.Println("unable to open connection")
t.FailNow()
}
defer db.Close()
repo := repositories.NewUserRepository(db)
repo.GetByName("testing")
} }