diff --git a/backend/internal/storage/sql.go b/backend/internal/storage/sql.go new file mode 100644 index 0000000..74690cf --- /dev/null +++ b/backend/internal/storage/sql.go @@ -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() +} diff --git a/backend/internal/storage/storage.go b/backend/internal/storage/storage.go index ca533a5..9456de3 100644 --- a/backend/internal/storage/storage.go +++ b/backend/internal/storage/storage.go @@ -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() -} diff --git a/backend/internal/storage/users.go b/backend/internal/storage/users.go index dcc609b..52a5d48 100644 --- a/backend/internal/storage/users.go +++ b/backend/internal/storage/users.go @@ -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 +// }