added a interface and adding to CRUD
This commit is contained in:
parent
e08dbea213
commit
f78d78d061
@ -2,6 +2,7 @@ package repositories
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"go-cook/api/models"
|
"go-cook/api/models"
|
||||||
"time"
|
"time"
|
||||||
@ -12,8 +13,17 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
TableName string = "users"
|
TableName string = "users"
|
||||||
|
ErrUserNotFound string = "requested user was not found"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type IUserTable interface {
|
||||||
|
GetByName(name string) (models.UserModel, error)
|
||||||
|
Create(name, password string) (int64, error)
|
||||||
|
Update(id int, entity models.UserModel) error
|
||||||
|
UpdatePassword(name, password string) error
|
||||||
|
CheckUserHash(name, password string) error
|
||||||
|
}
|
||||||
|
|
||||||
// Creates a new instance of UserRepository with the bound sql
|
// Creates a new instance of UserRepository with the bound sql
|
||||||
func NewUserRepository(conn *sql.DB) UserRepository {
|
func NewUserRepository(conn *sql.DB) UserRepository {
|
||||||
return UserRepository{
|
return UserRepository{
|
||||||
@ -37,10 +47,15 @@ func (ur UserRepository) GetByName(name string) (models.UserModel, error) {
|
|||||||
return models.UserModel{}, err
|
return models.UserModel{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return ur.processRows(rows)[0], nil
|
data := ur.processRows(rows)
|
||||||
|
if (len(data) == 0) {
|
||||||
|
return models.UserModel{}, errors.New(ErrUserNotFound)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ur UserRepository) NewUser(name, password string) (int64, error) {
|
return data[0], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ur UserRepository) Create(name, password string) (int64, 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 {
|
||||||
@ -62,6 +77,10 @@ func (ur UserRepository) NewUser(name, password string) (int64, error) {
|
|||||||
return 1, nil
|
return 1, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ur UserRepository) Update(id int, entity models.UserModel) error {
|
||||||
|
return errors.New("not implemented")
|
||||||
|
}
|
||||||
|
|
||||||
func (ur UserRepository) UpdatePassword(name, password string) error {
|
func (ur UserRepository) UpdatePassword(name, password string) error {
|
||||||
_, err := ur.GetByName(name)
|
_, err := ur.GetByName(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -20,7 +20,7 @@ func TestCanCreateNewUser(t *testing.T) {
|
|||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
repo := repositories.NewUserRepository(db)
|
repo := repositories.NewUserRepository(db)
|
||||||
updated, err := repo.NewUser("testing", "NotSecure")
|
updated, err := repo.Create("testing", "NotSecure")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
|
Loading…
Reference in New Issue
Block a user