2022-06-01 09:55:33 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"git.kapelle.org/niklas/s3share/internal/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type mockDB struct {
|
|
|
|
shares map[string]*types.Share
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMock() DB {
|
|
|
|
return &mockDB{
|
|
|
|
shares: make(map[string]*types.Share),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *mockDB) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *mockDB) CreateShare(ctx context.Context, share *types.Share) error {
|
|
|
|
if d.shares[share.Slug] != nil {
|
|
|
|
return errors.New("share already exists")
|
|
|
|
}
|
|
|
|
d.shares[share.Slug] = share
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *mockDB) DeleteShare(ctx context.Context, slug string) error {
|
|
|
|
if d.shares[slug] == nil {
|
2022-06-03 12:27:40 +00:00
|
|
|
return types.ErrShareNotFound
|
2022-06-01 09:55:33 +00:00
|
|
|
}
|
|
|
|
delete(d.shares, slug)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *mockDB) GetAllShares(ctx context.Context) ([]*types.Share, error) {
|
|
|
|
// convert map to slice
|
|
|
|
shares := make([]*types.Share, 0, len(d.shares))
|
|
|
|
for _, share := range d.shares {
|
|
|
|
shares = append(shares, share)
|
|
|
|
}
|
|
|
|
return shares, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *mockDB) GetShare(ctx context.Context, slug string) (*types.Share, error) {
|
|
|
|
if d.shares[slug] == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return d.shares[slug], nil
|
|
|
|
}
|