change IIdentifiable interface to use a string instead of int
This commit is contained in:
@@ -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 }
|
||||
|
||||
|
||||
@@ -2,5 +2,5 @@ package data
|
||||
|
||||
// TODO docstring
|
||||
type IIdentifiable interface {
|
||||
Id() uint
|
||||
Id() string // not allowed to contain a ':'
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user