s3-share/internal/db/mock.go
2022-06-03 14:27:40 +02:00

55 lines
1.0 KiB
Go

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 {
return types.ErrShareNotFound
}
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
}