Compare commits
4 Commits
3bf0d93af2
...
2b2fa217d6
| Author | SHA1 | Date | |
|---|---|---|---|
| 2b2fa217d6 | |||
| 064faeadca | |||
| a99ef367c7 | |||
| 29bf0bec88 |
4
backend/internal/storage/cache/cache.go
vendored
4
backend/internal/storage/cache/cache.go
vendored
@ -34,8 +34,8 @@ type Storage struct {
|
||||
Delete(ctx context.Context, name string)
|
||||
}
|
||||
UserGroups interface {
|
||||
Get(ctx context.Context, userid int64) (*[]int64, error)
|
||||
Set(ctx context.Context, groups *[]int64) error
|
||||
Get(ctx context.Context, userid int64) (*storage.UserGroups, error)
|
||||
Set(ctx context.Context, groups *storage.UserGroups) error
|
||||
Delete(ctx context.Context, userid int64)
|
||||
}
|
||||
Receipts interface {
|
||||
|
||||
47
backend/internal/storage/cache/redis_roles.go
vendored
Normal file
47
backend/internal/storage/cache/redis_roles.go
vendored
Normal file
@ -0,0 +1,47 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"git.ewellenr.ca/receipt_indexer/backend/internal/storage"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
type RolesStore struct {
|
||||
rdb *redis.Client
|
||||
}
|
||||
|
||||
func (s *RolesStore) Get(ctx context.Context, name string) (*storage.Role, error) {
|
||||
|
||||
data, err := s.rdb.Get(ctx, name).Result()
|
||||
if err == redis.Nil {
|
||||
return nil, nil
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var role storage.Role
|
||||
if data != "" {
|
||||
err := json.Unmarshal([]byte(data), &role)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return &role, nil
|
||||
}
|
||||
|
||||
func (s *RolesStore) Set(ctx context.Context, role *storage.Role) error {
|
||||
|
||||
json, err := json.Marshal(role)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return s.rdb.Set(ctx, role.Name, json, RolesExpTime).Err()
|
||||
}
|
||||
|
||||
func (s *RolesStore) Delete(ctx context.Context, name string) {
|
||||
s.rdb.Del(ctx, name)
|
||||
}
|
||||
4
backend/internal/storage/cache/redis_user.go
vendored
4
backend/internal/storage/cache/redis_user.go
vendored
@ -4,9 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
// auth_storage "git.ewellenr.ca/receipt_indexer/backend/internal/storage/auth"
|
||||
"git.ewellenr.ca/receipt_indexer/backend/internal/storage"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
@ -15,8 +13,6 @@ type UserStore struct {
|
||||
rdb *redis.Client
|
||||
}
|
||||
|
||||
const UserExpTime = time.Minute
|
||||
|
||||
func (s *UserStore) Get(ctx context.Context, userID int64) (*storage.User, error) {
|
||||
cacheKey := fmt.Sprintf("user-%d", userID)
|
||||
|
||||
|
||||
51
backend/internal/storage/cache/redis_usergroups.go
vendored
Normal file
51
backend/internal/storage/cache/redis_usergroups.go
vendored
Normal file
@ -0,0 +1,51 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"git.ewellenr.ca/receipt_indexer/backend/internal/storage"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
type UserGroupsStore struct {
|
||||
rdb *redis.Client
|
||||
}
|
||||
|
||||
func (s *UserGroupsStore) Get(ctx context.Context, userid int64) (*storage.UserGroups, error) {
|
||||
cacheKey := fmt.Sprintf("user-%d", userid)
|
||||
|
||||
data, err := s.rdb.Get(ctx, cacheKey).Result()
|
||||
if err == redis.Nil {
|
||||
return nil, nil
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var usergroups storage.UserGroups
|
||||
if data != "" {
|
||||
err := json.Unmarshal([]byte(data), &usergroups)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return &usergroups, nil
|
||||
}
|
||||
|
||||
func (s *UserGroupsStore) Set(ctx context.Context, groups *storage.UserGroups) error {
|
||||
cacheKey := fmt.Sprintf("user-%d", groups.UserID)
|
||||
|
||||
json, err := json.Marshal(groups)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return s.rdb.Set(ctx, cacheKey, json, RolesExpTime).Err()
|
||||
}
|
||||
|
||||
func (s *UserGroupsStore) Delete(ctx context.Context, userid int64) {
|
||||
cacheKey := fmt.Sprintf("user-%d", userid)
|
||||
s.rdb.Del(ctx, cacheKey)
|
||||
}
|
||||
@ -7,3 +7,8 @@ type Group struct {
|
||||
Owner int64 `json:"owner"`
|
||||
Moderators []int64 `json:"moderators"`
|
||||
}
|
||||
|
||||
type UserGroups struct {
|
||||
UserID int64 `json:"user_id"`
|
||||
GroupIDs []int64 `json:"group_ids"`
|
||||
}
|
||||
|
||||
@ -48,9 +48,11 @@ func (s *SQLGroupsStore) GetByID(ctx context.Context, id int64) (*Group, error)
|
||||
return s.getByID(ctx, id)
|
||||
}
|
||||
|
||||
func (s *SQLGroupsStore) GetUserGroups(ctx context.Context, userID int64) ([]int64, error) {
|
||||
func (s *SQLGroupsStore) GetUserGroups(ctx context.Context, userID int64) (*UserGroups, error) {
|
||||
// Implement logic to retrieve user's groups from the database
|
||||
var groups []int64
|
||||
usergroups := &UserGroups{
|
||||
UserID: userID,
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(ctx, QueryTimeoutDuration)
|
||||
defer cancel()
|
||||
@ -68,9 +70,9 @@ func (s *SQLGroupsStore) GetUserGroups(ctx context.Context, userID int64) ([]int
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
groups = append(groups, groupid)
|
||||
usergroups.GroupIDs = append(usergroups.GroupIDs, groupid)
|
||||
}
|
||||
return groups, nil
|
||||
return usergroups, nil
|
||||
}
|
||||
|
||||
// func (s *SQLGroupsStore) GetUsersInGroup(ctx context.Context, groupId int64) ([]*User, error) {
|
||||
|
||||
@ -66,7 +66,7 @@ type Storage struct {
|
||||
}
|
||||
Groups interface {
|
||||
GetByID(context.Context, int64) (*Group, error)
|
||||
GetUserGroups(context.Context, int64) ([]int64, error)
|
||||
GetUserGroups(context.Context, int64) (*UserGroups, error)
|
||||
// GetUsersInGroup(context.Context, int64) ([]*User, error)
|
||||
Create(context.Context, *Group) error
|
||||
Delete(context.Context, int64) error
|
||||
|
||||
Loading…
Reference in New Issue
Block a user