diff --git a/internal/data/DefaultRepository.go b/internal/data/DefaultRepository.go new file mode 100644 index 0000000..208607d --- /dev/null +++ b/internal/data/DefaultRepository.go @@ -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 +} diff --git a/internal/data/FileDatastore.go b/internal/data/FileDatastore.go index fbcf6a0..cd4a063 100644 --- a/internal/data/FileDatastore.go +++ b/internal/data/FileDatastore.go @@ -7,13 +7,13 @@ import ( "encoding/json" ) + // A very simple datastructure, implementing the IDatastore interface. It uses // a simple text file to the data as json. type FileDatastore struct { path string } - // Creates a new FileDatastore object, creating the storage file in the // process. 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 // cannot be opened or the contents cannot be decoded or encoded correctly. func (fds *FileDatastore) Delete(key string) error { - m, err := fds.readMapObj() if err != nil { return err } diff --git a/internal/data/IDatastore.go b/internal/data/IDatastore.go index d735091..3f6255c 100644 --- a/internal/data/IDatastore.go +++ b/internal/data/IDatastore.go @@ -1,5 +1,6 @@ package data +// TODO docstring type IDatastore interface { Set(key string, val string) error KeyExists(key string) (bool, error) diff --git a/internal/data/IIdentifiable.go b/internal/data/IIdentifiable.go index c54759d..f12e704 100644 --- a/internal/data/IIdentifiable.go +++ b/internal/data/IIdentifiable.go @@ -1,5 +1,6 @@ package data +// TODO docstring type IIdentifiable interface { Id() uint } diff --git a/internal/data/IRepository.go b/internal/data/IRepository.go index 3c9496f..d1ef3ed 100644 --- a/internal/data/IRepository.go +++ b/internal/data/IRepository.go @@ -1,12 +1,5 @@ package data -import ( - "encoding/json" - "errors" - "strconv" -) - - // An interface to manage generic structure objects persistently. Should use an // IDatastore as the interface that actually stores and retrieves the data from // an external source. @@ -18,73 +11,3 @@ type IRepository[T IIdentifiable] interface { GetById(id uint) (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 -} diff --git a/internal/data/ISearchCriteria.go b/internal/data/ISearchCriteria.go index 194a88c..08f2d75 100644 --- a/internal/data/ISearchCriteria.go +++ b/internal/data/ISearchCriteria.go @@ -1,5 +1,6 @@ package data +// TODO docstring type ISearchCriteria[T any] interface { Matches(t T) bool }