Moving and adjusting interfaces and related structs

Signed-off-by: Ethan Wellenreiter <ewellenreiter@gmail.com>
This commit is contained in:
Ethan Wellenreiter 2025-05-05 22:05:34 -04:00
parent 074658f71c
commit 10de7d3749
3 changed files with 62 additions and 31 deletions

View File

@ -0,0 +1,25 @@
package storage
import (
"context"
"database/sql"
"time"
)
var (
QueryTimeoutDuration = time.Second * 5
)
func withTx(db *sql.DB, ctx context.Context, fn func(*sql.Tx) error) error {
tx, err := db.BeginTx(ctx, nil)
if err != nil {
return err
}
if err := fn(tx); err != nil {
_ = tx.Rollback()
return err
}
return tx.Commit()
}

View File

@ -3,9 +3,17 @@ package storage
import (
"context"
"database/sql"
"errors"
"time"
)
var (
ErrNotFound = errors.New("resource not found")
ErrConflict = errors.New("resource already exists")
ErrDuplicateEmail = errors.New("a user with that email already exists")
ErrDuplicateUsername = errors.New("a user with that username already exists")
)
type Storage struct {
Users interface { // store user id, username, password(hashed+salted), role?
GetByID(ctx context.Context, id int64) (*User, error)
@ -18,8 +26,8 @@ type Storage struct {
UpdateUserPass(ctx context.Context, user string, oldPassword string, newPass string) error
CheckPass(ctx context.Context, name string, pass string) (bool, error)
SigninUser(ctx context.Context, name string, pass string) (bool, *User, error)
// CheckPass(ctx context.Context, name string, pass string) (bool, error)
// SigninUser(ctx context.Context, name string, pass string) (bool, *User, error)
// ValidCredentials(ctx context.Context, user *User, pass string) (bool, error)
}
@ -39,18 +47,29 @@ type Storage struct {
Roles interface {
GetByName(context.Context, string) (*Role, error)
GetById(context.Context, int64) (*Role, error)
}
Receipts interface {
GetByID(context.Context, int64) (*Receipt, error)
GetByID(ctx context.Context, id int64) (*Receipt, error)
Create(ctx context.Context, receipt *Receipt) error
Delete(ctx context.Context, id int64) error
}
Images interface {
GetByID(context.Context, int64) (*Image, error)
RemoveByID(context.Context, int64) (*Image, error)
Create(context.Context, *Image) error
Delete(context.Context, int64) (*Image, error)
ConfirmImage(context.Context, int64) error // may need to change this. Consider finishing https://www.youtube.com/watch?v=pmEmQcd9_KA
// Can have it so that the client get's a presigned url
// For creation, do a delayed check in 10 minutes to clean up the image from AWS or check
}
Groups interface {
GetByID(context.Context, int64)
GetUserGroups(context.Context, int64)
GetUsersInGroup(context.Context, int64)
Create(context.Context, *Group)
Delete(context.Context, int64)
}
}
@ -59,17 +78,3 @@ func NewSQLRedisMinIOStorage(db *sql.DB) Storage {
Users: &SQLUsersStore{db},
}
}
func withTx(db *sql.DB, ctx context.Context, fn func(*sql.Tx) error) error {
tx, err := db.BeginTx(ctx, nil)
if err != nil {
return err
}
if err := fn(tx); err != nil {
_ = tx.Rollback()
return err
}
return tx.Commit()
}

View File

@ -11,18 +11,19 @@ var (
)
type User struct {
ID int64 `json:"id"`
Username string `json:"username"`
Email string `json:"email"`
Password Password `json:"-"`
CreatedAt string `json:"created_at"`
IsActive bool `json:"is_active"`
Role Role `json:"role"`
Groups []int64 `json:"groups"`
ID int64 `json:"id"`
Username string `json:"username"`
Email string `json:"email"`
Password string `json:"-"`
CreatedAt string `json:"created_at"`
IsActive bool `json:"is_active"`
Role Role `json:"role"`
PersonalGroup int64 `json:"user_group"`
Groups []int64 `json:"groups"`
}
type Password struct {
text *string
hash []byte
encoded *string
}
// type Password struct {
// text *string
// hash []byte
// encoded *string
// }