You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.1 KiB
Go

package store
import (
"context"
storepb "github.com/yourselfhosted/slash/proto/gen/store"
)
type UpdateShortcut struct {
ID int32
RowStatus *RowStatus
Name *string
Link *string
Title *string
Description *string
Visibility *Visibility
Tag *string
OpenGraphMetadata *storepb.OpenGraphMetadata
}
type FindShortcut struct {
ID *int32
CreatorID *int32
RowStatus *RowStatus
Name *string
VisibilityList []Visibility
Tag *string
}
type DeleteShortcut struct {
ID int32
}
func (s *Store) CreateShortcut(ctx context.Context, create *storepb.Shortcut) (*storepb.Shortcut, error) {
shortcut, err := s.driver.CreateShortcut(ctx, create)
if err != nil {
return nil, err
}
s.shortcutCache.Store(shortcut.Id, shortcut)
return shortcut, nil
}
func (s *Store) UpdateShortcut(ctx context.Context, update *UpdateShortcut) (*storepb.Shortcut, error) {
shortcut, err := s.driver.UpdateShortcut(ctx, update)
if err != nil {
return nil, err
}
s.shortcutCache.Store(shortcut.Id, shortcut)
return shortcut, nil
}
func (s *Store) ListShortcuts(ctx context.Context, find *FindShortcut) ([]*storepb.Shortcut, error) {
list, err := s.driver.ListShortcuts(ctx, find)
if err != nil {
return nil, err
}
for _, shortcut := range list {
s.shortcutCache.Store(shortcut.Id, shortcut)
}
return list, nil
}
func (s *Store) GetShortcut(ctx context.Context, find *FindShortcut) (*storepb.Shortcut, error) {
if find.ID != nil {
if cache, ok := s.shortcutCache.Load(*find.ID); ok {
return cache.(*storepb.Shortcut), nil
}
}
shortcuts, err := s.ListShortcuts(ctx, find)
if err != nil {
return nil, err
}
if len(shortcuts) == 0 {
return nil, nil
}
shortcut := shortcuts[0]
s.shortcutCache.Store(shortcut.Id, shortcut)
return shortcut, nil
}
func (s *Store) DeleteShortcut(ctx context.Context, delete *DeleteShortcut) error {
if err := s.driver.DeleteShortcut(ctx, delete); err != nil {
return err
}
s.shortcutCache.Delete(delete.ID)
return nil
}