2016-07-06 03:55:05 +05:30
|
|
|
|
package data
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"log"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
|
|
"elyby/minecraft-skinsystem/lib/services"
|
|
|
|
|
"elyby/minecraft-skinsystem/lib/tools"
|
2016-08-27 02:15:55 +05:30
|
|
|
|
|
|
|
|
|
"github.com/mediocregopher/radix.v2/redis"
|
2016-07-06 03:55:05 +05:30
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type SkinItem struct {
|
2016-09-15 03:49:16 +05:30
|
|
|
|
UserId int `json:"userId"`
|
|
|
|
|
Username string `json:"username"`
|
|
|
|
|
SkinId int `json:"skinId"`
|
|
|
|
|
Url string `json:"url"`
|
|
|
|
|
Is1_8 bool `json:"is1_8"`
|
|
|
|
|
IsSlim bool `json:"isSlim"`
|
|
|
|
|
Hash string `json:"hash"`
|
|
|
|
|
oldUsername string
|
2016-07-06 03:55:05 +05:30
|
|
|
|
}
|
|
|
|
|
|
2016-09-15 03:49:16 +05:30
|
|
|
|
const accountIdToUsernameKey string = "hash:username-to-account-id"
|
|
|
|
|
|
2016-07-06 03:55:05 +05:30
|
|
|
|
func (s *SkinItem) Save() {
|
|
|
|
|
str, _ := json.Marshal(s)
|
2016-09-15 03:49:16 +05:30
|
|
|
|
pool, _ := services.RedisPool.Get()
|
|
|
|
|
pool.Cmd("MULTI")
|
|
|
|
|
|
|
|
|
|
// Если пользователь сменил ник, то мы должны удать его ключ
|
|
|
|
|
if (s.oldUsername != "" && s.oldUsername != s.Username) {
|
|
|
|
|
pool.Cmd("DEL", tools.BuildKey(s.oldUsername))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Если это новая запись или если пользователь сменил ник, то обновляем значение в хэш-таблице
|
|
|
|
|
if (s.oldUsername != "" || s.oldUsername != s.Username) {
|
|
|
|
|
pool.Cmd("HSET", accountIdToUsernameKey, s.UserId, s.Username)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pool.Cmd("SET", tools.BuildKey(s.Username), str)
|
|
|
|
|
|
|
|
|
|
pool.Cmd("EXEC")
|
|
|
|
|
|
|
|
|
|
s.oldUsername = s.Username
|
2016-07-06 03:55:05 +05:30
|
|
|
|
}
|
|
|
|
|
|
2016-09-15 03:49:16 +05:30
|
|
|
|
func (s *SkinItem) Delete() {
|
|
|
|
|
if (s.oldUsername == "") {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pool, _ := services.RedisPool.Get()
|
|
|
|
|
pool.Cmd("MULTI")
|
|
|
|
|
|
|
|
|
|
pool.Cmd("DEL", tools.BuildKey(s.oldUsername))
|
|
|
|
|
pool.Cmd("HDEL", accountIdToUsernameKey, s.UserId)
|
|
|
|
|
|
|
|
|
|
pool.Cmd("EXEC")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func FindByUsername(username string) (SkinItem, error) {
|
2016-07-06 03:55:05 +05:30
|
|
|
|
var record SkinItem;
|
2016-08-27 02:15:55 +05:30
|
|
|
|
response := services.RedisPool.Cmd("GET", tools.BuildKey(username));
|
|
|
|
|
if (response.IsType(redis.Nil)) {
|
|
|
|
|
return record, DataNotFound{username}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result, err := response.Str()
|
2016-07-06 03:55:05 +05:30
|
|
|
|
if (err == nil) {
|
|
|
|
|
decodeErr := json.Unmarshal([]byte(result), &record)
|
|
|
|
|
if (decodeErr != nil) {
|
|
|
|
|
log.Println("Cannot decode record data")
|
|
|
|
|
}
|
2016-09-15 03:49:16 +05:30
|
|
|
|
|
|
|
|
|
record.oldUsername = record.Username
|
2016-07-06 03:55:05 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return record, err
|
|
|
|
|
}
|
2016-09-15 03:49:16 +05:30
|
|
|
|
|
|
|
|
|
func FindById(id int) (SkinItem, error) {
|
|
|
|
|
response := services.RedisPool.Cmd("HGET", accountIdToUsernameKey, id);
|
|
|
|
|
if (response.IsType(redis.Nil)) {
|
|
|
|
|
return SkinItem{}, DataNotFound{"unknown"}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
username, _ := response.Str()
|
|
|
|
|
|
|
|
|
|
return FindByUsername(username)
|
|
|
|
|
}
|