summaryrefslogtreecommitdiff
path: root/system/db/user.go
diff options
context:
space:
mode:
authorSteve Manuel <nilslice@gmail.com>2016-10-06 03:12:14 -0700
committerSteve Manuel <nilslice@gmail.com>2016-10-06 03:12:14 -0700
commit698173a6176762f966be0abd1dc77b85e482a03a (patch)
tree9613cb11d115bbf644b80a84ad12d1b953d574c7 /system/db/user.go
parent179cb469c9204a463284c5823251e8cea31a4a1e (diff)
adding config, user and refactored some content query code. all in their own files for better maintainability
Diffstat (limited to 'system/db/user.go')
-rw-r--r--system/db/user.go73
1 files changed, 73 insertions, 0 deletions
diff --git a/system/db/user.go b/system/db/user.go
new file mode 100644
index 0000000..e205a47
--- /dev/null
+++ b/system/db/user.go
@@ -0,0 +1,73 @@
+package db
+
+import (
+ "bytes"
+ "encoding/json"
+ "errors"
+
+ "github.com/boltdb/bolt"
+ "github.com/nilslice/cms/system/admin/user"
+)
+
+// ErrUserExists is used for the db to report to admin user of existing user
+var ErrUserExists = errors.New("Error. User exists.")
+
+// SetUser sets key:value pairs in the db for user settings
+func SetUser(usr *user.User) (int, error) {
+ err := store.Update(func(tx *bolt.Tx) error {
+ email := []byte(usr.Email)
+ users := tx.Bucket([]byte("_users"))
+
+ // check if user is found by email, fail if nil
+ exists := users.Get(email)
+ if exists != nil {
+ return ErrUserExists
+ }
+
+ // get NextSequence int64 and set it as the User.ID
+ id, err := users.NextSequence()
+ if err != nil {
+ return err
+ }
+ usr.ID = int(id)
+
+ // marshal User to json and put into bucket
+ j, err := json.Marshal(usr)
+ if err != nil {
+ return err
+ }
+
+ err = users.Put([]byte(usr.Email), j)
+ if err != nil {
+ return err
+ }
+
+ return nil
+ })
+ if err != nil {
+ return 0, err
+ }
+
+ return usr.ID, nil
+}
+
+// User gets the user by email from the db
+func User(email string) ([]byte, error) {
+ val := &bytes.Buffer{}
+ err := store.View(func(tx *bolt.Tx) error {
+ b := tx.Bucket([]byte("_users"))
+ usr := b.Get([]byte(email))
+
+ _, err := val.Write(usr)
+ if err != nil {
+ return err
+ }
+
+ return nil
+ })
+ if err != nil {
+ return nil, err
+ }
+
+ return val.Bytes(), nil
+}