github.com/goplus/yap@v0.8.1/ydb/demo/foo/users_ydb.gox (about)

     1  import (
     2  	"time"
     3  
     4  	_ "github.com/goplus/yap/ydb/mysql"
     5  	_ "github.com/goplus/yap/ydb/sqlite3"
     6  )
     7  
     8  type User struct {
     9  	Id       string `id CHAR(32) UNIQUE`
    10  	Spwd     string
    11  	Salt     string
    12  	Nickname string
    13  	Email    string    `CHAR(64) INDEX`
    14  	Tel      string    `CHAR(16) INDEX`
    15  	Born     time.Time `INDEX`
    16  	Ctime    time.Time `DATETIME(6) INDEX`
    17  }
    18  
    19  func API_Register(id, pwd, nickname, email, tel string, ctime time.Time) error {
    20  	if email == "" && tel == "" {
    21  		return ErrNoEmailAndTel
    22  	}
    23  	limit 3, "email=?", email // one email can't register >3 accounts
    24  	limit 3, "tel=?", tel
    25  
    26  	salt := rand
    27  	spwd := hs256(pwd, salt)
    28  	insert &User{Id: id, Spwd: spwd, Salt: salt, Nickname: nickname, Email: email, Tel: tel, Ctime: ctime}
    29  	return nil
    30  }
    31  
    32  func API_Login(id, pwd string) bool {
    33  	var spwd, salt string
    34  	query "id=?", id
    35  	ret "salt", &salt, "spwd", &spwd
    36  	if noRows {
    37  		return false
    38  	}
    39  	return hs256(pwd, salt) == spwd
    40  }
    41  
    42  engine "sqlite3"
    43  
    44  table User, "user v0.1.0"
    45  
    46  use "user"
    47  
    48  register "user", "pwd", "nickname", "", "", time.now
    49  ret ErrNoEmailAndTel
    50  register "user", "pwd", "nickname", "user@foo.com", "", time.now
    51  ret nil
    52  register "user", "pwd", "nickname", "user@foo.com", "13500000000", time.now
    53  ret ErrDuplicated
    54  
    55  login "", ""
    56  ret false
    57  login "user", "pwd"
    58  ret true