From e3fcd6215918ea420b90354e7ebcd56c9be49537 Mon Sep 17 00:00:00 2001 From: Elias Kohout Date: Sun, 29 Dec 2024 01:05:04 +0100 Subject: [PATCH] change IIdentifiable interface to use a string instead of int --- internal/data/DefaultRepository.go | 17 ++++++----------- internal/data/IIdentifiable.go | 2 +- internal/data/IRepository.go | 2 +- internal/model/article.go | 6 ++---- 4 files changed, 10 insertions(+), 17 deletions(-) diff --git a/internal/data/DefaultRepository.go b/internal/data/DefaultRepository.go index f23bee9..90884d8 100644 --- a/internal/data/DefaultRepository.go +++ b/internal/data/DefaultRepository.go @@ -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 } diff --git a/internal/data/IIdentifiable.go b/internal/data/IIdentifiable.go index f12e704..3398305 100644 --- a/internal/data/IIdentifiable.go +++ b/internal/data/IIdentifiable.go @@ -2,5 +2,5 @@ package data // TODO docstring type IIdentifiable interface { - Id() uint + Id() string // not allowed to contain a ':' } diff --git a/internal/data/IRepository.go b/internal/data/IRepository.go index d1ef3ed..c27e570 100644 --- a/internal/data/IRepository.go +++ b/internal/data/IRepository.go @@ -8,6 +8,6 @@ type IRepository[T IIdentifiable] interface { Update(t T) error Delete(t T) error GetAll() ([]T, error) - GetById(id uint) (T, error) + GetById(id string) (T, error) GetByCriteria(c ISearchCriteria[T]) ([]T, error) } diff --git a/internal/model/article.go b/internal/model/article.go index e405c71..2ea722a 100644 --- a/internal/model/article.go +++ b/internal/model/article.go @@ -8,7 +8,6 @@ import ( // TODO docstring type Article struct { - Identifier uint SourceUrl string PublishDate time.Time FetchDate time.Time @@ -21,7 +20,6 @@ type Article struct { // Generates a hash based on the source url of the article. Can be used to // identify the article. func (article *Article) Hash() string { - hash := sha256.Sum256([]byte(article.SourceUrl)) return hex.EncodeToString(hash[:]) } @@ -30,6 +28,6 @@ func (article *Article) Hash() string { // --- implement IIdentifiable interface --- // Using the Identifier attribute as the Id. -func (article *Article) Id() uint { - return article.Identifier +func (article *Article) Id() string { + return article.Hash() }