features/working-on-scopes #13

Merged
jtom38 merged 28 commits from features/working-on-scopes into main 2024-04-04 15:31:54 -07:00
2 changed files with 47 additions and 26 deletions
Showing only changes of commit 6af8a2a0cd - Show all commits

View File

@ -3,14 +3,14 @@ package repositories
import ( import (
"database/sql" "database/sql"
"errors" "errors"
"go-cook/api/models" "go-cook/api/domain"
) )
type IRecipeTable interface { type IRecipeTable interface {
Create(models.RecipeModel) error Create(domain.RecipeEntity) error
List() ([]models.RecipeModel, error) List() ([]domain.RecipeEntity, error)
Get(id int) (models.RecipeModel, error) Get(id int) (domain.RecipeEntity, error)
Update(id int, entity models.RecipeModel) error Update(id int, entity domain.RecipeEntity) error
Delete(id int) error Delete(id int) error
} }
@ -24,19 +24,19 @@ func NewRecipeRepository(client *sql.DB) RecipeRepository {
} }
} }
func (rr RecipeRepository) Create(models.RecipeModel) error { func (rr RecipeRepository) Create(domain.RecipeEntity) error {
return errors.New("not implemented") return errors.New("not implemented")
} }
func (rr RecipeRepository) List() ([]models.RecipeModel, error) { func (rr RecipeRepository) List() ([]domain.RecipeEntity, error) {
return []models.RecipeModel{}, errors.New("not implemented") return []domain.RecipeEntity{}, errors.New("not implemented")
} }
func (rr RecipeRepository) Get(id int) (models.RecipeModel, error) { func (rr RecipeRepository) Get(id int) (domain.RecipeEntity, error) {
return models.RecipeModel{}, errors.New("not implemented") return domain.RecipeEntity{}, errors.New("not implemented")
} }
func (rr RecipeRepository) Update(id int, entity models.RecipeModel) error { func (rr RecipeRepository) Update(id int, entity domain.RecipeEntity) error {
return errors.New("not implemented") return errors.New("not implemented")
} }

View File

@ -4,7 +4,7 @@ import (
"database/sql" "database/sql"
"errors" "errors"
"fmt" "fmt"
"go-cook/api/models" "go-cook/api/domain"
"time" "time"
"github.com/huandu/go-sqlbuilder" "github.com/huandu/go-sqlbuilder"
@ -12,16 +12,17 @@ import (
) )
const ( const (
TableName string = "users" TableName string = "users"
ErrUserNotFound string = "requested user was not found" ErrUserNotFound string = "requested user was not found"
) )
type IUserTable interface { type IUserTable interface {
GetByName(name string) (models.UserModel, error) GetByName(name string) (domain.UserEntity, error)
Create(name, password string) (int64, error) Create(name, password string) (int64, error)
Update(id int, entity models.UserModel) error Update(id int, entity domain.UserEntity) error
UpdatePassword(name, password string) error UpdatePassword(name, password string) error
CheckUserHash(name, password string) error CheckUserHash(name, password string) error
AddScope(name, scope string) error
} }
// Creates a new instance of UserRepository with the bound sql // Creates a new instance of UserRepository with the bound sql
@ -35,7 +36,7 @@ type UserRepository struct {
connection *sql.DB connection *sql.DB
} }
func (ur UserRepository) GetByName(name string) (models.UserModel, error) { func (ur UserRepository) GetByName(name string) (domain.UserEntity, 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),
@ -44,12 +45,12 @@ func (ur UserRepository) GetByName(name string) (models.UserModel, error) {
rows, err := ur.connection.Query(query, args...) rows, err := ur.connection.Query(query, args...)
if err != nil { if err != nil {
return models.UserModel{}, err return domain.UserEntity{}, err
} }
data := ur.processRows(rows) data := ur.processRows(rows)
if (len(data) == 0) { if len(data) == 0 {
return models.UserModel{}, errors.New(ErrUserNotFound) return domain.UserEntity{}, errors.New(ErrUserNotFound)
} }
return data[0], nil return data[0], nil
@ -77,7 +78,7 @@ func (ur UserRepository) Create(name, password string) (int64, error) {
return 1, nil return 1, nil
} }
func (ur UserRepository) Update(id int, entity models.UserModel) error { func (ur UserRepository) Update(id int, entity domain.UserEntity) error {
return errors.New("not implemented") return errors.New("not implemented")
} }
@ -109,8 +110,26 @@ func (ur UserRepository) CheckUserHash(name, password string) error {
return nil return nil
} }
func (ur UserRepository) processRows(rows *sql.Rows) []models.UserModel { func (ur UserRepository) AddScope(name, scope string) error {
items := []models.UserModel{} builder := sqlbuilder.NewUpdateBuilder()
builder.Update("users")
builder.Set (
builder.Assign("Scopes", scope),
)
builder.Where(
builder.Equal("Name", name),
)
query, args := builder.Build()
_, err := ur.connection.Exec(query, args...)
if err != nil {
return err
}
return nil
}
func (ur UserRepository) processRows(rows *sql.Rows) []domain.UserEntity {
items := []domain.UserEntity{}
for rows.Next() { for rows.Next() {
var id int var id int
@ -118,15 +137,17 @@ func (ur UserRepository) processRows(rows *sql.Rows) []models.UserModel {
var hash string var hash string
var createdAt time.Time var createdAt time.Time
var lastUpdated time.Time var lastUpdated time.Time
err := rows.Scan(&id, &name, &hash, &createdAt, &lastUpdated) var scopes string
err := rows.Scan(&id, &name, &hash, &createdAt, &lastUpdated, &scopes)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
items = append(items, models.UserModel{ items = append(items, domain.UserEntity{
Id: id, Id: id,
Name: name, Name: name,
Hash: hash, Hash: hash,
Scopes: scopes,
CreatedAt: createdAt, CreatedAt: createdAt,
LastUpdated: lastUpdated, LastUpdated: lastUpdated,
}) })