Last active
May 30, 2017 02:10
-
-
Save josephspurrier/da81e0755011b334f07756c802d5723a to your computer and use it in GitHub Desktop.
User Model in Bolt
This file contains hidden or 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
package database | |
import ( | |
"log" | |
"github.com/boltdb/bolt" | |
) | |
var ( | |
BoltDB *bolt.DB // Bolt wrapper | |
databases DatabaseInfo // Database info | |
) | |
type DatabaseType string | |
const ( | |
TypeBolt DatabaseType = "Bolt" | |
) | |
type DatabaseInfo struct { | |
Type DatabaseType | |
Bolt BoltInfo | |
} | |
// BoltInfo is the details for the database connection | |
type BoltInfo struct { | |
Path string | |
} | |
// Connect to the database | |
func Connect(d DatabaseInfo) { | |
var err error | |
// Store the config | |
databases = d | |
switch d.Type { | |
case TypeBolt: | |
// Connect to Bolt | |
if BoltDB, err = bolt.Open(d.Bolt.Path, 0600, nil); err != nil { | |
log.Println("Bolt Driver Error", err) | |
} | |
default: | |
log.Println("No registered database in config") | |
} | |
} | |
// ReadConfig returns the database information | |
func ReadConfig() DatabaseInfo { | |
return databases | |
} |
This file contains hidden or 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
package user | |
import ( | |
"encoding/json" | |
"errors" | |
"fmt" | |
"log" | |
"time" | |
"github.com/josephspurrier/gowebapi/shared/database" | |
z "github.com/josephspurrier/gowebapi/shared/mission" | |
"github.com/boltdb/bolt" | |
) | |
// Name of the bucket | |
const bucketName = "user" | |
var ( | |
/*Permissions = z.Mission{ | |
z.L{0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0}, // FirstName | |
z.L{C: {0, 0, 0}, R: {1, 1, 0}, U: {0, 0, 0}, D: {0, 0, 0}}, // LastName | |
z.L{C: {0, 0, 0}, R: {1, 1, 0}, U: {0, 0, 0}, D: {0, 0, 0}}, // Email | |
z.L{C: {0, 0, 0}, R: {1, 1, 0}, U: {0, 0, 0}, D: {0, 0, 0}}, // Password | |
z.L{C: {0, 0, 0}, R: {1, 1, 0}, U: {0, 0, 0}, D: {0, 0, 0}}, // Status | |
z.L{C: {0, 0, 0}, R: {1, 1, 0}, U: {0, 0, 0}, D: {0, 0, 0}}, // CreatedAt | |
z.L{C: {0, 0, 0}, R: {1, 1, 0}, U: {0, 0, 0}, D: {0, 0, 0}}, // UpdatedAt | |
z.L{C: {0, 0, 0}, R: {1, 1, 0}, U: {0, 0, 0}, D: {0, 0, 0}}, // DeletedAt | |
}*/ | |
Permissions = z.Mission{ | |
"Id": {{0, 0, 0}, {1, 1, 0}, {0, 0, 0}, {0, 0, 0}}, | |
"FirstName": {{0, 0, 0}, {1, 1, 0}, {0, 0, 0}, {0, 0, 0}}, | |
"LastName": {{0, 0, 0}, {1, 1, 0}, {0, 0, 0}, {0, 0, 0}}, | |
"Email": {{0, 0, 0}, {1, 1, 0}, {0, 0, 0}, {0, 0, 0}}, | |
"Password": {{0, 0, 0}, {1, 1, 0}, {0, 0, 0}, {0, 0, 0}}, | |
"Status": {{0, 0, 0}, {1, 1, 0}, {0, 0, 0}, {0, 0, 0}}, | |
"CreatedAt": {{0, 0, 0}, {1, 1, 0}, {0, 0, 0}, {0, 0, 0}}, | |
"UpdatedAt": {{0, 0, 0}, {1, 1, 0}, {0, 0, 0}, {0, 0, 0}}, | |
"DeletedAt": {{0, 0, 0}, {1, 1, 0}, {0, 0, 0}, {0, 0, 0}}, | |
} | |
) | |
// Entity information | |
type Entity struct { | |
Id string `json:"id"` | |
FirstName string `json:"first_name" require:"true"` | |
LastName string `json:"last_name" require:"true"` | |
Email string `json:"email" require:"true"` | |
Password string `json:"password" require:"true"` | |
Status string `json:"status"` | |
CreatedAt time.Time `json:"created_at"` | |
UpdatedAt time.Time `json:"updated_at"` | |
DeletedAt time.Time `json:"deleted_at"` | |
} | |
// Group list | |
type Group []Entity | |
// Errors | |
var ( | |
ErrNoResult = errors.New("no results") | |
ErrNoChange = errors.New("no change") | |
) | |
// New entity | |
func New() *Entity { | |
i := &Entity{} | |
return i | |
} | |
// List all users | |
func ListAll() (Group, error) { | |
var u Group | |
e := database.BoltDB.View(func(tx *bolt.Tx) error { | |
// Get the bucket | |
b := tx.Bucket([]byte(bucketName)) | |
// If bucket is not found | |
if b == nil { | |
return ErrNoResult | |
} | |
// Get the iterator | |
c := b.Cursor() | |
for k, v := c.First(); k != nil; k, v = c.Next() { | |
var single Entity | |
// Decode the record | |
err := json.Unmarshal(v, &single) | |
if err != nil { | |
log.Println(err) | |
continue | |
} | |
u = append(u, single) | |
} | |
return nil | |
}) | |
return u, e | |
} | |
// Delete all entities | |
func DeleteAll() (int, error) { | |
// Number of items | |
count := 0 | |
e := database.BoltDB.Update(func(tx *bolt.Tx) error { | |
// Get the bucket | |
b := tx.Bucket([]byte(bucketName)) | |
// If bucket is not found | |
if b == nil { | |
return ErrNoChange | |
} | |
// Number of current level items | |
count = b.Stats().KeyN | |
err := tx.DeleteBucket([]byte(bucketName)) | |
// If bucket is not found | |
if err == bolt.ErrBucketNotFound { | |
return ErrNoResult | |
} else if err != nil { | |
// Set the number of items deleted | |
count = 0 | |
return err | |
} | |
return nil | |
}) | |
return count, e | |
} | |
// Create a user | |
func (u *Entity) Create() (int, error) { | |
// Number of items | |
count := 0 | |
e := database.BoltDB.Update(func(tx *bolt.Tx) error { | |
// Create the bucket | |
bucket, err := tx.CreateBucketIfNotExists([]byte(bucketName)) | |
if err != nil { | |
return err | |
} | |
// Set the time | |
now := time.Now() | |
u.CreatedAt = now | |
if len(u.Id) == 0 { | |
id, err := bucket.NextSequence() | |
if err != nil { | |
return err | |
} | |
u.Id = fmt.Sprintf("%d", id) | |
} | |
// Encode the record | |
bytes, err := json.Marshal(u) | |
if err != nil { | |
return err | |
} | |
// Store the record | |
if err = bucket.Put([]byte(u.Id), bytes); err != nil { | |
return err | |
} | |
// Set then number of items added | |
count = 1 | |
return nil | |
}) | |
return count, e | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment