81 lines
3.1 KiB
Go
81 lines
3.1 KiB
Go
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)
|
|
GetByEmail(context.Context, string) (*User, error)
|
|
GetByUsername(context.Context, string) (*User, error)
|
|
Create(context.Context, *User) error // create a non-exported create function which does take in the tx
|
|
CreateAndInvite(ctx context.Context, user *User, token string, exp time.Duration) error // figure this out
|
|
Activate(context.Context, string) error // what does this do?
|
|
Delete(ctx context.Context, id int64) error
|
|
|
|
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)
|
|
// ValidCredentials(ctx context.Context, user *User, pass string) (bool, error)
|
|
}
|
|
|
|
Sessions interface { // store just session tokens, and their corresponding user id
|
|
AddSession(ctx context.Context, userid int64) (token string, err error)
|
|
GetSession(ctx context.Context, token string) (valid bool, userid int64, err error) // extends it's expiry
|
|
RemoveSession(ctx context.Context, token string) error
|
|
// SetLifespan(ctx context.Context, token string, lf time.Time) error
|
|
}
|
|
|
|
CSRF interface {
|
|
AddCSRF(ctx context.Context, sessionToken string) (csrftoken string, err error)
|
|
CheckCSRF(ctx context.Context, sessionToken string, csrfToken string) (bool, error)
|
|
RemoveCSRF(ctx context.Context, sessionToken string) error
|
|
// CleanupCSRF()
|
|
}
|
|
|
|
Roles interface {
|
|
GetByName(context.Context, string) (*Role, error)
|
|
GetById(context.Context, int64) (*Role, error)
|
|
}
|
|
|
|
Receipts interface {
|
|
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)
|
|
Create(context.Context, *Image) error
|
|
Delete(context.Context, int64) error
|
|
ActivateImage(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) (*Group, error)
|
|
GetUserGroups(context.Context, int64) ([]*Group, error)
|
|
GetUsersInGroup(context.Context, int64) ([]*User, error)
|
|
Create(context.Context, *Group) error
|
|
Delete(context.Context, int64) error
|
|
}
|
|
}
|
|
|
|
func NewSQLRedisMinIOStorage(db *sql.DB) Storage {
|
|
return Storage{
|
|
Users: &SQLUsersStore{db},
|
|
}
|
|
}
|