forked from leeprovoost/go-rest-api-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
65 lines (57 loc) · 1.49 KB
/
database.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import "github.com/palantir/stacktrace"
// DataStorer defines all the database operations
type DataStorer interface {
ListUsers() ([]User, error)
GetUser(i int) (User, error)
AddUser(u User) (User, error)
UpdateUser(u User) (User, error)
DeleteUser(i int) error
}
// MockDB will hold the connection and key db info
type MockDB struct {
UserList map[int]User
MaxUserID int
}
// ListUsers returns a list of JSON documents
func (db *MockDB) ListUsers() ([]User, error) {
var list []User
for _, v := range db.UserList {
list = append(list, v)
}
return list, nil
}
// GetUser returns a single JSON document
func (db *MockDB) GetUser(i int) (User, error) {
user, ok := db.UserList[i]
if !ok {
return User{}, stacktrace.NewError("Failure trying to retrieve user")
}
return user, nil
}
// AddUser adds a User JSON document, returns the JSON document with the generated id
func (db *MockDB) AddUser(u User) (User, error) {
db.MaxUserID = db.MaxUserID + 1
u.ID = db.MaxUserID
db.UserList[db.MaxUserID] = u
return u, nil
}
// UpdateUser updates an existing user
func (db *MockDB) UpdateUser(u User) (User, error) {
id := u.ID
_, ok := db.UserList[id]
if !ok {
return u, stacktrace.NewError("Failure trying to update user")
}
db.UserList[id] = u
return db.UserList[id], nil
}
// DeleteUser deletes a user
func (db *MockDB) DeleteUser(i int) error {
_, ok := db.UserList[i]
if !ok {
return stacktrace.NewError("Failure trying to delete user")
}
delete(db.UserList, i)
return nil
}