Last active
August 29, 2015 14:06
-
-
Save Gurpartap/e6e1420931f6becf67b2 to your computer and use it in GitHub Desktop.
A user model based on zoom (a lightweight redis orm)
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 models | |
import "github.com/albrow/zoom" | |
func Init() { | |
if err := zoom.Register(&User{}); err != nil { | |
panic(err) | |
} | |
} |
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 main | |
import ( | |
"./app/models" | |
"github.com/albrow/zoom" | |
) | |
func main() { | |
zoom.Init(nil) | |
defer zoom.Close() | |
models.Init() | |
... | |
} |
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 models | |
import ( | |
"github.com/albrow/zoom" | |
) | |
type User struct { | |
Name string `json:"name"` | |
Email string `json:"email"` | |
Password string `json:"password"` | |
zoom.DefaultData | |
zoom.Sync | |
} | |
func (m User) FindAll() []*User { | |
var users []*User | |
if err := zoom.NewQuery("User").Scan(&users); err != nil { | |
panic(err) | |
} | |
for _, u := range users { | |
u.Unlock() | |
} | |
return users | |
} | |
func (m User) FindById(id string) *User { | |
u := &User{} | |
if err := zoom.ScanById(id, u); err != nil { | |
panic(err) | |
} | |
// defer u.Unlock() | |
return u | |
} | |
func (u User) DeleteById(id string) { | |
if err := zoom.DeleteById("User", id); err != nil { | |
panic(err) | |
} | |
defer u.Unlock() | |
} | |
func (u *User) Save() { | |
if err := zoom.Save(u); err != nil { | |
panic(err) | |
} | |
defer u.Unlock() | |
} | |
func (u *User) Delete() { | |
if err := zoom.Delete(u); err != nil { | |
panic(err) | |
} | |
defer u.Unlock() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment