added a interface and adding to CRUD

This commit is contained in:
James Tombleson 2024-03-26 17:53:22 -07:00
parent e08dbea213
commit f78d78d061
2 changed files with 22 additions and 3 deletions

View File

@ -2,6 +2,7 @@ package repositories
import (
"database/sql"
"errors"
"fmt"
"go-cook/api/models"
"time"
@ -12,8 +13,17 @@ import (
const (
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
func NewUserRepository(conn *sql.DB) UserRepository {
return UserRepository{
@ -37,10 +47,15 @@ func (ur UserRepository) GetByName(name string) (models.UserModel, error) {
return models.UserModel{}, err
}
return ur.processRows(rows)[0], nil
data := ur.processRows(rows)
if (len(data) == 0) {
return models.UserModel{}, errors.New(ErrUserNotFound)
}
return data[0], nil
}
func (ur UserRepository) NewUser(name, password string) (int64, error) {
func (ur UserRepository) Create(name, password string) (int64, error) {
passwordBytes := []byte(password)
hash, err := bcrypt.GenerateFromPassword(passwordBytes, bcrypt.DefaultCost)
if err != nil {
@ -62,6 +77,10 @@ func (ur UserRepository) NewUser(name, password string) (int64, error) {
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 {
_, err := ur.GetByName(name)
if err != nil {

View File

@ -20,7 +20,7 @@ func TestCanCreateNewUser(t *testing.T) {
defer db.Close()
repo := repositories.NewUserRepository(db)
updated, err := repo.NewUser("testing", "NotSecure")
updated, err := repo.Create("testing", "NotSecure")
if err != nil {
log.Println(err)
t.FailNow()