Browse Source

Cleaned up

dependabot/npm_and_yarn/web/prismjs-1.21.0
Max Schmitt 8 years ago
parent
commit
2dee88c6a6
  1. 1
      static/src/Home/Home.js
  2. 25
      store/store.go
  3. 7
      store/store_test.go
  4. 17
      store/util.go

1
static/src/Home/Home.js

@ -45,7 +45,6 @@ export default class HomeComponent extends Component {
<Card.Group itemsPerRow="2"> <Card.Group itemsPerRow="2">
{links.map((link, i) => <Card key={i}> {links.map((link, i) => <Card key={i}>
<Card.Content> <Card.Content>
<Image floated='right' size='mini' src='/assets/images/avatar/large/steve.jpg' />
<Card.Header> <Card.Header>
{new URL(link[1]).hostname} {new URL(link[1]).hostname}
</Card.Header> </Card.Header>

25
store/store.go

@ -23,24 +23,23 @@ type Store struct {
// Entry is the data set which is stored in the DB as JSON // Entry is the data set which is stored in the DB as JSON
type Entry struct { type Entry struct {
URL string URL, OAuthProvider, OAuthID string
VisitCount int VisitCount int
RemoteAddr string `json:",omitempty"` RemoteAddr string `json:",omitempty"`
OAuthProvider, OAuthID string
CreatedOn, LastVisit time.Time CreatedOn, LastVisit time.Time
} }
// ErrNoEntryFound is returned when no entry to a id is found // 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 // 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 // ErrGeneratingIDFailed is returned when the 10 tries to generate an id failed
var ErrGeneratingTriesFailed = errors.New("could not generate unique id, db full?") var ErrGeneratingIDFailed = errors.New("could not generate unique id, all ten tries failed")
// ErrIDIsEmpty is returned when the given ID is empty // 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 // New initializes the store with the db
func New(storeConfig config.Store, log *logrus.Logger) (*Store, error) { 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 { func (s *Store) IncreaseVisitCounter(id string) error {
entry, err := s.GetEntryByID(id) entry, err := s.GetEntryByID(id)
if err != nil { if err != nil {
return err return errors.Wrap(err, "could not get entry by ID")
} }
entry.VisitCount++ entry.VisitCount++
entry.LastVisit = time.Now() entry.LastVisit = time.Now()
@ -90,9 +89,8 @@ func (s *Store) IncreaseVisitCounter(id string) error {
return err return err
} }
err = s.db.Update(func(tx *bolt.Tx) error { err = s.db.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket(s.bucketName) if err := tx.Bucket(s.bucketName).Put([]byte(id), raw); err != nil {
if err := bucket.Put([]byte(id), raw); err != nil { return errors.Wrap(err, "could not put updated visitor count JSON into the bucket")
return errors.Wrap(err, "could not put data into bucket")
} }
return nil return nil
}) })
@ -103,8 +101,7 @@ func (s *Store) IncreaseVisitCounter(id string) error {
func (s *Store) GetEntryByIDRaw(id string) ([]byte, error) { func (s *Store) GetEntryByIDRaw(id string) ([]byte, error) {
var raw []byte var raw []byte
err := s.db.View(func(tx *bolt.Tx) error { err := s.db.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket(s.bucketName) raw = tx.Bucket(s.bucketName).Get([]byte(id))
raw = bucket.Get([]byte(id))
if raw == nil { if raw == nil {
return ErrNoEntryFound return ErrNoEntryFound
} }
@ -127,7 +124,7 @@ func (s *Store) CreateEntry(entry Entry) (string, error) {
} }
return id, nil return id, nil
} }
return "", ErrGeneratingTriesFailed return "", ErrGeneratingIDFailed
} }
// Close closes the bolt db database // Close closes the bolt db database

7
store/store_test.go

@ -75,7 +75,7 @@ func TestCreateEntry(t *testing.T) {
} }
for i := 1; i <= 100; i++ { for i := 1; i <= 100; i++ {
_, err := store.CreateEntry(Entry{URL: "https://golang.org/"}) _, 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) t.Fatalf("unexpected error during creating entry: %v", err)
} }
} }
@ -121,8 +121,9 @@ func TestIncreaseVisitCounter(t *testing.T) {
if entryBeforeInc.VisitCount+1 != entryAfterInc.VisitCount { if entryBeforeInc.VisitCount+1 != entryAfterInc.VisitCount {
t.Fatalf("the increasement was not successful, the visit count is not correct") t.Fatalf("the increasement was not successful, the visit count is not correct")
} }
if err = store.IncreaseVisitCounter(""); err != ErrIDIsEmpty { errIDIsEmpty := "could not get entry by ID: the given ID is empty"
t.Fatalf("could not get expected '%v' error: %v", ErrIDIsEmpty, err) if err = store.IncreaseVisitCounter(""); err.Error() != errIDIsEmpty {
t.Fatalf("could not get expected '%v'; got: %v", errIDIsEmpty, err)
} }
} }

17
store/util.go

@ -11,19 +11,6 @@ import (
"github.com/pkg/errors" "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 // createEntryRaw creates a entry with the given key value pair
func (s *Store) createEntryRaw(key, value []byte) error { func (s *Store) createEntryRaw(key, value []byte) error {
err := s.db.Update(func(tx *bolt.Tx) 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 { if err != nil {
return "", errors.Wrap(err, "could not generate random string") return "", errors.Wrap(err, "could not generate random string")
} }
exists := s.checkExistence(id)
if !exists {
entry.CreatedOn = time.Now() entry.CreatedOn = time.Now()
raw, err := json.Marshal(entry) raw, err := json.Marshal(entry)
if err != nil { if err != nil {
return "", err return "", err
} }
return id, s.createEntryRaw([]byte(id), raw) return id, s.createEntryRaw([]byte(id), raw)
}
return "", errors.New("entry already exists")
} }
// generateRandomString generates a random string with an predefined length // generateRandomString generates a random string with an predefined length

Loading…
Cancel
Save