move DefaultRepository into own file; add todo tags;
This commit is contained in:
78
internal/data/DefaultRepository.go
Normal file
78
internal/data/DefaultRepository.go
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
package data
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
// TODO docstring
|
||||||
|
// Default implementation of IRepository
|
||||||
|
type DefaultRepository[T IIdentifiable] struct {
|
||||||
|
ds IDatastore
|
||||||
|
prefix string
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO docstring
|
||||||
|
func NewDefaultRepository[T IIdentifiable](ds IDatastore, prefix string) *DefaultRepository[T] {
|
||||||
|
return &DefaultRepository[T]{ ds: ds, prefix: prefix }
|
||||||
|
}
|
||||||
|
|
||||||
|
// Creates a new entry in the repository with the given object t. Throws an
|
||||||
|
// error if there already exists an entry with the same id, the json encoding
|
||||||
|
// fails or the connection to the IDatastore fails.
|
||||||
|
func (repo *DefaultRepository[T]) Create(t T) error {
|
||||||
|
|
||||||
|
key := repo.prefix + ":" + strconv.FormatUint(uint64(t.Id()), 10)
|
||||||
|
exists, err := repo.ds.KeyExists(key)
|
||||||
|
if err != nil { return err }
|
||||||
|
if exists { return errors.New("key already exits") }
|
||||||
|
|
||||||
|
d, err := json.Marshal(t)
|
||||||
|
if err != nil { return err }
|
||||||
|
|
||||||
|
err = repo.ds.Set(key, string(d))
|
||||||
|
if err != nil { return err }
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Updates the entry with the same id as t in the repository with the values of
|
||||||
|
// t. Trows an error if the json encoding fails or the connection to the
|
||||||
|
// IDatastore fails.
|
||||||
|
func (repo *DefaultRepository[T]) Update(t T) error {
|
||||||
|
|
||||||
|
key := repo.prefix + ":" + strconv.FormatUint(uint64(t.Id()), 10)
|
||||||
|
exists, err := repo.ds.KeyExists(key)
|
||||||
|
if err != nil { return err }
|
||||||
|
if !exists { return errors.New("key does not exits") }
|
||||||
|
|
||||||
|
d, err := json.Marshal(t)
|
||||||
|
if err != nil { return err }
|
||||||
|
|
||||||
|
err = repo.ds.Set(key, string(d))
|
||||||
|
if err != nil { return err }
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (repo *DefaultRepository[T]) Delete(t T) error {
|
||||||
|
// TODO
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (repo *DefaultRepository[T]) GetAll() ([]T, error) {
|
||||||
|
// TODO
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (repo *DefaultRepository[T]) GetById(id uint) (T, error) {
|
||||||
|
// TODO
|
||||||
|
return *new(T), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (repo *DefaultRepository[T]) GetByCriteria(c ISearchCriteria[T]) ([]T, error) {
|
||||||
|
// TODO
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
@@ -7,13 +7,13 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
// A very simple datastructure, implementing the IDatastore interface. It uses
|
// A very simple datastructure, implementing the IDatastore interface. It uses
|
||||||
// a simple text file to the data as json.
|
// a simple text file to the data as json.
|
||||||
type FileDatastore struct {
|
type FileDatastore struct {
|
||||||
path string
|
path string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Creates a new FileDatastore object, creating the storage file in the
|
// Creates a new FileDatastore object, creating the storage file in the
|
||||||
// process.
|
// process.
|
||||||
func NewFileDatastore(path string) (*FileDatastore, error) {
|
func NewFileDatastore(path string) (*FileDatastore, error) {
|
||||||
@@ -120,7 +120,6 @@ func (fds *FileDatastore) GetAllKeys() (map[string]bool, error) {
|
|||||||
// Deletes the entry with the given key. May through an error if the file
|
// Deletes the entry with the given key. May through an error if the file
|
||||||
// cannot be opened or the contents cannot be decoded or encoded correctly.
|
// cannot be opened or the contents cannot be decoded or encoded correctly.
|
||||||
func (fds *FileDatastore) Delete(key string) error {
|
func (fds *FileDatastore) Delete(key string) error {
|
||||||
|
|
||||||
m, err := fds.readMapObj()
|
m, err := fds.readMapObj()
|
||||||
if err != nil { return err }
|
if err != nil { return err }
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package data
|
package data
|
||||||
|
|
||||||
|
// TODO docstring
|
||||||
type IDatastore interface {
|
type IDatastore interface {
|
||||||
Set(key string, val string) error
|
Set(key string, val string) error
|
||||||
KeyExists(key string) (bool, error)
|
KeyExists(key string) (bool, error)
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package data
|
package data
|
||||||
|
|
||||||
|
// TODO docstring
|
||||||
type IIdentifiable interface {
|
type IIdentifiable interface {
|
||||||
Id() uint
|
Id() uint
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,5 @@
|
|||||||
package data
|
package data
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"strconv"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
// An interface to manage generic structure objects persistently. Should use an
|
// An interface to manage generic structure objects persistently. Should use an
|
||||||
// IDatastore as the interface that actually stores and retrieves the data from
|
// IDatastore as the interface that actually stores and retrieves the data from
|
||||||
// an external source.
|
// an external source.
|
||||||
@@ -18,73 +11,3 @@ type IRepository[T IIdentifiable] interface {
|
|||||||
GetById(id uint) (T, error)
|
GetById(id uint) (T, error)
|
||||||
GetByCriteria(c ISearchCriteria[T]) ([]T, error)
|
GetByCriteria(c ISearchCriteria[T]) ([]T, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// --- default implementation ---
|
|
||||||
|
|
||||||
type DefaultRepository[T IIdentifiable] struct {
|
|
||||||
ds IDatastore
|
|
||||||
prefix string
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewDefaultRepository[T IIdentifiable](ds IDatastore, prefix string) *DefaultRepository[T] {
|
|
||||||
return &DefaultRepository[T]{ ds: ds, prefix: prefix }
|
|
||||||
}
|
|
||||||
|
|
||||||
// Creates a new entry in the repository with the given object t. Throws an
|
|
||||||
// error if there already exists an entry with the same id, the json encoding
|
|
||||||
// fails or the connection to the IDatastore fails.
|
|
||||||
func (repo *DefaultRepository[T]) Create(t T) error {
|
|
||||||
|
|
||||||
key := repo.prefix + ":" + strconv.FormatUint(uint64(t.Id()), 10)
|
|
||||||
exists, err := repo.ds.KeyExists(key)
|
|
||||||
if err != nil { return err }
|
|
||||||
if exists { return errors.New("key already exits") }
|
|
||||||
|
|
||||||
d, err := json.Marshal(t)
|
|
||||||
if err != nil { return err }
|
|
||||||
|
|
||||||
err = repo.ds.Set(key, string(d))
|
|
||||||
if err != nil { return err }
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Updates the entry with the same id as t in the repository with the values of
|
|
||||||
// t. Trows an error if the json encoding fails or the connection to the
|
|
||||||
// IDatastore fails.
|
|
||||||
func (repo *DefaultRepository[T]) Update(t T) error {
|
|
||||||
|
|
||||||
key := repo.prefix + ":" + strconv.FormatUint(uint64(t.Id()), 10)
|
|
||||||
exists, err := repo.ds.KeyExists(key)
|
|
||||||
if err != nil { return err }
|
|
||||||
if !exists { return errors.New("key does not exits") }
|
|
||||||
|
|
||||||
d, err := json.Marshal(t)
|
|
||||||
if err != nil { return err }
|
|
||||||
|
|
||||||
err = repo.ds.Set(key, string(d))
|
|
||||||
if err != nil { return err }
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (repo *DefaultRepository[T]) Delete(t T) error {
|
|
||||||
// TODO
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (repo *DefaultRepository[T]) GetAll() ([]T, error) {
|
|
||||||
// TODO
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (repo *DefaultRepository[T]) GetById(id uint) (T, error) {
|
|
||||||
// TODO
|
|
||||||
return *new(T), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (repo *DefaultRepository[T]) GetByCriteria(c ISearchCriteria[T]) ([]T, error) {
|
|
||||||
// TODO
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package data
|
package data
|
||||||
|
|
||||||
|
// TODO docstring
|
||||||
type ISearchCriteria[T any] interface {
|
type ISearchCriteria[T any] interface {
|
||||||
Matches(t T) bool
|
Matches(t T) bool
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user