2024-04-26 16:03:32 -07:00
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
2024-05-05 10:02:17 -07:00
|
|
|
"context"
|
2024-04-26 16:03:32 -07:00
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.jamestombleson.com/jtom38/newsbot-api/internal/domain"
|
|
|
|
|
|
|
|
"github.com/huandu/go-sqlbuilder"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
TableName string = "users"
|
|
|
|
ErrUserNotFound string = "requested user was not found"
|
|
|
|
)
|
|
|
|
|
2024-04-28 11:39:25 -07:00
|
|
|
type Users interface {
|
2024-05-05 10:02:17 -07:00
|
|
|
GetByName(ctx context.Context, name string) (domain.UserEntity, error)
|
|
|
|
Create(ctx context.Context, name, password, scope string) (int64, error)
|
|
|
|
Update(ctx context.Context, id int, entity domain.UserEntity) error
|
|
|
|
UpdatePassword(ctx context.Context, name, password string) error
|
|
|
|
CheckUserHash(ctx context.Context, name, password string) error
|
|
|
|
UpdateScopes(ctx context.Context, name, scope string) error
|
2024-04-26 16:03:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a new instance of UserRepository with the bound sql
|
2024-04-28 11:39:25 -07:00
|
|
|
func NewUserRepository(conn *sql.DB) userRepository {
|
|
|
|
return userRepository{
|
2024-04-26 16:03:32 -07:00
|
|
|
connection: conn,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-28 11:39:25 -07:00
|
|
|
type userRepository struct {
|
2024-04-26 16:03:32 -07:00
|
|
|
connection *sql.DB
|
|
|
|
}
|
|
|
|
|
2024-05-05 10:02:17 -07:00
|
|
|
func (ur userRepository) GetByName(ctx context.Context, name string) (domain.UserEntity, error) {
|
2024-04-26 16:03:32 -07:00
|
|
|
builder := sqlbuilder.NewSelectBuilder()
|
|
|
|
builder.Select("*").From("users").Where(
|
|
|
|
builder.E("Name", name),
|
|
|
|
)
|
|
|
|
query, args := builder.Build()
|
|
|
|
|
2024-05-05 10:02:17 -07:00
|
|
|
rows, err := ur.connection.QueryContext(ctx, query, args...)
|
2024-04-26 16:03:32 -07:00
|
|
|
if err != nil {
|
|
|
|
return domain.UserEntity{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
data := ur.processRows(rows)
|
|
|
|
if len(data) == 0 {
|
|
|
|
return domain.UserEntity{}, errors.New(ErrUserNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
return data[0], nil
|
|
|
|
}
|
|
|
|
|
2024-05-05 10:02:17 -07:00
|
|
|
func (ur userRepository) Create(ctx context.Context,name, password, scope string) (int64, error) {
|
2024-04-26 16:03:32 -07:00
|
|
|
passwordBytes := []byte(password)
|
|
|
|
hash, err := bcrypt.GenerateFromPassword(passwordBytes, bcrypt.DefaultCost)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
dt := time.Now()
|
|
|
|
queryBuilder := sqlbuilder.NewInsertBuilder()
|
|
|
|
queryBuilder.InsertInto("users")
|
2024-05-05 10:02:17 -07:00
|
|
|
queryBuilder.Cols("Name", "Hash", "UpdatedAt", "CreatedAt", "DeletedAt", "Scopes")
|
|
|
|
queryBuilder.Values(name, string(hash), dt, dt, time.Time{}, scope)
|
2024-04-26 16:03:32 -07:00
|
|
|
query, args := queryBuilder.Build()
|
|
|
|
|
2024-05-05 10:02:17 -07:00
|
|
|
_, err = ur.connection.ExecContext(ctx, query, args...)
|
2024-04-26 16:03:32 -07:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1, nil
|
|
|
|
}
|
|
|
|
|
2024-05-05 10:02:17 -07:00
|
|
|
func (ur userRepository) Update(ctx context.Context, id int, entity domain.UserEntity) error {
|
2024-04-26 16:03:32 -07:00
|
|
|
return errors.New("not implemented")
|
|
|
|
}
|
|
|
|
|
2024-05-05 10:02:17 -07:00
|
|
|
func (ur userRepository) UpdatePassword(ctx context.Context, name, password string) error {
|
|
|
|
_, err := ur.GetByName(ctx, name)
|
2024-04-26 16:03:32 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
queryBuilder := sqlbuilder.NewUpdateBuilder()
|
|
|
|
queryBuilder.Update(TableName)
|
|
|
|
//queryBuilder.Set
|
|
|
|
return 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
|
2024-05-05 10:02:17 -07:00
|
|
|
func (ur userRepository) CheckUserHash(ctx context.Context,name, password string) error {
|
|
|
|
record, err := ur.GetByName(ctx, name)
|
2024-04-26 16:03:32 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = bcrypt.CompareHashAndPassword([]byte(record.Hash), []byte(password))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-05 10:02:17 -07:00
|
|
|
func (ur userRepository) UpdateScopes(ctx context.Context,name, scope string) error {
|
2024-04-26 16:03:32 -07:00
|
|
|
builder := sqlbuilder.NewUpdateBuilder()
|
|
|
|
builder.Update("users")
|
|
|
|
builder.Set(
|
|
|
|
builder.Assign("Scopes", scope),
|
|
|
|
)
|
|
|
|
builder.Where(
|
|
|
|
builder.Equal("Name", name),
|
|
|
|
)
|
|
|
|
query, args := builder.Build()
|
|
|
|
|
2024-05-05 10:02:17 -07:00
|
|
|
_, err := ur.connection.ExecContext(ctx, query, args...)
|
2024-04-26 16:03:32 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-28 11:39:25 -07:00
|
|
|
func (ur userRepository) processRows(rows *sql.Rows) []domain.UserEntity {
|
2024-04-26 16:03:32 -07:00
|
|
|
items := []domain.UserEntity{}
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var id int64
|
|
|
|
var username string
|
|
|
|
var hash string
|
|
|
|
var createdAt time.Time
|
|
|
|
var updatedAt time.Time
|
|
|
|
var deletedAt sql.NullTime
|
|
|
|
var scopes string
|
|
|
|
err := rows.Scan(&id, &createdAt, &updatedAt, &deletedAt, &username, &hash, &scopes)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
item := domain.UserEntity{
|
|
|
|
ID: id,
|
|
|
|
UpdatedAt: updatedAt,
|
|
|
|
Username: username,
|
|
|
|
Hash: hash,
|
|
|
|
Scopes: scopes,
|
|
|
|
CreatedAt: createdAt,
|
|
|
|
}
|
|
|
|
if deletedAt.Valid {
|
|
|
|
item.DeletedAt = deletedAt.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
items = append(items, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
return items
|
|
|
|
}
|