From 2dee88c6a6909243af97b09ab4330ce9cc286818 Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Sat, 11 Nov 2017 02:38:19 +0100 Subject: [PATCH] Cleaned up --- static/src/Home/Home.js | 1 - store/store.go | 31 ++++++++++++++----------------- store/store_test.go | 7 ++++--- store/util.go | 27 +++++---------------------- 4 files changed, 23 insertions(+), 43 deletions(-) diff --git a/static/src/Home/Home.js b/static/src/Home/Home.js index 11ad0fe..0f31ef0 100644 --- a/static/src/Home/Home.js +++ b/static/src/Home/Home.js @@ -45,7 +45,6 @@ export default class HomeComponent extends Component { {links.map((link, i) => - {new URL(link[1]).hostname} diff --git a/store/store.go b/store/store.go index 9c32c4e..b353867 100644 --- a/store/store.go +++ b/store/store.go @@ -23,24 +23,23 @@ type Store struct { // Entry is the data set which is stored in the DB as JSON type Entry struct { - URL string - VisitCount int - RemoteAddr string `json:",omitempty"` - OAuthProvider, OAuthID string - CreatedOn, LastVisit time.Time + URL, OAuthProvider, OAuthID string + VisitCount int + RemoteAddr string `json:",omitempty"` + CreatedOn, LastVisit time.Time } // ErrNoEntryFound is returned when no entry to a id is found -var ErrNoEntryFound = errors.New("no entry found") +var ErrNoEntryFound = errors.New("no entry found with this ID") // ErrNoValidURL is returned when the URL is not valid -var ErrNoValidURL = errors.New("no valid URL") +var ErrNoValidURL = errors.New("the given URL is no valid URL") -// ErrGeneratingTriesFailed is returned when the 10 tries to generate an id failed -var ErrGeneratingTriesFailed = errors.New("could not generate unique id, db full?") +// ErrGeneratingIDFailed is returned when the 10 tries to generate an id failed +var ErrGeneratingIDFailed = errors.New("could not generate unique id, all ten tries failed") // ErrIDIsEmpty is returned when the given ID is empty -var ErrIDIsEmpty = errors.New("id is empty") +var ErrIDIsEmpty = errors.New("the given ID is empty") // New initializes the store with the db func New(storeConfig config.Store, log *logrus.Logger) (*Store, error) { @@ -81,7 +80,7 @@ func (s *Store) GetEntryByID(id string) (*Entry, error) { func (s *Store) IncreaseVisitCounter(id string) error { entry, err := s.GetEntryByID(id) if err != nil { - return err + return errors.Wrap(err, "could not get entry by ID") } entry.VisitCount++ entry.LastVisit = time.Now() @@ -90,9 +89,8 @@ func (s *Store) IncreaseVisitCounter(id string) error { return err } err = s.db.Update(func(tx *bolt.Tx) error { - bucket := tx.Bucket(s.bucketName) - if err := bucket.Put([]byte(id), raw); err != nil { - return errors.Wrap(err, "could not put data into bucket") + if err := tx.Bucket(s.bucketName).Put([]byte(id), raw); err != nil { + return errors.Wrap(err, "could not put updated visitor count JSON into the bucket") } return nil }) @@ -103,8 +101,7 @@ func (s *Store) IncreaseVisitCounter(id string) error { func (s *Store) GetEntryByIDRaw(id string) ([]byte, error) { var raw []byte err := s.db.View(func(tx *bolt.Tx) error { - bucket := tx.Bucket(s.bucketName) - raw = bucket.Get([]byte(id)) + raw = tx.Bucket(s.bucketName).Get([]byte(id)) if raw == nil { return ErrNoEntryFound } @@ -127,7 +124,7 @@ func (s *Store) CreateEntry(entry Entry) (string, error) { } return id, nil } - return "", ErrGeneratingTriesFailed + return "", ErrGeneratingIDFailed } // Close closes the bolt db database diff --git a/store/store_test.go b/store/store_test.go index 4cb4381..f158c50 100644 --- a/store/store_test.go +++ b/store/store_test.go @@ -75,7 +75,7 @@ func TestCreateEntry(t *testing.T) { } for i := 1; i <= 100; i++ { _, err := store.CreateEntry(Entry{URL: "https://golang.org/"}) - if err != nil && err != ErrGeneratingTriesFailed { + if err != nil && err != ErrGeneratingIDFailed { t.Fatalf("unexpected error during creating entry: %v", err) } } @@ -121,8 +121,9 @@ func TestIncreaseVisitCounter(t *testing.T) { if entryBeforeInc.VisitCount+1 != entryAfterInc.VisitCount { t.Fatalf("the increasement was not successful, the visit count is not correct") } - if err = store.IncreaseVisitCounter(""); err != ErrIDIsEmpty { - t.Fatalf("could not get expected '%v' error: %v", ErrIDIsEmpty, err) + errIDIsEmpty := "could not get entry by ID: the given ID is empty" + if err = store.IncreaseVisitCounter(""); err.Error() != errIDIsEmpty { + t.Fatalf("could not get expected '%v'; got: %v", errIDIsEmpty, err) } } diff --git a/store/util.go b/store/util.go index 69414e9..b2daa4b 100644 --- a/store/util.go +++ b/store/util.go @@ -11,19 +11,6 @@ import ( "github.com/pkg/errors" ) -// checkExistens returns true if a entry with a given ID -// exists and false if not -func (s *Store) checkExistence(id string) bool { - raw, err := s.GetEntryByIDRaw(id) - if err != nil && err != ErrNoEntryFound { - return true - } - if raw != nil { - return true - } - return false -} - // createEntryRaw creates a entry with the given key value pair func (s *Store) createEntryRaw(key, value []byte) error { err := s.db.Update(func(tx *bolt.Tx) error { @@ -46,16 +33,12 @@ func (s *Store) createEntry(entry Entry) (string, error) { if err != nil { return "", errors.Wrap(err, "could not generate random string") } - exists := s.checkExistence(id) - if !exists { - entry.CreatedOn = time.Now() - raw, err := json.Marshal(entry) - if err != nil { - return "", err - } - return id, s.createEntryRaw([]byte(id), raw) + entry.CreatedOn = time.Now() + raw, err := json.Marshal(entry) + if err != nil { + return "", err } - return "", errors.New("entry already exists") + return id, s.createEntryRaw([]byte(id), raw) } // generateRandomString generates a random string with an predefined length