- auto gen.
This commit is contained in:
53
models/object.go
Normal file
53
models/object.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
Objects map[string]*Object
|
||||
)
|
||||
|
||||
type Object struct {
|
||||
ObjectId string
|
||||
Score int64
|
||||
PlayerName string
|
||||
}
|
||||
|
||||
func init() {
|
||||
Objects = make(map[string]*Object)
|
||||
Objects["hjkhsbnmn123"] = &Object{"hjkhsbnmn123", 100, "astaxie"}
|
||||
Objects["mjjkxsxsaa23"] = &Object{"mjjkxsxsaa23", 101, "someone"}
|
||||
}
|
||||
|
||||
func AddOne(object Object) (ObjectId string) {
|
||||
object.ObjectId = "astaxie" + strconv.FormatInt(time.Now().UnixNano(), 10)
|
||||
Objects[object.ObjectId] = &object
|
||||
return object.ObjectId
|
||||
}
|
||||
|
||||
func GetOne(ObjectId string) (object *Object, err error) {
|
||||
if v, ok := Objects[ObjectId]; ok {
|
||||
return v, nil
|
||||
}
|
||||
return nil, errors.New("ObjectId Not Exist")
|
||||
}
|
||||
|
||||
func GetAll() map[string]*Object {
|
||||
return Objects
|
||||
}
|
||||
|
||||
func Update(ObjectId string, Score int64) (err error) {
|
||||
if v, ok := Objects[ObjectId]; ok {
|
||||
v.Score = Score
|
||||
return nil
|
||||
}
|
||||
return errors.New("ObjectId Not Exist")
|
||||
}
|
||||
|
||||
func Delete(ObjectId string) {
|
||||
delete(Objects, ObjectId)
|
||||
}
|
||||
|
||||
86
models/user.go
Normal file
86
models/user.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
UserList map[string]*User
|
||||
)
|
||||
|
||||
func init() {
|
||||
UserList = make(map[string]*User)
|
||||
u := User{"user_11111", "astaxie", "11111", Profile{"male", 20, "Singapore", "astaxie@gmail.com"}}
|
||||
UserList["user_11111"] = &u
|
||||
}
|
||||
|
||||
type User struct {
|
||||
Id string
|
||||
Username string
|
||||
Password string
|
||||
Profile Profile
|
||||
}
|
||||
|
||||
type Profile struct {
|
||||
Gender string
|
||||
Age int
|
||||
Address string
|
||||
Email string
|
||||
}
|
||||
|
||||
func AddUser(u User) string {
|
||||
u.Id = "user_" + strconv.FormatInt(time.Now().UnixNano(), 10)
|
||||
UserList[u.Id] = &u
|
||||
return u.Id
|
||||
}
|
||||
|
||||
func GetUser(uid string) (u *User, err error) {
|
||||
if u, ok := UserList[uid]; ok {
|
||||
return u, nil
|
||||
}
|
||||
return nil, errors.New("User not exists")
|
||||
}
|
||||
|
||||
func GetAllUsers() map[string]*User {
|
||||
return UserList
|
||||
}
|
||||
|
||||
func UpdateUser(uid string, uu *User) (a *User, err error) {
|
||||
if u, ok := UserList[uid]; ok {
|
||||
if uu.Username != "" {
|
||||
u.Username = uu.Username
|
||||
}
|
||||
if uu.Password != "" {
|
||||
u.Password = uu.Password
|
||||
}
|
||||
if uu.Profile.Age != 0 {
|
||||
u.Profile.Age = uu.Profile.Age
|
||||
}
|
||||
if uu.Profile.Address != "" {
|
||||
u.Profile.Address = uu.Profile.Address
|
||||
}
|
||||
if uu.Profile.Gender != "" {
|
||||
u.Profile.Gender = uu.Profile.Gender
|
||||
}
|
||||
if uu.Profile.Email != "" {
|
||||
u.Profile.Email = uu.Profile.Email
|
||||
}
|
||||
return u, nil
|
||||
}
|
||||
return nil, errors.New("User Not Exist")
|
||||
}
|
||||
|
||||
func Login(username, password string) bool {
|
||||
for _, u := range UserList {
|
||||
if u.Username == username && u.Password == password {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func DeleteUser(uid string) {
|
||||
delete(UserList, uid)
|
||||
}
|
||||
Reference in New Issue
Block a user