change IIdentifiable interface to use a string instead of int
This commit is contained in:
@@ -3,7 +3,6 @@ package data
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"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
|
// error if there already exists an entry with the same id, the json encoding
|
||||||
// fails or the connection to the IDatastore fails.
|
// fails or the connection to the IDatastore fails.
|
||||||
func (repo *DefaultRepository[T]) Create(t T) error {
|
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)
|
exists, err := repo.ds.KeyExists(key)
|
||||||
if err != nil { return err }
|
if err != nil { return err }
|
||||||
if exists { return errors.New("entry with given id already exists") }
|
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
|
// t. Trows an error if the json encoding fails or the connection to the
|
||||||
// IDatastore fails.
|
// IDatastore fails.
|
||||||
func (repo *DefaultRepository[T]) Update(t T) error {
|
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)
|
exists, err := repo.ds.KeyExists(key)
|
||||||
if err != nil { return err }
|
if err != nil { return err }
|
||||||
if !exists { return errors.New("no entry with given id") }
|
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
|
// 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.
|
// the connection to the IDatastore fails or the key of t does not exist.
|
||||||
func (repo *DefaultRepository[T]) Delete(t T) error {
|
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)
|
exists, err := repo.ds.KeyExists(key)
|
||||||
if err != nil { return err }
|
if err != nil { return err }
|
||||||
@@ -86,12 +85,8 @@ func (repo *DefaultRepository[T]) GetAll() ([]T, error) {
|
|||||||
splitkey := strings.Split(key, ":")
|
splitkey := strings.Split(key, ":")
|
||||||
if splitkey[0] == repo.prefix {
|
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
|
// retrieve the object
|
||||||
obj, err := repo.GetById(uint(id))
|
obj, err := repo.GetById(splitkey[1])
|
||||||
if err != nil { return nil, err }
|
if err != nil { return nil, err }
|
||||||
out = append(out, obj)
|
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
|
// 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.
|
// 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
|
var obj T
|
||||||
|
|
||||||
key := repo.prefix + ":" + strconv.FormatUint(uint64(id), 10)
|
key := repo.prefix + ":" + id
|
||||||
value, err := repo.ds.Get(key)
|
value, err := repo.ds.Get(key)
|
||||||
if err != nil { return obj, err }
|
if err != nil { return obj, err }
|
||||||
|
|
||||||
|
|||||||
@@ -2,5 +2,5 @@ package data
|
|||||||
|
|
||||||
// TODO docstring
|
// TODO docstring
|
||||||
type IIdentifiable interface {
|
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
|
Update(t T) error
|
||||||
Delete(t T) error
|
Delete(t T) error
|
||||||
GetAll() ([]T, error)
|
GetAll() ([]T, error)
|
||||||
GetById(id uint) (T, error)
|
GetById(id string) (T, error)
|
||||||
GetByCriteria(c ISearchCriteria[T]) ([]T, error)
|
GetByCriteria(c ISearchCriteria[T]) ([]T, error)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import (
|
|||||||
|
|
||||||
// TODO docstring
|
// TODO docstring
|
||||||
type Article struct {
|
type Article struct {
|
||||||
Identifier uint
|
|
||||||
SourceUrl string
|
SourceUrl string
|
||||||
PublishDate time.Time
|
PublishDate time.Time
|
||||||
FetchDate 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
|
// Generates a hash based on the source url of the article. Can be used to
|
||||||
// identify the article.
|
// identify the article.
|
||||||
func (article *Article) Hash() string {
|
func (article *Article) Hash() string {
|
||||||
|
|
||||||
hash := sha256.Sum256([]byte(article.SourceUrl))
|
hash := sha256.Sum256([]byte(article.SourceUrl))
|
||||||
return hex.EncodeToString(hash[:])
|
return hex.EncodeToString(hash[:])
|
||||||
}
|
}
|
||||||
@@ -30,6 +28,6 @@ func (article *Article) Hash() string {
|
|||||||
// --- implement IIdentifiable interface ---
|
// --- implement IIdentifiable interface ---
|
||||||
|
|
||||||
// Using the Identifier attribute as the Id.
|
// Using the Identifier attribute as the Id.
|
||||||
func (article *Article) Id() uint {
|
func (article *Article) Id() string {
|
||||||
return article.Identifier
|
return article.Hash()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user