refactored for domain
This commit is contained in:
parent
09235760d2
commit
6af8a2a0cd
@ -3,15 +3,15 @@ package repositories
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"go-cook/api/models"
|
||||
"go-cook/api/domain"
|
||||
)
|
||||
|
||||
type IRecipeTable interface {
|
||||
Create(models.RecipeModel) error
|
||||
List() ([]models.RecipeModel, error)
|
||||
Get(id int) (models.RecipeModel, error)
|
||||
Update(id int, entity models.RecipeModel) error
|
||||
Delete(id int) error
|
||||
Create(domain.RecipeEntity) error
|
||||
List() ([]domain.RecipeEntity, error)
|
||||
Get(id int) (domain.RecipeEntity, error)
|
||||
Update(id int, entity domain.RecipeEntity) error
|
||||
Delete(id int) error
|
||||
}
|
||||
|
||||
type RecipeRepository struct {
|
||||
@ -24,22 +24,22 @@ 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")
|
||||
}
|
||||
|
||||
func (rr RecipeRepository) List() ([]models.RecipeModel, error) {
|
||||
return []models.RecipeModel{}, errors.New("not implemented")
|
||||
func (rr RecipeRepository) List() ([]domain.RecipeEntity, error) {
|
||||
return []domain.RecipeEntity{}, errors.New("not implemented")
|
||||
}
|
||||
|
||||
func (rr RecipeRepository) Get(id int) (models.RecipeModel, error) {
|
||||
return models.RecipeModel{}, errors.New("not implemented")
|
||||
func (rr RecipeRepository) Get(id int) (domain.RecipeEntity, error) {
|
||||
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")
|
||||
}
|
||||
|
||||
func (rr RecipeRepository) Delete(id int) error {
|
||||
return errors.New("not implemented")
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"go-cook/api/models"
|
||||
"go-cook/api/domain"
|
||||
"time"
|
||||
|
||||
"github.com/huandu/go-sqlbuilder"
|
||||
@ -12,16 +12,17 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
TableName string = "users"
|
||||
TableName string = "users"
|
||||
ErrUserNotFound string = "requested user was not found"
|
||||
)
|
||||
|
||||
type IUserTable interface {
|
||||
GetByName(name string) (models.UserModel, error)
|
||||
GetByName(name string) (domain.UserEntity, 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
|
||||
CheckUserHash(name, password string) error
|
||||
AddScope(name, scope string) error
|
||||
}
|
||||
|
||||
// Creates a new instance of UserRepository with the bound sql
|
||||
@ -35,7 +36,7 @@ type UserRepository struct {
|
||||
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.Select("*").From("users").Where(
|
||||
builder.E("Name", name),
|
||||
@ -44,12 +45,12 @@ func (ur UserRepository) GetByName(name string) (models.UserModel, error) {
|
||||
|
||||
rows, err := ur.connection.Query(query, args...)
|
||||
if err != nil {
|
||||
return models.UserModel{}, err
|
||||
return domain.UserEntity{}, err
|
||||
}
|
||||
|
||||
data := ur.processRows(rows)
|
||||
if (len(data) == 0) {
|
||||
return models.UserModel{}, errors.New(ErrUserNotFound)
|
||||
if len(data) == 0 {
|
||||
return domain.UserEntity{}, errors.New(ErrUserNotFound)
|
||||
}
|
||||
|
||||
return data[0], nil
|
||||
@ -77,7 +78,7 @@ func (ur UserRepository) Create(name, password string) (int64, error) {
|
||||
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")
|
||||
}
|
||||
|
||||
@ -109,8 +110,26 @@ func (ur UserRepository) CheckUserHash(name, password string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ur UserRepository) processRows(rows *sql.Rows) []models.UserModel {
|
||||
items := []models.UserModel{}
|
||||
func (ur UserRepository) AddScope(name, scope string) error {
|
||||
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() {
|
||||
var id int
|
||||
@ -118,15 +137,17 @@ func (ur UserRepository) processRows(rows *sql.Rows) []models.UserModel {
|
||||
var hash string
|
||||
var createdAt 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 {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
items = append(items, models.UserModel{
|
||||
items = append(items, domain.UserEntity{
|
||||
Id: id,
|
||||
Name: name,
|
||||
Hash: hash,
|
||||
Scopes: scopes,
|
||||
CreatedAt: createdAt,
|
||||
LastUpdated: lastUpdated,
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user