Early stages of the storage classes/decouplers for receipts and such

Signed-off-by: Ethan Wellenreiter <ewellenreiter@gmail.com>
This commit is contained in:
Ethan Wellenreiter 2025-04-30 20:58:14 -04:00
parent aed333d04c
commit 031942c9d9
6 changed files with 67 additions and 16 deletions

View File

@ -0,0 +1,8 @@
package storage
type Image struct {
ID int64 `json:"id"`
ReceiptID string `json:"receipt_id"`
CreatedAt string `json:"created_at"`
Path string `json:"path"`
}

View File

@ -0,0 +1 @@
package storage

View File

@ -0,0 +1,20 @@
package storage
import "time"
type Receipt struct {
ID int64 `json:"id"`
Owner string `json:"username"`
OwnerID int64 `json:"user_id"`
ImageIDs []int64 `json:"image_ids"`
Data ReceiptData `json:"receipt_data"`
}
type ReceiptData struct {
Date time.Time `json:"date"`
Subtotal float64 `json:"subtotal"`
Total float64 `json:"total"`
Tax float64 `json:"tax"`
Items map[string]float64 `json:"items"`
// Currency string `json:"currency"`
}

View File

@ -1,9 +0,0 @@
package storage
import "time"
type SessionStore interface {
AddSession(sessKey string) error
RemoveSession(sessKey string) error
SetLifespan(sessKey string, lf time.Time) error
}

View File

@ -0,0 +1,38 @@
package storage
import (
"context"
"database/sql"
)
type Storage struct {
Receipts interface {
GetByID(context.Context, int64) (*Receipt, error)
}
Users interface {
AddUser(user string, password string) error
RemoveUser(user string, password string) error
UpdateUserPass(user string, oldPassword string, newPass string) error
}
Images interface {
GetByID(context.Context, int64) (*Image, error)
}
}
func NewSQLStorage(db *sql.DB) Storage {
return Storage{}
}
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

@ -1,7 +0,0 @@
package storage
type UserStore interface {
AddUser(user string, password string) error
RemoveUser(user string, password string) error
UpdateUserPass(user string, password string) error
}