Blog/User/user.go
2024-01-14 21:47:16 +03:00

56 lines
1.2 KiB
Go
Executable file

package Users
import (
"github.com/nspcc-dev/neo-go/pkg/interop"
"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(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+lastIndex, -1)
storage.Put(ctx, userLogin, std.Serialize(user))
}