forked from anz-bank/go-course
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SyncStore which implements the Storer interface
SyncStore implements the Storer interface and supplies a NewSyncStore factory function. - Uuid increment is handled internally by SyncStore Create - Read will return an empty Puppy struct if provided uuid doesn't exist - Update returns a bool whether a matching identifier was modified - Destroy returns a bool whether a matching identifier was modified Lab 06 (anz-bank#537)
- Loading branch information
1 parent
dc94061
commit 33a306c
Showing
3 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package main | ||
|
||
// NewSyncStore returns a pointer to a new instance of the SyncStore struct which implements the Storer interface. | ||
func NewSyncStore() Storer { | ||
return &SyncStore{ | ||
uuid: 0, | ||
} | ||
} | ||
|
||
// Create increments the uuid and adds the provided Puppy struct to the store with this identifier. | ||
func (store *SyncStore) Create(puppy Puppy) int { | ||
puppy.ID = store.uuid | ||
store.Store(puppy.ID, puppy) | ||
store.uuid++ | ||
|
||
return puppy.ID | ||
} | ||
|
||
// Read returns the puppy matching the provided uuid. | ||
// An empty Puppy struct is returned if the identifier does not exist. | ||
func (store *SyncStore) Read(id int) Puppy { | ||
if value, ok := store.Load(id); ok { | ||
return value.(Puppy) | ||
} | ||
|
||
return Puppy{} | ||
} | ||
|
||
// Update modifies the puppy matching the provided uuid in the store with the provided Puppy struct. | ||
// It returns a bool whether a matching identifier was modified or not. | ||
func (store *SyncStore) Update(id int, puppy Puppy) bool { | ||
if _, ok := store.Load(id); !ok { | ||
return false | ||
} | ||
|
||
puppy.ID = id | ||
store.Store(id, puppy) | ||
return true | ||
} | ||
|
||
// Destroy removes the puppy matching the provided uuid from the store. | ||
// It returns a bool whether a matching identifier was deleted or not. | ||
func (store *SyncStore) Destroy(id int) bool { | ||
if _, ok := store.Load(id); !ok { | ||
return false | ||
} | ||
|
||
store.Delete(id) | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters