Blog/User/user.go
2024-01-17 18:54:48 +03:00

66 lines
1.5 KiB
Go
Executable file

package Users
import (
"github.com/nspcc-dev/neo-go/pkg/interop"
"github.com/nspcc-dev/neo-go/pkg/interop/contract"
"github.com/nspcc-dev/neo-go/pkg/interop/native/std"
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
)
type User struct {
name string
surname string
login string
password string
ownerHash interop.Hash160
}
const (
lastIndex = "_lastIndex"
)
func NewUser(name string, surname string, login string, password string, owner interop.Hash160) {
ctx := storage.GetContext()
existing_login := storage.Get(ctx, login)
if existing_login != nil {
panic("this login is taken by someone else")
}
user := User{
name: name,
surname: surname,
login: login,
password: password,
ownerHash: owner,
}
saveUser(ctx, login, user)
}
func GetUser(login string) User {
return getUserTst(storage.GetReadOnlyContext(), login)
}
func RateForGas(commentId string, contractHash interop.Hash160, login string) {
ctx := storage.GetContext()
contract.Call(contractHash, "rate", contract.ReadOnly, commentId, storage.Get(ctx, login+"_hash"))
}
func getUserTst(ctx storage.Context, login string) User {
data := storage.Get(ctx, login)
if data == nil {
panic("User does not exist")
}
return std.Deserialize(data.([]byte)).(User)
}
func saveUser(ctx storage.Context, userLogin string, user User) {
runtime.Log("User " + userLogin + " was created")
storage.Put(ctx, userLogin+"_hash", user.ownerHash)
storage.Put(ctx, userLogin, std.Serialize(user))
}