change IIdentifiable interface to use a string instead of int

This commit is contained in:
2024-12-29 01:05:04 +01:00
parent b47b6d51b2
commit e3fcd62159
4 changed files with 10 additions and 17 deletions

View File

@@ -3,7 +3,6 @@ package data
import (
"encoding/json"
"errors"
"strconv"
"strings"
)
@@ -27,7 +26,7 @@ func NewDefaultRepository[T IIdentifiable](ds IDatastore, prefix string) (*Defau
// 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)
key := repo.prefix + ":" + t.Id()
exists, err := repo.ds.KeyExists(key)
if err != nil { return err }
if exists { return errors.New("entry with given id already exists") }
@@ -45,7 +44,7 @@ func (repo *DefaultRepository[T]) Create(t T) error {
// 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)
key := repo.prefix + ":" + t.Id()
exists, err := repo.ds.KeyExists(key)
if err != nil { return err }
if !exists { return errors.New("no entry with given id") }
@@ -62,7 +61,7 @@ func (repo *DefaultRepository[T]) Update(t T) error {
// Delete the entry with the same id as t in the repository. Trows an error if
// the connection to the IDatastore fails or the key of t does not exist.
func (repo *DefaultRepository[T]) Delete(t T) error {
key := repo.prefix + ":" + strconv.FormatUint(uint64(t.Id()), 10)
key := repo.prefix + ":" + t.Id()
exists, err := repo.ds.KeyExists(key)
if err != nil { return err }
@@ -86,12 +85,8 @@ func (repo *DefaultRepository[T]) GetAll() ([]T, error) {
splitkey := strings.Split(key, ":")
if splitkey[0] == repo.prefix {
// convert id to an int
id, err := strconv.ParseUint(splitkey[1], 10, 0)
if err != nil { return nil, err }
// retrieve the object
obj, err := repo.GetById(uint(id))
obj, err := repo.GetById(splitkey[1])
if err != nil { return nil, err }
out = append(out, obj)
@@ -103,10 +98,10 @@ func (repo *DefaultRepository[T]) GetAll() ([]T, error) {
// Get the objects of type T from the repository that has the given id. Trows an error
// if the connection to the IDatastore or the decoding process fails.
func (repo *DefaultRepository[T]) GetById(id uint) (T, error) {
func (repo *DefaultRepository[T]) GetById(id string) (T, error) {
var obj T
key := repo.prefix + ":" + strconv.FormatUint(uint64(id), 10)
key := repo.prefix + ":" + id
value, err := repo.ds.Get(key)
if err != nil { return obj, err }